KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example13 > ClusterExample


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.example13;
19
20 import java.util.Date JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.quartz.JobDetail;
26 import org.quartz.Scheduler;
27 import org.quartz.SchedulerFactory;
28 import org.quartz.SimpleTrigger;
29 import org.quartz.impl.StdSchedulerFactory;
30
31 /**
32  * Used to test/show the clustering features of JDBCJobStore (JobStoreTX or
33  * JobStoreCMT).
34  *
35  * <p>
36  * All instances MUST use a different properties file, because their instance
37  * Ids must be different, however all other properties should be the same.
38  * </p>
39  *
40  * <p>
41  * If you want it to clear out existing jobs & triggers, pass a command-line
42  * argument called "clearJobs".
43  * </p>
44  *
45  * <p>
46  * You should probably start with a "fresh" set of tables (assuming you may
47  * have some data lingering in it from other tests), since mixing data from a
48  * non-clustered setup with a clustered one can be bad.
49  * </p>
50  *
51  * <p>
52  * Try killing one of the cluster instances while they are running, and see
53  * that the remaining instance(s) recover the in-progress jobs. Note that
54  * detection of the failure may take up to 15 or so seconds with the default
55  * settings.
56  * </p>
57  *
58  * <p>
59  * Also try running it with/without the shutdown-hook plugin registered with
60  * the scheduler. (org.quartz.plugins.management.ShutdownHookPlugin).
61  * </p>
62  *
63  * <p>
64  * <i>Note:</i> Never run clustering on separate machines, unless their
65  * clocks are synchronized using some form of time-sync service (daemon).
66  * </p>
67  *
68  * @see DumbRecoveryJob
69  *
70  * @author James House
71  */

