KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > mbeans > legacy > LegacyExample


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.examples.mbeans.legacy;
10
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.MBeanServerFactory JavaDoc;
13 import javax.management.Notification JavaDoc;
14 import javax.management.NotificationListener JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16
17 /**
18  * This example aims to show how it is possible, with JMX, to write a non-invasive
19  * wrapper for an existing legacy service in order to expose the functionality
20  * of the legacy service with JMX.
21  *
22  * @version $Revision: 1.3 $
23  */

24 public class LegacyExample
25 {
26    public static void main(String JavaDoc[] args) throws Exception JavaDoc
27    {
28       // Create the service
29
LegacyService legacyService = new LegacyService();
30
31       // Create the JMX MBeanServer and register the service wrapper
32
MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
33       ObjectName JavaDoc serviceName = new ObjectName JavaDoc("examples", "mbean", "legacy");
34       DynamicLegacyService dynamicService = new DynamicLegacyService(legacyService);
35       server.registerMBean(dynamicService, serviceName);
36
37       // Now register a listener: we want to be able to know when the service starts and stops
38
server.addNotificationListener(serviceName, new NotificationListener JavaDoc()
39       {
40          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
41          {
42             System.out.println(notification);
43          }
44       }, null, null);
45
46       // Now start the service, using the new method name: 'start' instead of 'execute'
47
server.invoke(serviceName, "start", null, null);
48    }
49
50    /**
51     * This is the old main routine that started the service.
52     * In this example we had the possibility to modify the starter of the service
53     * by renaming the main method and by writing a new one that uses JMX.
54     * However, it is also possible to write another starter leaving the legacy part
55     * totally unchanged.
56     */

57    public static void oldMain(String JavaDoc[] args)
58    {
59       LegacyService service = new LegacyService();
60       service.execute();
61    }
62 }
63
Popular Tags