Spring and Quartz integration in spring (1)
----------
Use Quartz without Spring support
To use Quartz for scheduling, you must first implement the Job interface to create a task. To use Quartz in an application, you must include the quartz-all-xxx.jar, commons-collections.jar, and jta. jar in classpath. Example:
Java code
Package com. apress. springrecipes. 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 a task, use the Quartz API to configure and schedule it. For example, the following scheduling program runs a file copy task every 60 seconds, with a latency of 5 seconds for the first execution.
Java code
Package com. apress. springrecipes. replicator;
...
Import org. quartz. JobDetail;
Import org. quartz. Scheduler;
Import org. quartz. SimpleTrigger;
Import org. quartz. impl. StdSchedulerFactory;
....
Public class Main {
Public static void main (String [] args) throws Exception {
ApplicationContext context =
New ClassPathXmlApplicationContext ("beans. xml ");
FileReplicator documentReplicator = (FileReplicator) context.
GetBean ("documentReplicator ");
JobDetail job = new JobDetail ();
Job. setName ("documentReplicationJob ");
Job. setJobClass (FileReplicationJob. class );
Map dataMap = job. getJobDataMap ();
DataMap. put ("fileReplicator", documentReplicator );
SimpleTrigger trigger = new SimpleTrigger ();
Triger. setName ("documentReplicationJob ");
Trigger. setStartTime (new Date (System. currentTimeMillis () + 5000 ));
Trigger. setRepeatCount (SimpleTringger. REPEAT_INDEFINITELY );
Trigger. setRepeatInterval (60000 );
Scheduler schedfactory = new StdSchedulerFactory (). getScheduler ();
Scheduler. start ();
Schedgger. scheduleJob (job, trigger );
}
}
In the Main class, configure the task details for the file replication task in the JobDetail object, and prepare the task data in its jobDataMap attribute. Next, create a SimpleTrigger object to configure the scheduling attributes. Finally, create a scheduler to use the trigger to run the task.
Quartz supports two types of triggers: SimplerTrigger and CronTrigger. SimpleTrigger allows you to set trigger attributes, such as start time, end time, repetition interval, and number of repetitions. CronTrigger accepts Unix cron expressions and allows you to specify the number of times a task runs.
For example, you can use the following CronTrigger to replace the previous SimpleTrigger and run the task at every day.
Java code
CronTrigger trigger = new CronTrigger ();
Trigger. setName ("documentReplicationJob ");
Trigger. setCronExpression (0 30 17 **?);
Cron expressions are composed of seven fields (the last field is optional) and are separated by spaces.
Location domain name value range
1 Second 0 ~ 59
2 Minute 0 ~ 59
3 Hour 0 ~ 23
4 days of month 1 ~ 31
5 Month 1 ~ 12 or JAN-DEC
6 days of week 1 ~ 7. SUN-SAT
7 Year (optional) 1970 ~ 2099
Each part of the cron expression can be assigned a specific value (for example, 3) and a range (for example, 1 ~ 5), a list (such as 1, 3, 5), a wildcard (*: match all values), or a question mark (? : Used in the "Day of month" and "Day of week" domains to match one of these two domains and cannot match at the same time ).