KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > util > Timer


1 /*
2 *
3 * Timer.java -
4 * Copyright (C) 2003 Ecoo Team
5 * valdes@loria.fr
6 *
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */

22
23
24 package hero.util;
25
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.management.InstanceAlreadyExistsException JavaDoc;
29 import javax.management.MBeanRegistrationException JavaDoc;
30 import javax.management.NotCompliantMBeanException JavaDoc;
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.InstanceNotFoundException JavaDoc;
33 import javax.management.NotificationBroadcaster JavaDoc;
34 import javax.management.MBeanServerFactory JavaDoc;
35 import javax.management.MBeanException JavaDoc;
36 import javax.management.ReflectionException JavaDoc;
37 import javax.naming.Context JavaDoc;
38 import org.objectweb.jonas.service.ServiceException;
39
40 //import objectweb
41
import org.objectweb.jonas.jmx.JmxService;
42 import org.objectweb.jonas.service.AbsServiceImpl;
43 import org.objectweb.jonas.service.ServiceManager;
44
45 public class Timer extends AbsServiceImpl implements hero.util.TimerMBean, NotificationBroadcaster JavaDoc {
46     
47     private MBeanServer JavaDoc mbeanServer = null;
48     private MBeanServer JavaDoc server;
49     private final static String JavaDoc SERVICE_NAME = "jonas:type=service,name=Timer";
50     private ObjectName JavaDoc timerName;
51     javax.management.timer.Timer JavaDoc timer;
52
53         public void doInit(Context JavaDoc ctx) throws ServiceException {
54             // Get the JMX Server via JMX Service
55
try {
56                   JmxService jmx = (JmxService) ServiceManager.getInstance().getJmxService();
57                   mbeanServer = jmx.getJmxServer();
58             } catch (Exception JavaDoc e) {
59                 //the JMX service may not be started
60
throw new ServiceException("JMX Server is not started...");
61             }
62         }
63     public void doStart() throws ServiceException {
64         try {
65             // Register MailService MBean : MailServiceImplMBean
66
timer = new javax.management.timer.Timer JavaDoc();
67                 mbeanServer.registerMBean(timer,new ObjectName JavaDoc(SERVICE_NAME));
68             server = MBeanServerFactory.createMBeanServer();
69             timerName = new ObjectName JavaDoc("jonas:type=service,name=Timer");
70             server.createMBean("javax.management.timer.Timer", timerName,new Object JavaDoc[0], new String JavaDoc[0]);
71             server.invoke(timerName, "start", new Object JavaDoc[0], new String JavaDoc[0]);
72             timer.start();
73                 System.out.println("Timer :: Start Service");
74
75         } catch (MBeanException JavaDoc me) {
76             throw new ServiceException("Cannot start the Timer Service (MBean Exception)" , me);
77         } catch (ReflectionException JavaDoc re) {
78             throw new ServiceException("Cannot start the Timer Service (Reflexion Exception)" , re);
79         } catch (InstanceNotFoundException JavaDoc infe) {
80             throw new ServiceException("Cannot start the Timer Service (MBean Not Found)" , infe);
81         } catch (InstanceAlreadyExistsException JavaDoc iae) {
82             throw new ServiceException("Cannot start the Timer Service (Already Exists)" , iae);
83         } catch (NotCompliantMBeanException JavaDoc ncmbe) {
84             throw new ServiceException("Cannot start the Timer Service (MBean Not compliant error)" , ncmbe);
85         } catch (MalformedObjectNameException JavaDoc mone) {
86             throw new ServiceException("Cannot start the Timer Service (ObjectName Malformed)" , mone);
87         }
88     }
89
90     public void doStop() throws ServiceException {
91
92         if (mbeanServer != null) {
93             try {
94                 mbeanServer.unregisterMBean(new ObjectName JavaDoc(SERVICE_NAME));
95                 server.invoke(timerName, "removeAllNotifications", new Object JavaDoc[0], new String JavaDoc[0]);
96                 server.invoke(timerName, "stop", new Object JavaDoc[0], new String JavaDoc[0]);
97                 server.unregisterMBean(timerName);
98                 MBeanServerFactory.releaseMBeanServer(server);
99             } catch (InstanceNotFoundException JavaDoc infe) {
100                 throw new ServiceException("Cannot stop the Timer Service (MBean Not Found).", infe);
101             } catch (Exception JavaDoc e) {
102                 throw new ServiceException("Cannot stop the Timer Service (JMX).", e);
103             }
104         }
105         System.out.println("Timer :: Stop Service");
106     }
107     
108 }
109
Popular Tags