1 22 23 24 package hero.util; 25 26 import javax.management.MBeanServer ; 27 import javax.management.ObjectName ; 28 import javax.management.InstanceAlreadyExistsException ; 29 import javax.management.MBeanRegistrationException ; 30 import javax.management.NotCompliantMBeanException ; 31 import javax.management.MalformedObjectNameException ; 32 import javax.management.InstanceNotFoundException ; 33 import javax.management.NotificationBroadcaster ; 34 import javax.management.MBeanServerFactory ; 35 import javax.management.MBeanException ; 36 import javax.management.ReflectionException ; 37 import javax.naming.Context ; 38 import org.objectweb.jonas.service.ServiceException; 39 40 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 { 46 47 private MBeanServer mbeanServer = null; 48 private MBeanServer server; 49 private final static String SERVICE_NAME = "jonas:type=service,name=Timer"; 50 private ObjectName timerName; 51 javax.management.timer.Timer timer; 52 53 public void doInit(Context ctx) throws ServiceException { 54 try { 56 JmxService jmx = (JmxService) ServiceManager.getInstance().getJmxService(); 57 mbeanServer = jmx.getJmxServer(); 58 } catch (Exception e) { 59 throw new ServiceException("JMX Server is not started..."); 61 } 62 } 63 public void doStart() throws ServiceException { 64 try { 65 timer = new javax.management.timer.Timer (); 67 mbeanServer.registerMBean(timer,new ObjectName (SERVICE_NAME)); 68 server = MBeanServerFactory.createMBeanServer(); 69 timerName = new ObjectName ("jonas:type=service,name=Timer"); 70 server.createMBean("javax.management.timer.Timer", timerName,new Object [0], new String [0]); 71 server.invoke(timerName, "start", new Object [0], new String [0]); 72 timer.start(); 73 System.out.println("Timer :: Start Service"); 74 75 } catch (MBeanException me) { 76 throw new ServiceException("Cannot start the Timer Service (MBean Exception)" , me); 77 } catch (ReflectionException re) { 78 throw new ServiceException("Cannot start the Timer Service (Reflexion Exception)" , re); 79 } catch (InstanceNotFoundException infe) { 80 throw new ServiceException("Cannot start the Timer Service (MBean Not Found)" , infe); 81 } catch (InstanceAlreadyExistsException iae) { 82 throw new ServiceException("Cannot start the Timer Service (Already Exists)" , iae); 83 } catch (NotCompliantMBeanException ncmbe) { 84 throw new ServiceException("Cannot start the Timer Service (MBean Not compliant error)" , ncmbe); 85 } catch (MalformedObjectNameException 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 (SERVICE_NAME)); 95 server.invoke(timerName, "removeAllNotifications", new Object [0], new String [0]); 96 server.invoke(timerName, "stop", new Object [0], new String [0]); 97 server.unregisterMBean(timerName); 98 MBeanServerFactory.releaseMBeanServer(server); 99 } catch (InstanceNotFoundException infe) { 100 throw new ServiceException("Cannot stop the Timer Service (MBean Not Found).", infe); 101 } catch (Exception 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 |