Monday, August 9, 2010

Java : Simple Quartz Job Scheduler

Quartz

Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may executed virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.

To know more click here

What Can Quartz Do For You?

If your application has tasks that need to occur at given moments in time, or if your system has recurring maintenance jobs then Quartz may be your ideal solution.

Sample uses of job scheduling with Quartz:

* Driving Process Workflow: As a new order is initially placed, schedule a Job to fire in exactly 2 hours, that will check the status of that order, and trigger a warning notification if an order confirmation message has not yet been received for the order, as well as changing the order's status to 'awaiting intervention'.
* System Maintenance: Schedule a job to dump the contents of a database into an XML file every business day (all weekdays except holidays) at 11:30 PM.
* Providing reminder services within an application.

Refrence : http://www.quartz-scheduler.org/


In our example we used Quartz 1.8.3

Download quartz-1.8.3.tar

Simple Example

In our example we will create a simple job class ie(MyJob.java) which will print a date time on console after every 1 minute.

Below is our Quartz class (QuartzExample.java), to run this example you need to set below files in class path :
quartz-1.8.3.jar
log4j-1.2.14.jar
slf4j-api-1.5.10.jar
slf4j-log4j12-1.5.10.jar

Download quartz-1.8.3.tar


//QuartzExample.java

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzExample {

    public static void main(String[] args) throws SchedulerException {
      
        SchedulerFactory schedFact = new StdSchedulerFactory();
      
        Scheduler scheduler = schedFact.getScheduler();
      
        JobDetail job = new JobDetail("job1",scheduler.DEFAULT_GROUP, quartz.MyJob.class);
      
        Trigger trigger = new SimpleTrigger("trigger1",scheduler.DEFAULT_GROUP,new Date(),null,SimpleTrigger.REPEAT_INDEFINITELY,60L*1000L);
      
        scheduler.start();
      
        scheduler.scheduleJob(job, trigger);
      
        //scheduler.shutdown();

    }

}


Lets look at  MyJob.java

//MyJob.java

import java.util.Date;

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

public class MyJob implements Job {
   
    //Required public no argument constructor
    public MyJob() {
       
    }

    public void execute(JobExecutionContext arg0) throws JobExecutionException {
       
        System.out.println("Now : "+new Date());

    }

}

Console Output


log4j:WARN No appenders could be found for logger (org.quartz.simpl.SimpleThreadPool).
log4j:WARN Please initialize the log4j system properly.
Now : Mon Aug 09 19:44:09 IST 2010
Now : Mon Aug 09 19:45:09 IST 2010
Now : Mon Aug 09 19:46:09 IST 2010
Now : Mon Aug 09 19:47:09 IST 2010
Now : Mon Aug 09 19:48:09 IST 2010
Now : Mon Aug 09 19:49:09 IST 2010
Now : Mon Aug 09 19:50:09 IST 2010


For quick and fast development use IDE like Eclipse.