View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: SerialTask.java,v 1.1 2005/08/30 05:38:56 tanderson Exp $
44   */
45  package org.exolab.jms.scheduler;
46  
47  import org.apache.commons.logging.Log;
48  import org.apache.commons.logging.LogFactory;
49  
50  
51  /***
52   * A {@link Runnable} implementation which may only be executed serially by the
53   * {@link Scheduler}.
54   *
55   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
56   * @version $Revision: 1.1 $ $Date: 2005/08/30 05:38:56 $
57   */
58  public class SerialTask implements Runnable {
59  
60      /***
61       * The task to execute.
62       */
63      private Runnable _task;
64  
65      /***
66       * The scheduler.
67       */
68      private final Scheduler _scheduler;
69  
70      /***
71       * Determines if the task is currently active.
72       */
73      private boolean _active = false;
74  
75      /***
76       * Determines if the task should stop.
77       */
78      private boolean _stop = false;
79  
80      /***
81       * Determines if the task is scheduled to run.
82       */
83      private boolean _scheduled = false;
84  
85      /***
86       * Determines if the task needs to be rescheduled.
87       */
88      private boolean _reschedule = false;
89  
90      /***
91       * The logger.
92       */
93      private static final Log _log = LogFactory.getLog(SerialTask.class);
94  
95  
96      /***
97       * Construct a new <code>SerialTask</code>.
98       *
99       * @param task      the task to execute
100      * @param scheduler the scheduler
101      */
102     public SerialTask(Runnable task, Scheduler scheduler) {
103         _task = task;
104         _scheduler = scheduler;
105     }
106 
107     /***
108      * Schedule this to run.
109      * <p/>
110      * If the task is currently running, it will be scheduled after it has
111      * completed.
112      *
113      * @return <code>true</code> if the task was scheduled; <code>false</code>
114      *         if the task is already scheduled, or is in the process of
115      *         stopping
116      * @throws InterruptedException if the task can't be scheduled
117      */
118     public synchronized boolean schedule() throws InterruptedException {
119         if (_log.isDebugEnabled()) {
120             _log.debug("schedule() " + this);
121         }
122         boolean result = false;
123         if (!_stop && !_scheduled) {
124             if (_active) {
125                 _reschedule = true;
126             } else {
127                 _scheduled = true;
128                 try {
129                     _scheduler.execute(this);
130                 } catch (InterruptedException exception) {
131                     _scheduled = false;
132                     throw exception;
133                 }
134             }
135             result = true;
136         }
137         return result;
138     }
139 
140     /***
141      * Run the task.
142      */
143     public void run() {
144         synchronized (this) {
145             if (_log.isDebugEnabled()) {
146                 _log.debug("run() " + this);
147             }
148             if (_stop) {
149                 if (_log.isDebugEnabled()) {
150                     _log.debug("Scheduled task cancelled");
151                 }
152                 return;
153             }
154             if (_active) {
155                 if (_log.isDebugEnabled()) {
156                     _log.debug("Serial task already running, aborting");
157                 }
158                 throw new IllegalStateException("SerialTask already running");
159             }
160             _active = true;
161             _scheduled = false;
162         }
163         try {
164             _task.run();
165         } finally {
166             synchronized (this) {
167                 _active = false;
168                 if (_reschedule) {
169                     try {
170                         _reschedule = false;
171                         _scheduled = true;
172                         _scheduler.execute(this);
173                     } catch (InterruptedException exception) {
174                         _scheduled = false;
175                     }
176                 } else {
177                     _scheduled = false;
178                 }
179                 notify();
180             }
181         }
182     }
183 
184     /***
185      * Stop the task.
186      * <p/>
187      * This will wait for the task to complete before returning. If the task
188      * has been scheduled to run again, it will be cancelled.
189      */
190     public synchronized void stop() {
191         if (_log.isDebugEnabled()) {
192             _log.debug("stop() " + this);
193         }
194         _stop = true;
195         _reschedule = false;
196         _scheduled = false;
197         while (_active) {
198             try {
199                 wait();
200             } catch (InterruptedException ignore) {
201             }
202         }
203         _reschedule = false;
204         _stop = false;
205     }
206 
207     /***
208      * Returns a stringified form of this, for debugging purposes.
209      *
210      * @return a stringified form of this
211      */
212     public synchronized String toString() {
213         return "[stop=" + _stop + ", active=" + _active
214                 + ", reschedule=" + _reschedule + ", scheduled=" + _scheduled
215                 + "]";
216     }
217 
218 }