01: package de.webman.util.scheduler;
02:
03: import java.util.Date;
04: import java.util.Calendar;
05:
06: /**
07: * this service entry represents a service which is called frequently in
08: * time. It is characterized by a specific start time, a start off delay,
09: * a repeat frequency, and a stop time.
10: *
11: * @author <a href="mailto:gregor@webman.de">Gregor Klinke</a>
12: * @version $Revision: 1.2 $
13: **/
14: class FrequentService extends ServiceEntry {
15: /* $Id: FrequentService.java,v 1.2 2002/04/12 12:45:53 gregor Exp $ */
16:
17: /**
18: * constructor, only to be used by the schedule manager
19: * @param _id the identification string of the service
20: * @param _start_at start the service at this point, must not be <code>null</code>
21: * @param _stop_at stop the service at this point, if <code>null</code> stop never
22: * @param _frequency the repeat frequency in milliseconds
23: * @param _delay the start off delay in milliseconds (delays the first
24: * run relative to start_at)
25: * @param _factory an instance of the service to execute
26: **/
27: FrequentService(String _id, Date _start_at, Date _stop_at,
28: long _frequency, long _delay,
29: SchedulerServiceFactory _factory) {
30: if (_delay > 0) {
31: Calendar cal = Calendar.getInstance();
32: cal.setTime(_start_at);
33: cal.add(Calendar.MILLISECOND, (int) _delay);
34: _start_at = cal.getTime();
35: }
36:
37: init(_id, _start_at, _stop_at, _frequency, _delay, _factory);
38: }
39:
40: /**
41: * returns <code>true</code> if the service is due to be executed
42: * @param ref the reference date to check for
43: * @return <code>true</code> if it is due
44: **/
45: boolean isDue(Date ref) {
46: return (ref.after(start_at) && (last + frequency < System
47: .currentTimeMillis()));
48: }
49:
50: /**
51: * returns <code>true</code> if this service is outdated
52: * @param ref the reference date to check for
53: * @return <code>true</code> if it is outdated
54: **/
55: boolean isOutdated(Date ref) {
56: if (stop_at != null)
57: return ref.after(stop_at);
58: return false;
59: }
60:
61: /**
62: * starts a service, by allocating a new service instance and starting
63: * it of in a new thread ({@link
64: * de.webman.util.scheduler.ServiceThread})
65: **/
66: void executeNewService() {
67: execute();
68: last = System.currentTimeMillis();
69: }
70: }
|