KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > services > TimerService


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.monitor.services;
23
24 import java.util.Date JavaDoc;
25
26 import javax.management.InstanceNotFoundException JavaDoc;
27 import javax.management.MBeanServerInvocationHandler JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.timer.TimerMBean JavaDoc;
30
31 import org.jboss.system.ServiceMBeanSupport;
32 import org.jboss.util.Strings;
33
34 /**
35  * A simple service used to configure the periodic emition of notifications
36  * by a standard JMX Timer.
37  *
38  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
39  * @version $Revision: 46247 $
40  */

41 public class TimerService extends ServiceMBeanSupport
42    implements TimerServiceMBean
43 {
44    // Private Data --------------------------------------------------
45

46    /** Notification type */
47    private String JavaDoc notificationType;
48    
49    /** Notification message */
50    private String JavaDoc notificationMessage;
51    
52    /** Timer period string */
53    private String JavaDoc timerPeriodString;
54    
55    /** Number of occurences */
56    private long repeatitions;
57    
58    /** Periodic execution mode */
59    private boolean fixedRate;
60    
61    /** TimerMBean name */
62    private ObjectName JavaDoc timerObjectName;
63    
64    /** Timer period as long */
65    private long timerPeriod;
66    
67    /** Proxy to the TimerMBean */
68    private TimerMBean JavaDoc timerProxy;
69
70    /** The timer subscription id */
71    private Integer JavaDoc id;
72    
73    // Constructors --------------------------------------------------
74

75    /**
76     * CTOR
77     */

78    public TimerService()
79    {
80       // empty
81
}
82    
83    // Attributes -----------------------------------------------------
84

85    /**
86     * @jmx:managed-attribute
87     */

88    public void setNotificationType(String JavaDoc type)
89    {
90       this.notificationType = type;
91    }
92    
93    /**
94     * @jmx:managed-attribute
95     */

96    public String JavaDoc getNotificationType()
97    {
98       return notificationType;
99    }
100
101    /**
102     * @jmx:managed-attribute
103     */

104    public void setNotificationMessage(String JavaDoc message)
105    {
106       this.notificationMessage = message;
107    }
108    
109    /**
110     * @jmx:managed-attribute
111     */

112    public String JavaDoc getNotificationMessage()
113    {
114       return notificationMessage;
115    }
116    
117    /**
118     * @jmx:managed-attribute
119     */

120    public void setTimerPeriod(String JavaDoc timerPeriod)
121    {
122       this.timerPeriod = Strings.parsePositiveTimePeriod(timerPeriod);
123       this.timerPeriodString = timerPeriod;
124    }
125    
126    /**
127     * @jmx:managed-attribute
128     */

129    public String JavaDoc getTimerPeriod()
130    {
131       return this.timerPeriodString;
132    }
133    
134    /**
135     * @jmx:managed-attribute
136     */

137    public void setRepeatitions(long repeatitions)
138    {
139       this.repeatitions = repeatitions;
140    }
141    
142    /**
143     * @jmx:managed-attribute
144     */

145    public long getRepeatitions()
146    {
147       return repeatitions;
148    }
149    
150    /**
151     * @jmx:managed-attribute
152     */

153    public void setFixedRate(boolean fixedRate)
154    {
155       this.fixedRate = fixedRate;
156    }
157    
158    /**
159     * @jmx:managed-attribute
160     */

161    public boolean getFixedRate()
162    {
163       return fixedRate;
164    }
165    
166    /**
167     * @jmx:managed-attribute
168     */

169    public void setTimerMBean(ObjectName JavaDoc timerMBean)
170    {
171       this.timerProxy = (TimerMBean JavaDoc)MBeanServerInvocationHandler
172          .newProxyInstance(getServer(), timerMBean, TimerMBean JavaDoc.class, false);
173       this.timerObjectName = timerMBean;
174    }
175    
176    /**
177     * @jmx:managed-attribute
178     */

179    public ObjectName JavaDoc getTimerMBean()
180    {
181       return timerObjectName;
182    }
183    
184    // Lifecycle control (ServiceMBeanSupport) -----------------------
185

186    /**
187     * Start
188     */

189    public void startService() throws Exception JavaDoc
190    {
191       if (timerProxy != null)
192       {
193          id = timerProxy.addNotification(
194                notificationType,
195                notificationMessage,
196                null, // UserData
197
new Date JavaDoc(), // now
198
timerPeriod,
199                repeatitions,
200                fixedRate);
201          
202          log.debug("Added timer notification(" + id
203                + ") : type=" + notificationType
204                + ", message=" + notificationMessage
205                + ", period=" + timerPeriodString
206                + ", repeatitions=" + repeatitions
207                + ", fixedRate=" + fixedRate
208                + ", to timer '" + timerObjectName + "'");
209       }
210       else
211       {
212          log.warn("TimerMBean not set");
213       }
214    }
215    
216    /**
217     * Stop
218     */

219    public void stopService()
220    {
221       if (id != null)
222       {
223          try
224          {
225             timerProxy.removeNotification(id);
226             log.debug("Removed timer notification(" + id + ") from timer '" + timerObjectName + "'");
227          }
228          catch (InstanceNotFoundException JavaDoc ignore)
229          {
230             // after a fixed number of repeatitions
231
// the notification is removed from the timer
232
}
233          id = null;
234       }
235    }
236    
237 }
238
Popular Tags