KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example3 > CronTriggerExample


1 /*
2  * Copyright 2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 package org.quartz.examples.example3;
19
20 import java.util.Date JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.quartz.CronTrigger;
25 import org.quartz.JobDetail;
26 import org.quartz.Scheduler;
27 import org.quartz.SchedulerFactory;
28 import org.quartz.SchedulerMetaData;
29 import org.quartz.impl.StdSchedulerFactory;
30
31 /**
32  * This Example will demonstrate all of the basics of scheduling capabilities of
33  * Quartz using Cron Triggers.
34  *
35  * @author Bill Kratzer
36  */

37 public class CronTriggerExample {
38
39
40     public void run() throws Exception JavaDoc {
41         Log log = LogFactory.getLog(CronTriggerExample.class);
42
43         log.info("------- Initializing -------------------");
44
45         // First we must get a reference to a scheduler
46
SchedulerFactory sf = new StdSchedulerFactory();
47         Scheduler sched = sf.getScheduler();
48
49         log.info("------- Initialization Complete --------");
50
51         log.info("------- Scheduling Jobs ----------------");
52
53         // jobs can be scheduled before sched.start() has been called
54

55         // job 1 will run every 20 seconds
56
JobDetail job = new JobDetail("job1", "group1", SimpleJob.class);
57         CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1",
58                 "group1", "0/20 * * * * ?");
59         sched.addJob(job, true);
60         Date JavaDoc ft = sched.scheduleJob(trigger);
61         log.info(job.getFullName() + " has been scheduled to run at: " + ft
62                 + " and repeat based on expression: "
63                 + trigger.getCronExpression());
64
65         // job 2 will run every other minute (at 15 seconds past the minute)
66
job = new JobDetail("job2", "group1", SimpleJob.class);
67         trigger = new CronTrigger("trigger2", "group1", "job2", "group1",
68                 "15 0/2 * * * ?");
69         sched.addJob(job, true);
70         ft = sched.scheduleJob(trigger);
71         log.info(job.getFullName() + " has been scheduled to run at: " + ft
72                 + " and repeat based on expression: "
73                 + trigger.getCronExpression());
74
75         // job 3 will run every other minute but only between 8am and 5pm
76
job = new JobDetail("job3", "group1", SimpleJob.class);
77         trigger = new CronTrigger("trigger3", "group1", "job3", "group1",
78                 "0 0/2 8-17 * * ?");
79         sched.addJob(job, true);
80         ft = sched.scheduleJob(trigger);
81         log.info(job.getFullName() + " has been scheduled to run at: " + ft
82                 + " and repeat based on expression: "
83                 + trigger.getCronExpression());
84
85         // job 4 will run every three minutes but only between 5pm and 11pm
86
job = new JobDetail("job4", "group1", SimpleJob.class);
87         trigger = new CronTrigger("trigger4", "group1", "job4", "group1",
88                 "0 0/3 17-23 * * ?");
89         sched.addJob(job, true);
90         ft = sched.scheduleJob(trigger);
91         log.info(job.getFullName() + " has been scheduled to run at: " + ft
92                 + " and repeat based on expression: "
93                 + trigger.getCronExpression());
94
95         // job 5 will run at 10am on the 1st and 15th days of the month
96
job = new JobDetail("job5", "group1", SimpleJob.class);
97         trigger = new CronTrigger("trigger5", "group1", "job5", "group1",
98                 "0 0 10am 1,15 * ?");
99         sched.addJob(job, true);
100         ft = sched.scheduleJob(trigger);
101         log.info(job.getFullName() + " has been scheduled to run at: " + ft
102                 + " and repeat based on expression: "
103                 + trigger.getCronExpression());
104
105         // job 6 will run every 30 seconds but only on Weekdays (Monday through
106
// Friday)
107
job = new JobDetail("job6", "group1", SimpleJob.class);
108         trigger = new CronTrigger("trigger6", "group1", "job6", "group1",
109                 "0,30 * * ? * MON-FRI");
110         sched.addJob(job, true);
111         ft = sched.scheduleJob(trigger);
112         log.info(job.getFullName() + " has been scheduled to run at: " + ft
113                 + " and repeat based on expression: "
114                 + trigger.getCronExpression());
115
116         // job 7 will run every 30 seconds but only on Weekends (Saturday and
117
// Sunday)
118
job = new JobDetail("job7", "group1", SimpleJob.class);
119         trigger = new CronTrigger("trigger7", "group1", "job7", "group1",
120                 "0,30 * * ? * SAT,SUN");
121         sched.addJob(job, true);
122         ft = sched.scheduleJob(trigger);
123         log.info(job.getFullName() + " has been scheduled to run at: " + ft
124                 + " and repeat based on expression: "
125                 + trigger.getCronExpression());
126
127         log.info("------- Starting Scheduler ----------------");
128
129         // All of the jobs have been added to the scheduler, but none of the
130
// jobs
131
// will run until the scheduler has been started
132
sched.start();
133
134         log.info("------- Started Scheduler -----------------");
135
136         log.info("------- Waiting five minutes... ------------");
137         try {
138             // wait five minutes to show jobs
139
Thread.sleep(300L * 1000L);
140             // executing...
141
} catch (Exception JavaDoc e) {
142         }
143
144         log.info("------- Shutting Down ---------------------");
145
146         sched.shutdown(true);
147
148         log.info("------- Shutdown Complete -----------------");
149
150         SchedulerMetaData metaData = sched.getMetaData();
151         log.info("Executed " + metaData.numJobsExecuted() + " jobs.");
152
153     }
154
155     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
156
157         CronTriggerExample example = new CronTriggerExample();
158         example.run();
159     }
160
161 }
Popular Tags