Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Tutorials
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

It's a wonderful help to get on with quartz. kudos to roseindia

Posted by Mustaque on Friday, 03.9.07 @ 11:58am | #11227

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.