KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > timers > impl > TimerManagerImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.timers.impl;
18
19 import java.util.Date JavaDoc;
20 import java.util.TimerTask JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.servicemix.timers.Timer;
25 import org.apache.servicemix.timers.TimerListener;
26 import org.apache.servicemix.timers.TimerManager;
27
28 public class TimerManagerImpl implements TimerManager {
29
30     private static final Log log = LogFactory.getLog(TimerManagerImpl.class);
31     private java.util.Timer JavaDoc timer;
32     
33     public Timer schedule(TimerListener listener, long delay) {
34         if (log.isDebugEnabled()) {
35             log.debug("Schedule timer " + listener + " for " + delay);
36         }
37         TimerImpl tt = new TimerImpl(listener);
38         timer.schedule(tt, delay);
39         return tt;
40     }
41
42     public Timer schedule(TimerListener listener, Date JavaDoc date) {
43         if (log.isDebugEnabled()) {
44             log.debug("Schedule timer " + listener + " at " + date);
45         }
46         TimerImpl tt = new TimerImpl(listener);
47         timer.schedule(tt, date);
48         return tt;
49     }
50     
51     public void start() {
52         timer = new java.util.Timer JavaDoc();
53     }
54     
55     public void stop() {
56         timer.cancel();
57     }
58     
59     protected static class TimerImpl extends TimerTask JavaDoc implements Timer {
60
61         private TimerListener timerListener;
62         
63         public TimerImpl(TimerListener timerListener) {
64             this.timerListener = timerListener;
65         }
66         
67         public boolean cancel() {
68             if (log.isDebugEnabled()) {
69                 log.debug("Timer " + timerListener + " cancelled");
70             }
71             return super.cancel();
72         }
73         
74         public TimerListener getTimerListener() {
75             return this.timerListener;
76         }
77
78         public void run() {
79             if (log.isDebugEnabled()) {
80                 log.debug("Timer " + timerListener + " expired");
81             }
82             this.timerListener.timerExpired(this);
83         }
84         
85     }
86
87 }
88
Popular Tags