01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.impl;
17:
18: import java.util.concurrent.ScheduledFuture;
19: import java.util.concurrent.ScheduledThreadPoolExecutor;
20: import java.util.concurrent.TimeUnit;
21:
22: import org.directwebremoting.extend.Alarm;
23: import org.directwebremoting.extend.Sleeper;
24: import org.directwebremoting.util.SharedObjects;
25:
26: /**
27: * An Alarm that goes off after a certain length of time.
28: * @author Joe Walker [joe at getahead dot ltd dot uk]
29: */
30: public class TimedAlarm extends BasicAlarm implements Alarm {
31: /**
32: * @param waitTime How long we wait before the Alarm goes off
33: */
34: public TimedAlarm(long waitTime) {
35: this .waitTime = waitTime;
36: }
37:
38: /* (non-Javadoc)
39: * @see org.directwebremoting.dwrp.PollHandler.Alarm#cancel()
40: */
41: @Override
42: public void cancel() {
43: future.cancel(false);
44: super .cancel();
45: }
46:
47: /* (non-Javadoc)
48: * @see org.directwebremoting.dwrp.PollHandler.Alarm#setAlarmAction(org.directwebremoting.dwrp.PollHandler.Sleeper)
49: */
50: @Override
51: public void setAlarmAction(Sleeper sleeper) {
52: Runnable runnable = new Runnable() {
53: public void run() {
54: raiseAlarm();
55: }
56: };
57:
58: ScheduledThreadPoolExecutor executor = SharedObjects
59: .getScheduledThreadPoolExecutor();
60: future = executor.schedule(runnable, waitTime,
61: TimeUnit.MILLISECONDS);
62:
63: super .setAlarmAction(sleeper);
64: }
65:
66: /**
67: * The future result that allows us to cancel the timer
68: */
69: private ScheduledFuture<?> future;
70:
71: /**
72: * How long do we wait for?
73: */
74: private long waitTime;
75: }
|