Hello World Quartz Scheduler

In this section we are going to develop a simple Quartz
Scheduler application with the help of Quartz framework. That will
display "Hello World Quartz Scheduler :<date & time>"
on the console window after specified time schedule.
Before using Scheduler you have to instantiate it. For
doing this some users may keep an instance of a factory serialized in a JNDI
store, and some other users may find it just to instantiate and use a factory
instance. Firstly create the instance of SchedulerFactory by getting the reference
of org.Quartz.impl.StdSchedulerFactory Class. Invoke the getScheduler()
method by this instance to instantiate the Scheduler.
Description of code:
execute(): Any software components you want to
schedule then you must implement the Job interface and override it execute()
method.
JobExecutionContext: The JobExecutionContext object that is passed to
execute() method provides the job instance with information about its "run-time" environment - a handle to the Scheduler that executed it, a handle to the Trigger that triggered the execution, the job's JobDetail object, and a few other items.
SchedulerFactory: SchedulerFactory is a
interface provides a mechanism for obtaining client-usable handles to Scheduler instances.
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.
getScheduler(): SchedulerFactoy interface
having the getScheduler() method that returns an instance of 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 jd, SimpleTrigger st):
This method is used to add the JobDetail to the Scheduler, and associate the
Trigger with it.
Here is the code of Job Class:
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
public class HelloJob implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException{
System.out.println("Hello World Quartz Scheduler: " + new Date());
}
}
|
Download this code
Here is the code of Scheduler Class:
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class HelloSchedule {
public HelloSchedule()throws Exception{
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler sched=sf.getScheduler();
sched.start();
JobDetail jd=new JobDetail("myjob",sched.DEFAULT_GROUP,HelloJob.class);
SimpleTrigger st=new SimpleTrigger("mytrigger",sched.DEFAULT_GROUP,new Date(),
null,SimpleTrigger.REPEAT_INDEFINITELY,60L*1000L);
sched.scheduleJob(jd, st);
}
public static void main(String args[]){
try{
new HelloSchedule();
}catch(Exception e){}
}
}
|
Download this code
Output of the program :
|
log4j:WARN No appenders could be found for logger (org.quartz.simpl.SimpleThreadPool).
log4j:WARN Please initialize the log4j system properly.
Hello World Quartz Scheduler: Fri Feb 16 16:15:10
GMT+05:30 2007
Hello World Quartz Scheduler: Fri Feb 16 16:16:10
GMT+05:30 2007
Hello World Quartz Scheduler: Fri Feb 16 16:17:10
GMT+05:30 2007
Hello World Quartz Scheduler: Fri Feb 16 16:18:10
GMT+05:30 2007
Hello World Quartz Scheduler: Fri Feb 16 16:19:10
GMT+05:30 2007
Hello World Quartz Scheduler: Fri Feb 16 16:20:10
GMT+05:30 2007 |

|