72 public class ClusterExample {
73
74     private static Log _log = LogFactory.getLog(ClusterExample.class);
75     
76     public void cleanUp(Scheduler inScheduler) throws Exception JavaDoc {
77         _log.warn("***** Deleting existing jobs/triggers *****");
78
79         // unschedule jobs
80
String JavaDoc[] groups = inScheduler.getTriggerGroupNames();
81         for (int i = 0; i < groups.length; i++) {
82             String JavaDoc[] names = inScheduler.getTriggerNames(groups[i]);
83             for (int j = 0; j < names.length; j++) {
84                 inScheduler.unscheduleJob(names[j], groups[i]);
85             }
86         }
87
88         // delete jobs
89
groups = inScheduler.getJobGroupNames();
90         for (int i = 0; i < groups.length; i++) {
91             String JavaDoc[] names = inScheduler.getJobNames(groups[i]);
92             for (int j = 0; j < names.length; j++) {
93                 inScheduler.deleteJob(names[j], groups[i]);
94             }
95         }
96     }
97     
98     public void run(boolean inClearJobs, boolean inScheduleJobs)
99         throws Exception JavaDoc {
100
101         // First we must get a reference to a scheduler
102
SchedulerFactory sf = new StdSchedulerFactory();
103         Scheduler sched = sf.getScheduler();
104         
105         if (inClearJobs) {
106             cleanUp(sched);
107         }
108
109         _log.info("------- Initialization Complete -----------");
110
111         if (inScheduleJobs) {
112
113             _log.info("------- Scheduling Jobs ------------------");
114
115             String JavaDoc schedId = sched.getSchedulerInstanceId();
116
117             int count = 1;
118
119             JobDetail job = new JobDetail("job_" + count, schedId,
120                     SimpleRecoveryJob.class);
121             // ask scheduler to re-execute this job if it was in progress when
122
// the scheduler went down...
123
job.setRequestsRecovery(true);
124             SimpleTrigger trigger =
125                 new SimpleTrigger("triger_" + count, schedId, 20, 5000L);
126             trigger.setStartTime(new Date JavaDoc(System.currentTimeMillis() + 1000L));
127             _log.info(job.getFullName() +
128                     " will run at: " + trigger.getNextFireTime() +
129                     " and repeat: " + trigger.getRepeatCount() +
130                     " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
131             sched.scheduleJob(job, trigger);
132
133             count++;
134             job = new JobDetail("job_" + count, schedId,
135                     SimpleRecoveryJob.class);
136             // ask scheduler to re-execute this job if it was in progress when
137
// the scheduler went down...
138
job.setRequestsRecovery(true);
139             trigger = new SimpleTrigger("trig_" + count, schedId, 20, 5000L);
140             trigger.setStartTime(new Date JavaDoc(System.currentTimeMillis() + 2000L));
141             _log.info(job.getFullName() +
142                     " will run at: " + trigger.getNextFireTime() +
143                     " and repeat: " + trigger.getRepeatCount() +
144                     " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
145             sched.scheduleJob(job, trigger);
146
147             count++;
148             job = new JobDetail("job_" + count, schedId,
149                     SimpleRecoveryStatefulJob.class);
150             // ask scheduler to re-execute this job if it was in progress when
151
// the scheduler went down...
152
job.setRequestsRecovery(true);
153             trigger = new SimpleTrigger("trig_" + count, schedId, 20, 3000L);
154             trigger.setStartTime(new Date JavaDoc(System.currentTimeMillis() + 1000L));
155             _log.info(job.getFullName() +
156                     " will run at: " + trigger.getNextFireTime() +
157                     " and repeat: " + trigger.getRepeatCount() +
158                     " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
159             sched.scheduleJob(job, trigger);
160
161             count++;
162             job = new JobDetail("job_" + count, schedId, SimpleRecoveryJob.class);
163             // ask scheduler to re-execute this job if it was in progress when
164
// the scheduler went down...
165
job.setRequestsRecovery(true);
166             trigger = new SimpleTrigger("trig_" + count, schedId, 20, 4000L);
167             trigger.setStartTime(new Date JavaDoc(System.currentTimeMillis() + 1000L));
168             _log.info(job.getFullName() + " will run at: "
169                     + trigger.getNextFireTime() + " & repeat: "
170                     + trigger.getRepeatCount() + "/"
171                     + trigger.getRepeatInterval());
172             sched.scheduleJob(job, trigger);
173
174             count++;
175             job = new JobDetail("job_" + count, schedId, SimpleRecoveryJob.class);
176             // ask scheduler to re-execute this job if it was in progress when
177
// the scheduler went down...
178
job.setRequestsRecovery(true);
179             trigger = new SimpleTrigger("trig_" + count, schedId, 20, 4500L);
180             trigger.setStartTime(new Date JavaDoc(System.currentTimeMillis() + 1000L));
181             _log.info(job.getFullName() + " will run at: "
182                     + trigger.getNextFireTime() + " & repeat: "
183                     + trigger.getRepeatCount() + "/"
184                     + trigger.getRepeatInterval());
185             sched.scheduleJob(job, trigger);
186         }
187
188         // jobs don't start firing until start() has been called...
189
_log.info("------- Starting Scheduler ---------------");
190         sched.start();
191         _log.info("------- Started Scheduler ----------------");
192
193         _log.info("------- Waiting for one hour... ----------");
194         try {
195             Thread.sleep(3600L * 1000L);
196         } catch (Exception JavaDoc e) {
197         }
198
199         _log.info("------- Shutting Down --------------------");
200         sched.shutdown();
201         _log.info("------- Shutdown Complete ----------------");
202     }
203
204     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
205         boolean clearJobs = false;
206         boolean scheduleJobs = true;
207
208         for (int i = 0; i < args.length; i++) {
209             if (args[i].equalsIgnoreCase("clearJobs")) {
210                 clearJobs = true;
211             } else if (args[i].equalsIgnoreCase("dontScheduleJobs")) {
212                 scheduleJobs = false;
213             }
214         }
215
216         ClusterExample example = new ClusterExample();
217         example.run(clearJobs, scheduleJobs);
218     }
219 }
220
221
Popular Tags