KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.quartz.InterruptableJob;
25 import org.quartz.JobExecutionContext;
26 import org.quartz.JobExecutionException;
27 import org.quartz.UnableToInterruptJobException;
28
29
30 /**
31  * <p>
32  * A dumb implementation of an InterruptableJob, for unittesting purposes.
33  * </p>
34  *
35  * @author <a HREF="mailto:bonhamcm@thirdeyeconsulting.com">Chris Bonham</a>
36  * @author Bill Kratzer
37  */

38 public class DumbInterruptableJob implements InterruptableJob {
39     
40     // logging services
41
private static Log _log = LogFactory.getLog(DumbInterruptableJob.class);
42     
43     // has the job been interrupted?
44
private boolean _interrupted = false;
45
46     // job name
47
private String JavaDoc _jobName = "";
48     
49     /**
50      * <p>
51      * Empty constructor for job initilization
52      * </p>
53      */

54     public DumbInterruptableJob() {
55     }
56
57
58     /**
59      * <p>
60      * Called by the <code>{@link org.quartz.Scheduler}</code> when a <code>{@link org.quartz.Trigger}</code>
61      * fires that is associated with the <code>Job</code>.
62      * </p>
63      *
64      * @throws JobExecutionException
65      * if there is an exception while executing the job.
66      */

67     public void execute(JobExecutionContext context)
68         throws JobExecutionException {
69
70         _jobName = context.getJobDetail().getFullName();
71         _log.info("---- " + _jobName + " executing at " + new Date JavaDoc());
72
73         try {
74             // main job loop... see the JavaDOC for InterruptableJob for discussion...
75
// do some work... in this example we are 'simulating' work by sleeping... :)
76

77             for (int i = 0; i < 4; i++) {
78                 try {
79                     Thread.sleep(1000L);
80                 } catch (Exception JavaDoc ignore) {
81                     ignore.printStackTrace();
82                 }
83                 
84                 // periodically check if we've been interrupted...
85
if(_interrupted) {
86                     _log.info("--- " + _jobName + " -- Interrupted... bailing out!");
87                     return; // could also choose to throw a JobExecutionException
88
// if that made for sense based on the particular
89
// job's responsibilities/behaviors
90
}
91             }
92             
93         } finally {
94             _log.info("---- " + _jobName + " completed at " + new Date JavaDoc());
95         }
96     }
97     
98     /**
99      * <p>
100      * Called by the <code>{@link Scheduler}</code> when a user
101      * interrupts the <code>Job</code>.
102      * </p>
103      *
104      * @return void (nothing) if job interrupt is successful.
105      * @throws JobExecutionException
106      * if there is an exception while interrupting the job.
107      */

108     public void interrupt() throws UnableToInterruptJobException {
109         _log.info("---" + " -- INTERRUPTING --");
110         _interrupted = true;
111     }
112
113 }
Popular Tags