javawaveblogs-20

Monday, April 14, 2008

Quartz Job Scheduler -- Part II (Example, Simple Trigger)

In this example we will see how to implement a Simple scheduler with the help of Quartz Framework.

Our application will just print Hello World on console after specified time.

For implementing the scheduler using quartz we need two classes.
1. which will implement org.quartz.Job interface, and the other
2. the scheduler class which will start the scheduler.

Now we will see the code which will implement Job interface:

package com.MyQuartz.simple;

import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
*
* @author dhanago
*/
public class HelloJob implements Job {

public void execute(JobExecutionContext jobExecutionContext)
throws JobExecutionException {
System.out.println("Hello World -- Executed on : " + new Date());
}
}

Here,
execute() --> is an overridden method. When ever Job interface is implemented its execute() of method should be overridden. Note that any component you want to schedule should implement Job interface.

JobExecutionContext --> is passed as an parameter to the execute() method. this provides the job instance which provides the job instance with information about its run-time environment. From this we will get the job detail information and also some important information regarding its triggers etc.,

Now we will see the code which will start the scheduler:

package com.MyQuartz.simple;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

/**
*
* @author dhanago
*/
public class StartScheduler {

public void startScheduler()
throws SchedulerException {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
JobDetail jobDetail = new JobDetail(
"MyJob", scheduler.DEFAULT_GROUP, HelloJob.class);
SimpleTrigger simpleTrigger = new SimpleTrigger(
"MyTrigger", scheduler.DEFAULT_GROUP, new Date(),
null, SimpleTrigger.REPEAT_INDEFINITELY, 60L * 1000L);
scheduler.scheduleJob(jobDetail, simpleTrigger);
}

public static void main(String args[]) {

StartScheduler startScheduler = new StartScheduler();
try {
startScheduler.startScheduler();
}
catch (SchedulerException ex) {
Logger.getLogger(StartScheduler.class.getName()).
log(Level.SEVERE, null, ex);
}
}
}

Here,

StdSchedulerFactory(): A Class StdSchedulerFactory is a class and it is implementation of SchedulerFactory interface. Here it just using for create an instance of SchedulerFactory instance.

Scheduler: Scheduler interface is the main interface (API) to this functionality. It provides some simple operations like scheduling jobs, unscheduling jobs, starting/stopping/pausing the scheduler.

start(): This method is used to starts the Scheduler's threads that fire Triggers. At the first time when we create the Scheduler it is in "stand-by" mode, and will not fire triggers. The scheduler can also be send back into stand-by mode by invoking the standby() method.

JobDetail(String name, String group, Class jobclass): The JobDetail object is created at the time the Job is added to scheduler. It contains various property settings like job name, group name and job class name. It can be used to store state information for a given instance of job class.

SimpleTrigger(String name, String group, Date startTime, Date endTime, int repeatCount, long repeatInterval): Trigger objects are used to firing the execution of jobs. When you want to schedule the job, instantiate the trigger and set the properties to provide the scheduling.

DEFAULT_GROUP: It is a constant, specified that Job and Trigger instances are belongs to which group..

REPEAT_INDEFINITELY: It is a constant used to indicate the 'repeat count' of the trigger is indefinite.

scheduleJob(JobDetail jobDetail, SimpleTrigger simpleTrigger): This method is used to add the JobDetail to the Scheduler, and associate the Trigger with it.

OutPut:


init:
deps-jar:
compile-single:
run-single:
log4j:WARN No appenders could be found for logger (org.quartz.simpl.SimpleThreadPool).
log4j:WARN Please initialize the log4j system properly.
Hello World -- Executed on : Mon Apr 14 22:52:25 IST 2008
Hello World -- Executed on : Mon Apr 14 22:53:25 IST 2008
Hello World -- Executed on : Mon Apr 14 22:54:25 IST 2008
Hello World -- Executed on : Mon Apr 14 22:55:25 IST 2008

1 comment:

Anonymous said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Servidor, I hope you enjoy. The address is http://servidor-brasil.blogspot.com. A hug.

diggthis