KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example5 > MisfireExample


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.example5;
19
20 import java.util.Date JavaDoc;
21
22 import org.quartz.JobDetail;
23 import org.quartz.Scheduler;
24 import org.quartz.SchedulerFactory;
25 import org.quartz.SchedulerMetaData;
26 import org.quartz.SimpleTrigger;
27 import org.quartz.TriggerUtils;
28 import org.quartz.impl.StdSchedulerFactory;
29
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.commons.logging.Log;
32
33 /**
34  * Demonstrates the behavior of <code>StatefulJob</code>s, as well as how
35  * misfire instructions affect the firings of triggers of <code>StatefulJob</code>
36  * s - when the jobs take longer to execute that the frequency of the trigger's
37  * repitition.
38  *
39  * <p>
40  * While the example is running, you should note that there are two triggers
41  * with identical schedules, firing identical jobs. The triggers "want" to fire
42  * every 3 seconds, but the jobs take 10 seconds to execute. Therefore, by the
43  * time the jobs complete their execution, the triggers have already "misfired"
44  * (unless the scheduler's "misfire threshold" has been set to more than 7
45  * seconds). You should see that one of the jobs has its misfire instruction
46  * set to <code>SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT</code>,
47  * which causes it to fire immediately, when the misfire is detected. The other
48  * trigger uses the default "smart policy" misfire instruction, which causes
49  * the trigger to advance to its next fire time (skipping those that it has
50  * missed) - so that it does not refire immediately, but rather at the next
51  * scheduled time.
52  * </p>
53  *
54  * @author <a HREF="mailto:bonhamcm@thirdeyeconsulting.com">Chris Bonham</a>
55  */

56 public class MisfireExample {
57
58     
59     public void run() throws Exception JavaDoc {
60         Log log = LogFactory.getLog(MisfireExample.class);
61
62         log.info("------- Initializing -------------------");
63
64         // First we must get a reference to a scheduler
65
SchedulerFactory sf = new StdSchedulerFactory();
66         Scheduler sched = sf.getScheduler();
67
68         log.info("------- Initialization Complete -----------");
69
70         log.info("------- Scheduling Jobs -----------");
71
72         // jobs can be scheduled before start() has been called
73

74         // get a "nice round" time a few seconds in the future...
75
long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime();
76
77         // statefulJob1 will run every three seconds
78
// (but it will delay for ten seconds)
79
JobDetail job = new JobDetail("statefulJob1", "group1",
80                 StatefulDumbJob.class);
81         job.getJobDataMap().put(MisfireJob.EXECUTION_DELAY, 10000L);
82         SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1",
83                 new Date JavaDoc(ts), null,
84                 SimpleTrigger.REPEAT_INDEFINITELY, 3000L);
85         Date JavaDoc ft = sched.scheduleJob(job, trigger);
86         log.info(job.getFullName() +
87                 " will run at: " + ft +
88                 " and repeat: " + trigger.getRepeatCount() +
89                 " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
90
91         // statefulJob2 will run every three seconds
92
// (but it will delay for ten seconds)
93
job = new JobDetail("statefulJob2", "group1", StatefulDumbJob.class);
94         job.getJobDataMap().put(MisfireJob.EXECUTION_DELAY, 10000L);
95         trigger = new SimpleTrigger("trigger2", "group1",
96                 new Date JavaDoc(ts), null,
97                 SimpleTrigger.REPEAT_INDEFINITELY, 3000L);
98         trigger
99             .setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT);
100         ft = sched.scheduleJob(job, trigger);
101         log.info(job.getFullName() +
102                 " will run at: " + ft +
103                 " and repeat: " + trigger.getRepeatCount() +
104                 " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
105
106         log.info("------- Starting Scheduler ----------------");
107
108         // jobs don't start firing until start() has been called...
109
sched.start();
110
111         log.info("------- Started Scheduler -----------------");
112         
113         try {
114             // sleep for ten minutes for triggers to file....
115
Thread.sleep(600L * 1000L);
116         } catch (Exception JavaDoc e) {
117         }
118
119         log.info("------- Shutting Down ---------------------");
120
121         sched.shutdown(true);
122
123         log.info("------- Shutdown Complete -----------------");
124
125         SchedulerMetaData metaData = sched.getMetaData();
126         log.info("Executed " + metaData.numJobsExecuted() + " jobs.");
127     }
128
129
130
131     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
132
133         MisfireExample example = new MisfireExample();
134         example.run();
135     }
136
137 }
Popular Tags