KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example7 > InterruptExample


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.example7;
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 InterruptExample {
57
58     public void run() throws Exception JavaDoc {
59         final Log log = LogFactory.getLog(InterruptExample.class);
60
61         log.info("------- Initializing ----------------------");
62
63         // First we must get a reference to a scheduler
64
SchedulerFactory sf = new StdSchedulerFactory();
65         Scheduler sched = sf.getScheduler();
66
67         log.info("------- Initialization Complete -----------");
68
69         log.info("------- Scheduling Jobs -------------------");
70
71         // get a "nice round" time a few seconds in the future...
72
long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime();
73
74         JobDetail job = new JobDetail("interruptableJob1", "group1",
75                 DumbInterruptableJob.class);
76         SimpleTrigger trigger =
77             new SimpleTrigger("trigger1", "group1",
78                     new Date JavaDoc(ts),
79                     null,
80                     SimpleTrigger.REPEAT_INDEFINITELY,
81                     5000L);
82         Date JavaDoc ft = sched.scheduleJob(job, trigger);
83         log.info(job.getFullName() + " will run at: " + ft + " and repeat: "
84                 + trigger.getRepeatCount() + " times, every "
85                 + trigger.getRepeatInterval() / 1000 + " seconds");
86
87         // start up the scheduler (jobs do not start to fire until
88
// the scheduler has been started)
89
sched.start();
90         log.info("------- Started Scheduler -----------------");
91         
92
93         log.info("------- Starting loop to interrupt job every 7 seconds ----------");
94         for(int i=0; i < 50; i++) {
95             try {
96                 Thread.sleep(7000L);
97                 // tell the scheduler to interrupt our job
98
sched.interrupt(job.getName(), job.getGroup());
99             } catch (Exception JavaDoc e) {
100             }
101         }
102         
103         log.info("------- Shutting Down ---------------------");
104
105         sched.shutdown(true);
106
107         log.info("------- Shutdown Complete -----------------");
108         SchedulerMetaData metaData = sched.getMetaData();
109         log.info("Executed " + metaData.numJobsExecuted() + " jobs.");
110
111     }
112
113     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
114
115         InterruptExample example = new InterruptExample();
116         example.run();
117     }
118
119 }
Popular Tags