KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > util > scheduler > FrequentService


1 package de.webman.util.scheduler;
2
3 import java.util.Date JavaDoc;
4 import java.util.Calendar JavaDoc;
5
6
7 /**
8  * this service entry represents a service which is called frequently in
9  * time. It is characterized by a specific start time, a start off delay,
10  * a repeat frequency, and a stop time.
11  *
12  * @author <a HREF="mailto:gregor@webman.de">Gregor Klinke</a>
13  * @version $Revision: 1.2 $
14  **/

15 class FrequentService
16     extends ServiceEntry
17 {
18     /* $Id: FrequentService.java,v 1.2 2002/04/12 12:45:53 gregor Exp $ */
19     
20     /**
21      * constructor, only to be used by the schedule manager
22      * @param _id the identification string of the service
23      * @param _start_at start the service at this point, must not be <code>null</code>
24      * @param _stop_at stop the service at this point, if <code>null</code> stop never
25      * @param _frequency the repeat frequency in milliseconds
26      * @param _delay the start off delay in milliseconds (delays the first
27      * run relative to start_at)
28      * @param _factory an instance of the service to execute
29      **/

30     FrequentService(String JavaDoc _id,
31                     Date JavaDoc _start_at,
32                     Date JavaDoc _stop_at,
33                     long _frequency, long _delay,
34                     SchedulerServiceFactory _factory)
35     {
36         if (_delay > 0) {
37             Calendar JavaDoc cal = Calendar.getInstance();
38             cal.setTime(_start_at);
39             cal.add(Calendar.MILLISECOND, (int)_delay);
40             _start_at = cal.getTime();
41         }
42         
43         init(_id, _start_at, _stop_at, _frequency, _delay, _factory);
44     }
45
46
47     /**
48      * returns <code>true</code> if the service is due to be executed
49      * @param ref the reference date to check for
50      * @return <code>true</code> if it is due
51      **/

52     boolean isDue(Date JavaDoc ref) {
53         return (ref.after(start_at) &&
54                 (last + frequency < System.currentTimeMillis()));
55     }
56     
57     /**
58      * returns <code>true</code> if this service is outdated
59      * @param ref the reference date to check for
60      * @return <code>true</code> if it is outdated
61      **/

62     boolean isOutdated(Date JavaDoc ref) {
63         if (stop_at != null)
64             return ref.after(stop_at);
65         return false;
66     }
67     
68
69     /**
70      * starts a service, by allocating a new service instance and starting
71      * it of in a new thread ({@link
72      * de.webman.util.scheduler.ServiceThread})
73      **/

74     void executeNewService() {
75         execute();
76         last = System.currentTimeMillis();
77     }
78 }
79
80
Popular Tags