Magazine
 
QUARTZ
 

CronTrigger:

The CronTrigger is based on Calendar-like schedules.
The CronTrigger is based on the UNIX cron expression.
Let’s take you need to execute a job every day at 12:15 a.m., except on Saturdays,Sundays and Mondays then a CronTrigger will work out for you.

The following Quartz cron expression would execute a Job at
12:15 a.m. every day, Tuesday through Friday.
0 15 12 ? * TUE-FRI and this expression 0 15 12 ? *
6L 005- 2008 fires at 10:15 a.m. on the last Friday of every month during the years 2005, 2006, 2007, and 2008.
This is not achievable with the SimpleTrigger. Either can
be used for your Jobs.

Job Stores

Quartz allows two types of JobStores : memory-based
and a database-based.
The memory-based job store is suitable for a small number of concurrent jobs—information about these jobs is stored in the RAM of the computer. This is the fastest way to access jobs in the Scheduler’s queue. However, the flip side is that all these jobs are lost when the machine is restarted—along with their information.

For jobs that need some persistence information, a JDBCbased store is the best solution. Information about these jobs is stored in a database and is accessed by the Scheduler through JDBC.

Hands on Quartz Framework

Installation and Configuration

Quartz is a Java API and thus, can be used in JSE
and JEE projects directly as an application module. To
begin, just include the required .jar files into the project’s
classpath. All the required .jar files come with the Quartz
distribution download (check the lib folder of the distribution).

The only minimum .jar files required to run Quartz
are Quartz.jar and commons-logging.jar. The other files
included in the download are supporting libraries supporting additional functions.

Quartz is highly configurable: it allows you to modify the properties of the Scheduler using properties file. The Scheduler first looks for a quartz.properties file in the current directory or any folder in the classpath. If it finds one, it starts up with the properties specified in the properties file. Otherwise, it uses the default quartz.properties file. The quartz.properties file allows you to configure which default

 

Scheduler will be used, the number of threads the Scheduler can start, properties about jobs, properties about triggers, information about JobStores, the priority of the threads being instantiated, etc.

Integration with Other Software

Since Quartz is a Java based API, it can be integrated with
any JEE-based API—like JavaMail, JMS, JDBC, etc. It can
also be integrated with any third-party API. Quartz can also be embedded into an application server by including the Quartz .jar files into the server’s classpath.

Now let’s develop a simplest program to illustrate the working of the Quartz Framework:

Hello World Quartz Application

In this section, we will develop a simple Quartz Scheduler
(“Hello World”) that will idisplay “Hello World Quartz Scheduler : <date & time>” at the console, after a specific time interval.

To develop a quartz application we need two classes:

1 A Quartz job class implementing the Job interface.
Here define all types of jobs needed for scheduling.
2 A scheduler class where we manage the jobs defined
in previous class. The scheduler schedules the jobs and
executes them after specified time duration.

Description

To use Quartz, the first thing you need to do is to create a Quartz job that defines a task to schedule.

Create a Quartz job by defining a Java class and implementing the job interface. Override the execute() method of the job Interface and add logic to be executed when the job runs.

Next, define a trigger parameter to determine when the job
needs to be run and how often. Then, register the Trigger and the job with the scheduler to ensure that the Scheduler can trigger the job at the appropriate time. The Scheduler does not allow you to register a job directly—this needs to be a done through a JobDetail class, which allows you to categorize and group similar jobs. The JobDetail class also allows you to add a job and define properties like the job’s name, the job’s Job Group, and the job’s Java class. If no Job group is specified, the Scheduler puts the job into a default Job group.

Before using Scheduler you have to instantiate it. Some users may keep an instance of a factory serialized in a JNDI store while others can instantiate it and use a factory instance.

August 2007 | Java Jazz Up | 11
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40        Download PDF