Monday, August 6, 2012

Quartz Tutorial

Quartz is an open source job scheduling framework that provides simple but powerful mechanisms for job scheduling in Java applications. Quartz allows developers to schedule jobs by time interval or by time of day. It implements many-to-many relationships for jobs and triggers and can associate multiple jobs with different triggers.

Here I am providing a simple guide to Quartz API with the help of fallowing example

Step 1: Create a Simple java Application in eclipse.

Step 2: Add the fallowing jars to the classpath
quartz-1.6.0.jar
commons-collections-3.2.jar
commons-logging-1.1.1.jar
jta-1.1.jar


Step 3:Create your job which implements Job interface
package com.test;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MyJob implements Job {

@Override
public void execute(JobExecutionContext ctx) throws JobExecutionException {
System.out.println("Quartz is executing my Job");
}

}

Step 4: Create Your Schedular and test application
package com.test;

import java.text.ParseException;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class JobSchedular {
public static void main(String[] args) throws ParseException, SchedulerException {
JobDetail job = new JobDetail();
job.setName("MyJobName");
job.setJobClass(MyJob.class);

CronTrigger crt = new CronTrigger();
crt.setName("MyTriggerName");
crt.setCronExpression("0/30 * * * * ");

Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, crt);
}

}

Step 5: Here is the outcome








No comments: