01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package mx4j.examples.mbeans.legacy;
10:
11: import javax.management.MBeanServer;
12: import javax.management.MBeanServerFactory;
13: import javax.management.Notification;
14: import javax.management.NotificationListener;
15: import javax.management.ObjectName;
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.1 $
23: */
24: public class LegacyExample {
25: public static void main(String[] args) throws Exception {
26: // Create the service
27: LegacyService legacyService = new LegacyService();
28:
29: // Create the JMX MBeanServer and register the service wrapper
30: MBeanServer server = MBeanServerFactory.newMBeanServer();
31: ObjectName serviceName = new ObjectName("examples", "mbean",
32: "legacy");
33: DynamicLegacyService dynamicService = new DynamicLegacyService(
34: 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,
39: new NotificationListener() {
40: public void handleNotification(
41: Notification notification, Object handback) {
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[] args) {
58: LegacyService service = new LegacyService();
59: service.execute();
60: }
61: }
|