KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > InterruptableJob


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

18
19 /*
20  * Previously Copyright (c) 2001-2004 James House
21  */

22 package org.quartz;
23
24 /**
25  * <p>
26  * The interface to be implemented by <code>{@link Job}s</code> that provide a
27  * mechanism for having their execution interrupted. It is NOT a requirment
28  * for jobs to implement this interface - in fact, for most people, none of
29  * their jobs will.
30  * </p>
31  *
32  * <p>
33  * The means of actually interrupting the Job must be implemented within the
34  * <code>Job</code> itself (the <code>interrupt()</code> method of this
35  * interface is simply a means for the scheduler to inform the <code>Job</code>
36  * that a request has been made for it to be interrupted). The mechanism that
37  * your jobs use to interrupt themselves might vary between implementations.
38  * However the principle idea in any implementation should be to have the
39  * body of the job's <code>execute(..)</code> periodically check some flag to
40  * see if an interruption has been requested, and if the flag is set, somehow
41  * abort the performance of the rest of the job's work. An example of
42  * interrupting a job can be found in the java source for the class
43  * <code>org.quartz.examples.DumbInterruptableJob</code>. It is legal to use
44  * some combination of <code>wait()</code> and <code>notify()</code>
45  * synchronization within <code>interrupt()</code> and <code>execute(..)</code>
46  * in order to have the <code>interrupt()</code> method block until the
47  * <code>execute(..)</code> signals that it has noticed the set flag.
48  * </p>
49  *
50  * <p>
51  * If the Job performs some form of blocking I/O or similar functions, you may
52  * want to consider having the <code>Job.execute(..)</code> method store a
53  * reference to the calling <code>Thread</code> as a member variable. Then the
54  * impplementation of this interfaces <code>interrupt()</code> method can call
55  * <code>interrupt()</code> on that Thread. Before attempting this, make
56  * sure that you fully understand what <code>java.lang.Thread.interrupt()</code>
57  * does and doesn't do. Also make sure that you clear the Job's member
58  * reference to the Thread when the execute(..) method exits (preferrably in a
59  * <code>finally</code> block.
60  * </p>
61  *
62  * <p>
63  * See Example 7 (org.quartz.examples.example7.DumbInterruptableJob) for a simple
64  * implementation demonstration.
65  * </p>
66  * @see Job
67  * @see StatefulJob
68  * @see Scheduler#interrupt(String, String)
69  *
70  * @author James House
71  */

72 public interface InterruptableJob extends Job {
73
74     /*
75      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76      *
77      * Interface.
78      *
79      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80      */

81
82     /**
83      * <p>
84      * Called by the <code>{@link Scheduler}</code> when a user
85      * interrupts the <code>Job</code>.
86      * </p>
87      *
88      * @throws UnableToInterruptJobException
89      * if there is an exception while interrupting the job.
90      */

91     void interrupt()
92         throws UnableToInterruptJobException;
93 }
94
Popular Tags