Quartz Scheduler Example Programs and Sample Code : The Quartz Example Programs : Example 1 - Your First Quartz Program
Example 1 - Your First Quartz Program
This example is designed to demonstrate how to get up and running with Quartz. This example will fire off a simple job that says "Hello World."
The program will perform the following actions:
*Start up the Quartz Scheduler
*Schedule a job to run at the next even minute
*Wait for 90 seconds to give Quartz a chance to run the job
*Shut down the scheduler
Running the Example
This example can be executed from the examples/example1 directory. There are two ways to run this example
*example1.sh - A UNIX/Linux shell script
*example1.bat - A Windows Batch file
The Code
The code for this example resides in the package org.quartz.examples.example1.
The code in this example is made up of the following classes:
Class Name
Description
SimpleExample
The main program.
HelloJob
A simple job that says Hello World.
HelloJob
HelloJob is a simple job that implements the Job interface and logs a message to the log (by default, this will simply go to the screen). The current date and time is printed in the job so that you can see exactly when the job was execcuted.
public void execute(JobExecutionContext context) throws JobExecutionException {
// Say Hello to the World and display the date/time
_log.info("Hello World! - " + new Date());
}
SimpleExample
The program starts by getting an instance of the Scheduler. This is done by creating a StdSchedulerFactory and then using it to create a scheduler. This will create a simple, RAM-based scheduler.
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
HelloJob is defined as a job to Quartz using the JobDetail class:
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();
We create a SimpleTrigger that will fire off at the next round minute:
// compute a time that is on the next round minute
Date runTime = evenMinuteDate(new Date());
// Trigger the job to run on the next round minute
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startAt(runTime)
.build();
We now will associate the job to the trigger in the scheduler:
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
At this point, the job is scheduled to run when its trigger fires. However, the scheduler is not yet running. So, we must tell the scheduler to start up.
sched.start();
To let the scheduler have an opportunity to run the job, our program sleeps for 90 seconds. The scheduler is running in the background and should fire off the job during those 90 seconds.
Thread.sleep(90L * 1000L);
Finally, the program gracefully shuts down the scheduler:
sched.shutdown(true);
Note:  
Passing "true" to the shutdown method tells Quartz Scheduler to wait until all jobs have completed running before returning from the method call.
Copyright © 2010-2015 Software AG, Darmstadt, Germany.

Product Logo |   Feedback