KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > taskman > RetryTask


1 package org.sapia.taskman;
2
3 /**
4  * This class can conveniently be inherited from when a given task needs to run
5  * until its work is successfully done, or until a maximum amount of retries has
6  * been reached.
7  *
8  * @author Yanick Duchesne
9  * <dl>
10  * <dt><b>Copyright: </b>
11  * <dd>Copyright &#169; 2002-2004 <a
12  * HREF="http://www.sapia-oss.org">Sapia Open Source Software </a>. All
13  * Rights Reserved.</dd>
14  * </dt>
15  * <dt><b>License: </b>
16  * <dd>Read the license.txt file of the jar or visit the <a
17  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the
18  * Sapia OSS web site</dd>
19  * </dt>
20  * </dl>
21  */

22 public abstract class RetryTask implements Abortable, Task {
23   private boolean _aborted;
24   private int _count = 0;
25   private int _maxRetries;
26
27   /**
28    * @param maxRetries
29    * the maximum number of times that this class' <code>doExec</code>
30    * method should be called.
31    *
32    * @see #doExec(TaskContext)
33    */

34   protected RetryTask(int maxRetries) {
35     _maxRetries = maxRetries;
36   }
37
38   /**
39    * @see org.sapia.taskman.Task#exec(org.sapia.taskman.TaskContext)
40    */

41   public void exec(TaskContext ctx) {
42     doExec(ctx);
43     _count++;
44   }
45
46   /**
47    * @see org.sapia.taskman.Abortable#isAborted()
48    */

49   public boolean isAborted() {
50     return _aborted || (_count > _maxRetries);
51   }
52
53   public int getRetryCount() {
54     return _count;
55   }
56
57   public int getMaxRetries() {
58     return _maxRetries;
59   }
60
61   protected void abort() {
62     _aborted = true;
63   }
64
65   /**
66    * This template method is overridden by inheriting classes. It typically
67    * performs the logic pertaining to a specific task. Inheriting classes should
68    * call the <code>abort()</code> method of this class when trapping
69    * conditions that trigger task termination (typicall, this occurs when the
70    * task has completed prior to the max number of retries having been reached).
71    *
72    * @param ctx
73    */

74   protected abstract void doExec(TaskContext ctx);
75 }
76
Popular Tags