Java Reference
In-Depth Information
How It Works
Using Quartz Without Spring's Support
To use Quartz for scheduling, first create your job by implementing the
Job
interface. For example, the
following job executes the
replicate()
method of a file replicator, retrieved from the job data map
through the
JobExecutionContext
object that's passed in.
■
Note To use Quartz in your application, you must include
quartz-all-1.6.0.jar
(located in the
lib/quartz
directory of the Spring installation),
commons-collections.jar
(located in
lib/jakarta-commons
), and
jta.jar
(located in
lib/j2ee
) in your classpath.
package com.apress.springenterpriserecipes.replicator;
...
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class FileReplicationJob implements Job {
public void execute(JobExecutionContext context)
throws JobExecutionException {
Map dataMap = context.getJobDetail().getJobDataMap();
FileReplicator fileReplicator =
(FileReplicator) dataMap.get("fileReplicator");
try {
fileReplicator.replicate();
} catch (IOException e) {
throw new JobExecutionException(e);
}
}
}
After creating the job, you configure and schedule it with the Quartz API. For instance, the following
scheduler runs your file replication job every 60 seconds with a 5-second delay for the first time of
execution:
package com.apress.springenterpriserecipes.replicator;
...
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
Search WWH ::
Custom Search