Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Quartz Hello World Quartz Scheduler

Related Tutorials


 
 

Share on Google+Share on Google+

Hello World Quartz Scheduler

Advertisement
In this section we are going to develop a simple Quartz Scheduler application with the help of Quartz framework.

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 arg0throws 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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 13, 2007

Related Tutorials

Discuss: Hello World Quartz Scheduler   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:9
dhruva
April 1, 2011
not working

JobDetails is interface in quartz 2.0 and hence this code not working for me
Roshan Randive
June 15, 2011
Use of Quartz in hibernate

How Quartz Scheduler is used in hibernate like event scheduler in MySql
mthang86
July 29, 2011
impossible create instance!

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); Plz.check it before post.
avadhesh baghel
September 17, 2011
Your Comment

Your Comment
Ashish
November 11, 2011
facing the logger problem.

//log4j:WARN No appenders could be found for logger(org.quartz.simpl.SimpleThreadPool). //log4j:WARN Please initialize the log4j system properly Have you found a solution for this yet? Also i need to view the internal logs of Quartz. is there any way.
sravan
March 7, 2012
Quartz scheduler

who are using the Quartz scheduler program, you need to set up the commons-logging jar as well.
KISHAN OMAR
April 3, 2012
job details class could not be instantiated

pls help
hiran
September 27, 2012
Quartz Hello World

Hi, I have tried this example and it gave me errors saying that JobDetail, SimpleTrigger "Cannot instantiate the type" 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); Please give me a solution
Ridzuan
May 22, 2014
What with the log4j error? how to solve that one?

What with the log4j error? how to solve that one?
DMCA.com