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.tools.remote.soap;
10:
11: import javax.management.MBeanServerConnection;
12: import javax.management.MBeanServerDelegateMBean;
13: import javax.management.MBeanServerInvocationHandler;
14: import javax.management.Notification;
15: import javax.management.NotificationListener;
16: import javax.management.ObjectName;
17: import javax.management.remote.JMXConnector;
18: import javax.management.remote.JMXConnectorFactory;
19: import javax.management.remote.JMXServiceURL;
20: import javax.management.timer.Timer;
21:
22: /**
23: * This example shows how to connect to a SOAPConnectorServer.
24: * MX4J's implementation of the SOAP provider requires Axis 1.1, that in turn requires
25: * a servlet container to run. The default servlet container used is Jetty 4.2.x.
26: * To run this example, you need the following jars:
27: * <ul>
28: * <li>MX4J 2.x</li>
29: * <ul>
30: * <li>mx4j.jar</li>
31: * <li>mx4j-remote.jar</li>
32: * <li>mx4j-tools.jar</li>
33: * <li>mx4j-examples.jar</li>
34: * </ul>
35: * <li>Axis 1.1</li>
36: * <ul>
37: * <li>axis.jar</li>
38: * <li>jaxrpc.jar</li>
39: * <li>commons-logging.jar</li>
40: * <li>commons-discovery.jar</li>
41: * <li>saaj.jar</li>
42: * <li>wsdl4j.jar</li>
43: * </ul>
44: * </ul>
45: *
46: * @version $Revision: 1.1 $
47: */
48: public class Client {
49: public static void main(String[] args) throws Exception {
50: // This JMXServiceURL works only if the SOAPConnectorServer is in-VM with
51: // the SOAPConnector. If this is not the case, set the correct host name.
52: JMXServiceURL address = new JMXServiceURL("soap", null, 8080,
53: "/jmxconnector");
54:
55: // Connect a JSR 160 JMXConnector to the server side
56: JMXConnector connector = JMXConnectorFactory.connect(address);
57:
58: // Retrieve an MBeanServerConnection that represent the MBeanServer
59: // the remote connector server is bound to
60: MBeanServerConnection connection = connector
61: .getMBeanServerConnection();
62:
63: // Call the server side as if it is a local MBeanServer
64: ObjectName delegateName = ObjectName
65: .getInstance("JMImplementation:type=MBeanServerDelegate");
66: Object proxy = MBeanServerInvocationHandler.newProxyInstance(
67: connection, delegateName,
68: MBeanServerDelegateMBean.class, true);
69: MBeanServerDelegateMBean delegate = (MBeanServerDelegateMBean) proxy;
70:
71: System.out.println(delegate.getImplementationVendor()
72: + " is cool !");
73:
74: // Register an MBean, and get notifications via the SOAP protocol
75: connection.addNotificationListener(delegateName,
76: new NotificationListener() {
77: public void handleNotification(
78: Notification notification, Object handback) {
79: System.out
80: .println("Got the following notification: "
81: + notification);
82: }
83: }, null, null);
84:
85: ObjectName timerName = ObjectName
86: .getInstance("services:type=Timer");
87: connection.createMBean(Timer.class.getName(), timerName, null);
88:
89: // Unregistering the MBean to get another notification
90: connection.unregisterMBean(timerName);
91:
92: // Allow the unregistration notification to arrive before killing this JVM
93: Thread.sleep(1000);
94:
95: connector.close();
96: }
97: }
|