KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > schedules > schedules > CountSchedule


1 package org.oddjob.schedules.schedules;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import org.apache.log4j.Logger;
7 import org.oddjob.schedules.AbstractSchedule;
8 import org.oddjob.schedules.Interval;
9 import org.oddjob.schedules.Schedule;
10 import org.oddjob.schedules.ScheduleContext;
11
12 /**
13  * @oddjob.description This schedule returns up to count
14  * number of child schedules. It is typically
15  * used to count a number intervals for re-trying something, But could be
16  * used to count
17  * the first n days of the month for instance.
18  * <p>
19  * The count will be reset if the end interval changes, i.e. when counting
20  * n days of the month,
21  * and the month moves on. However in the retry situation
22  * to reset the count schedule it would
23  * need to be reset.
24  * <p>
25  * If the nested schedule isn't specified it defaults to
26  * {@link org.oddjob.schedules.schedules.NowSchedule}
27  *
28  * @oddjob.example
29  *
30  * This would shcedule a job 5 times at intervals of 15 minutes.
31  *
32  * <pre>
33  * &lt;count count="5"&gt;
34  * &lt;interval interval="00:15"/&gt;
35  * &lt;/count&gt;
36  * </pre>
37  *
38  * @author Rob Gordon
39  */

40
41 final public class CountSchedule extends AbstractSchedule
42 implements Serializable JavaDoc {
43     private static final long serialVersionUID = 20050226;
44     
45     private static final Logger logger = Logger.getLogger(CountSchedule.class);
46     
47     private static final String JavaDoc COUNT_KEY = "countschedulecount";
48     private static final String JavaDoc LAST_KEY = "countschedulelast";
49     
50     /**
51      * @oddjob.property count
52      * @oddjob.description The number to count to.
53      * @oddjob.required Yes.
54      */

55     private int countTo;
56     
57     /**
58      * Set the number to count to.
59      *
60      * @param count The number to count to.
61      */

62     public void setCount(String JavaDoc count) {
63         this.countTo = Integer.parseInt(count);
64     }
65
66     /**
67      * Get the number to count to.
68      *
69      * @return The number to count to.
70      */

71     public String JavaDoc getCount() {
72         return Integer.toString(countTo);
73     }
74
75     class ImmediateSchedule implements Schedule, Serializable JavaDoc {
76         private static final long serialVersionUID = 20060113;
77         public Interval nextDue(ScheduleContext context) {
78             Date JavaDoc date = context.getDate();
79             return new Interval(date, date);
80         }
81         public void setLimits(Interval limits) {
82         }
83     }
84     
85     /*
86      * (non-Javadoc)
87      * @see org.treesched.Schedule#nextDue(java.util.Date)
88      */

89     public Interval nextDue(ScheduleContext context) {
90         Date JavaDoc now = context.getDate();
91         if (now == null) {
92             return null;
93         }
94         if (getChildSchedule() == null) {
95             setChildSchedule(new ImmediateSchedule());
96         }
97
98         int counted = 0;
99         Integer JavaDoc storedCount = (Integer JavaDoc) context.getData(COUNT_KEY);
100         if (storedCount != null) {
101             counted = storedCount.intValue();
102         }
103         
104         logger.debug(this + ": in date is " + now + ", count is " + counted);
105         
106         Date JavaDoc last = (Date JavaDoc) context.getData(LAST_KEY);
107         
108         if (!now.equals(last)) {
109             ++counted;
110             last = now;
111         }
112
113         context.putData(COUNT_KEY, new Integer JavaDoc(counted));
114         context.putData(LAST_KEY, last);
115         
116         if ( counted <= countTo) {
117             Schedule child = getChildSchedule();
118             child.setLimits(getLimits());
119             return child.nextDue(context);
120         }
121         else {
122             return null;
123         }
124     }
125     
126     /**
127      * Override toString to be more meaningful.
128      */

129
130     public String JavaDoc toString() {
131         return "Count Schedule, count to " + countTo;
132     }
133 }
134
Popular Tags