Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs.
This QuartzSchedulerExample will create scheduler
Job Scheduling
Jobs are scheduled to run when a given Trigger occurs. Triggers can be created with nearly any combination of the following directives:
- at a certain time of day (to the millisecond)
- on certain days of the week
- on certain days of the month
- on certain days of the year
- not on certain days listed within a registered Calendar (such as business holidays)
- repeated a specific number of times
- repeated until a specific time/date
- repeated indefinitely
- repeated with a delay interval
Job Execution
- Jobs can be any Java class that implements the simple Job interface, leaving infinite possibilities for the work your Jobs can perform.
- Job class instances can be instantiated by Quartz, or by your application's framework.
- When a Trigger occurs, the scheduler notifies zero or more Java objects implementing the JobListener and TriggerListener interfaces.
- As Jobs are completed, they return a JobCompletionCode which informs the scheduler of success or failure.
First Quartz Program :
This example is designed to demonstrate how to get up and running with Quartz. This example will fire off a simple job that says "Hello Job".
The program will perform the following actions:
- Start up the Quartz Scheduler
- Schedule a job to run every 5 seconds
- Wait for 90 seconds to give Quartz a chance to run the job
- Shut down the Scheduler
HelloJob :
HelloJob is a simple job that implements the Job interface and logs time exactly when the job is run.
public class HelloJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello Job! - "+ new Date()); } }
This QuartzSchedulerExample will create scheduler
public class QuartzSchedulerExample { public static void main( String[] args ) throws Exception { JobDetail job = JobBuilder.newJob(HelloJob.class) .withIdentity("job1", "group1").build(); Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("trigger1", "group1") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build(); //schedule it Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); Thread.sleep(90L * 1000L); scheduler.scheduleJob(job, trigger); } }
No comments:
Post a Comment