Java Reference
In-Depth Information
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();
trigger.setName("documentReplicationJob");
trigger.setStartTime(new Date(System.currentTimeMillis() + 5000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(60000);
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
In the
Main
class, you first configure the job details for your file replication job in a
JobDetail
object
and prepare job data in its
jobDataMap
property. Next, you create a
SimpleTrigger
object to configure the
scheduling properties. Finally, you create a scheduler to run your job using this trigger.
Quartz supports two types of triggers:
SimpleTrigger
and
CronTrigger
.
SimpleTrigger
allows you to
set trigger properties such as start time, end time, repeat interval, and repeat count.
CronTrigger
accepts
a Unix
cron
expression for you to specify the times to run your job. For example, you can replace the
preceding
SimpleTrigger
with the following
CronTrigger
to run your job at 17:30 every day:
CronTrigger trigger = new CronTrigger();
trigger.setName("documentReplicationJob");
trigger.setCronExpression("0 30 17 * * ?");
A cron expression is made up of seven fields (the last field is optional), separated by spaces.
Table 6-1 shows the field description for a cron expression.
Table 6-1.
Field Description for a Cron Expression
Position
Field Name
Range
1
Second
0-59
2
Minute
0-59
3
Hour
0-23
Search WWH ::
Custom Search