Please enable JavaScript.
Coggle requires JavaScript to display documents.
Quartz - Coggle Diagram
Quartz
Jobs & Job Details
-
When a trigger associated with a job fires, scheduler's worker thread invokes the execute method and passes in the JobExcecutionContext. This has information about the runtime environment, handle to the scheduler, handle to the trigger, the job's JobDetail object etc
JobDetail object is created by the Quartz client at the time the job is added to the scheduler. Comes with various property settings for the job as well as JobDataMap that stores state information for a given instance of the job
Job class implementation has code that knows how do the actual work of the particular type of job. These classes do not have state. The state can be provided via the JobDetail class.
Create a class say SendReportJob that implements the Job interface. The execute method then contains the code that you need implement or call a service that needs to do the job.
Implementation
-
Create a Trigger specifying the schedule, interval etc
Then create a JobDetail class passing in the SendReportJob.class (not instance) as a constructor argument
Create a class say SendReportJob that implements the Job interface. The execute method then contains the code that you need implement or call a service that needs to do the job.
Execution
The scheduler is given a JobDetail instance, it knows the type of job to be executed since it is provided the job class.
Each time the scheduler executes the job, it creates a new instance of the class before calling the execute method
When the execution is complete, the job instance is garbage collected. Jobs should have no argument constructor and state data should not be defined in job class
Thus for a given job class, many instances can be stored in the scheduler by creating many instances of the JobDetails, each with its own set of properties and JobDataMap. When a trigger fires, the JobDetail it is associated to is loaded and and the job class it is associated with it is instantiated by the default JobFactory. It then tries to set properties of the by calling setter methods that match the names of the keys in the jobDataMap
JobDataMap
-
-
If you add setter methods to your job class that correspond to the names of keys in the JobDataMap (such as a setJobSays(String val) method for the data in the example above), then Quartz’s default JobFactory implementation will automatically call those setters when the job is instantiated, thus preventing the need to explicitly get the values out of the map within your execute method.
-
API Interface
-
-
-
Job: Interface with an execute() method that must be implemented by component that have to be run as jobs by the scheduler
-
-
Listeners
Listeners are objects that you create to perform actions based on events occurring within the scheduler.
TriggerListener: receive events related to Triggers, such as trigger firings, trigger mis-firings
JobListener: receive events related to jobs such as when a job is about to be executed and when has completed execution
To create your own listeners you can either implement specific interfaces or extend specific classes
Listeners are registered with the scheduler’s ListenerManager along with a Matcher that describes which Jobs/Triggers the listener wants to receive events for.
Listeners are registered with the scheduler during run time, and are NOT stored in the JobStore along with the jobs and triggers. This is because listeners are typically an integration point with your application. Hence, each time your application runs, the listeners need to be re-registered with the scheduler.
Scheduler Listener: receive notification of events within the Scheduler itself - not necessarily events related to a specific trigger or job.
Scheduler-related events include: the addition of a job/trigger, the removal of a job/trigger, a serious error within the scheduler, notification of the scheduler being shutdown, and others.
Core
-
-
-
Once the scheduler is started it can be placed in a stand-by mode or shut down. If it is shut down, it has to reinstantiated
.Triggers don't fire when a scheduler is in stand by mode
Once created scheduler can be used to add, remove and list triggers and jobs.
Triggers
-
Priority
When there are more triggers than available threads, assigning a priority to certain triggers ensures that they are fired first
Misfire instructions: A misfire occurs if a persistent trigger “misses” its firing time because of the scheduler being shutdown, or because there are no available threads in Quartz’s thread pool for executing the job. The different trigger types have different misfire instructions available to them. Default is "smart policy"
Simple Triggers
When you need to have a job execute exactly once at a specific time or or at a specific time followed by repeats at a specific interval
Can specify startTime, endTime, repeat count and repeat interval
If "smart policy" is chosen for misfire then the trigger dynamically chooses between its various MISFIRE instructions, based on the configuration and state of the given SimpleTrigger instance
Cron Triggers
More versatile, can setup schedule that recurs based on calendar-like notions
-
-
-
Configuration
ThreadPool - provides threads to execute jobs. Needs to be tuned depending on application. If there are too few threads, results in misfires. Default thread pool is called org.qartz.simpl.SimpleThreadPool
-
-
Scheduler: needs a name, RMI instances, should be provided instances of JobStores and ThreadPool
Job Stores
JobStore’s are responsible for keeping track of all the “work data” that you give to the scheduler: jobs, triggers, calendars, etc.
Never use a JobStore instance directly in your code. For some reason many people attempt to do this. The JobStore is for behind-the-scenes use of Quartz itself. You have to tell Quartz (through configuration) which JobStore to use, but then you should only work with the Scheduler interface in your code.
Types
RAMJobStore: Keeps data in RAM. Simple to implement and performant. It is not durable, lose information when application restarts or crashes
JDBCJobStore - stores data in database. Need to created Quartz related tables. Need to specify Transaction Manager and a DataSource. Can specify a jdbcDataStore by specifying a config property in Quartz configuration