Saturday, September 11, 2010

Quartz Scheduling in Console Application


Prerequisites:

1) quartz-all-1.6.4.jar

2) commons-collections-3.1.jar

3) commons-codec-1.3.jar

4) commons-digester-1.8.jar

5) commons-logging-1.0.4.jar

6) commons-io.jar

7) jta.jar

8) commons-beanutils-1.7.0.jar

All the above mentioned jar files should be available inside the project build path>libraries.

Please note the instructions are related to eclipse europa 3.4 IDE.

Conceptual Touch:

•Quartz is an open source software provided by Open Symphony and which can be downloaded from http://www.opensymphony.com.

•It is a full-featured , open source job scheduling framework written entirely in Java. It can be integrated with J2SE & J2EE with smallest standalone to largest e-commerce system.

•Job Scheduler is a system that is responsible for executing other software components when a predetermined (scheduled) time arrives.

•It is distribured as a small java library (.jar file). Its important functionalities are like scheduling jobs, unscheduling jobs, starting/stopping/pausing the scheduler.

•If you want to schedule your software components then it must implement the Job Interface which overrides the execute() method.

•If you want components to notify when scheduled fire time arrives , then you should implement either TriggerListener / JobListener interface.

• Two important tasks need to be done beforehand. Firstly configure the Job class then secondly set up the schedule. When the scheduler determines the notifying time to your job, the quartz framework will call the execute() method on your Job class.

•Triggers are of two types i.e Simple trigger and Cron Trigger. Simple trigger is used for single execution of job at a given moment of time and have it repeat N times with a delay of T between executions. Cron trigger is used when you want triggers based on calendar like schedules such as Saturday, at noon or at 12.35 on the 15th day of every month.


Steps to do:


1) Create the instance of ScheduledFactory by getting the reference of org.quartz.impl.StdSchedulerFactory class.

2) Invoke the getScheduler() by this instance to instantiate the scheduler.

3) In execute() method , must implement the Job interface for software components scheduling.


Implementation of Simple Trigger:

HelloSchedule.java



package com.odisys.test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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 schedule = sf.getScheduler();
schedule.start();
//long ts = Long.parseLong("October 24, 2009, 11:03:00 IST");

JobDetail jd = new JobDetail("myjob", schedule.DEFAULT_GROUP,
HelloJob.class);
SimpleTrigger st = new SimpleTrigger("mytrigger", schedule.DEFAULT_GROUP,new Date(getCurrentDate()), null, 4, 60L * 1000L);
schedule.scheduleJob(jd, st);
}

public static long getCurrentDate() throws ParseException {
DateFormat dateFormat = new SimpleDateFormat();
Date date = new Date();
String currentDate = (dateFormat.format(date));
Date dt2 = new SimpleDateFormat().parse(currentDate);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(dt2);
long milliseconds1 = calendar2.getTimeInMillis();
return milliseconds1;
}

public static void main(String args[]) {
try {
new HelloSchedule();
} catch (Exception e) {
}
}
}



Explanation:

In the HelloSchedule.java, I have created an instance out of getScheduler() method. Then a job is defined based upon the HelloJob class. The final step is to supply the time interval parameters to the SimpleTrigger i.e 60L*1000L milliseconds. The getCurrentDate() method eventually checks for the appropriate time to come with regards to the interval defined. Once the interval of delay is achieved, it triggers a call to the HelloJob class where the execute() method is defined.

HelloJob.java




package com.odisys.test;

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());
//System.exit(0);
}
}





Explanation:

Upon call to the execute() method defined inside the HelloJob.class , it performs an output statement which can be verified in the console screen. The screenshot of the console screen is given below.







This session was just meant to give you the first taste of Scheduling Mechanism .In the upcoming sessions our prior motto will be to implement Cron Trigger in a web application which I am still working on.

Thank you for still holding on to your patience.

No comments:

Post a Comment