In this post the basic usage of
quartz scheduler is shown. You can use quartz scheduler to perform and monitor repetitive tasks easily in Java.
Basic steps for executing jobs using quartz scheduler
- Step 1: Create a Job Class that implements the quartz job interface
- Step 2: Create a handle to the job in a separate class, e.g. Main
- Step 3: Create a Trigger, independent of the job, specify how often you want a job to be executed
- Step 4: Create a Scheduler and associate a trigger with a job
- Step 5: Start the scheduler which will trigger the job based on the configuration present in the trigger
In short, you
(i) create a Job(s), (ii) create a Trigger(s), and (iii) create a Scheduler = Job + Trigger. You start the scheduler to run the jobs.
Sample Triggers,
This trigger will run any job, once, now.
Trigger triggerNow = TriggerBuilder.newTrigger() .withIdentity("NowTrigger").startNow().build();
This trigger will run any job every 1 second, indefinitely.
Trigger triggerEverySecond = TriggerBuilder.newTrigger().withIdentity("NameOneSecondTrigger", "GroupForever").withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever()).build();
No comments:
Post a Comment