Sunday, April 28, 2013

Java :: Quartz

Steps to use Quartz 

1- Download Quartz
From http://quartz-scheduler.org/downloads/catalog and after free registration.
Extract the archive file and copy quartz-2.1.7.jar (or the name of the downloaded version) to your project libraries.

2- Download slf4j
From http://www.slf4j.org/download.html copy slf4j-api-1.7.5.jar (or the name of the downloaded version) to your project libraries.

3- Create Main Class and Implement Job Interface
Use the bellow example:


public static void main(String[] args) {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched;
try {
sched = sf.getScheduler();
JobDetail job = JobBuilder.newJob(Main.class)
.withIdentity("SimpleJob").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("SimpleJob")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5).repeatForever())
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);

} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

or you can download the source from:
https://github.com/firask86/QuartzExample

The example demonstrate simple and cron schedule.




No comments:

Post a Comment