KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > tools > remote > soap > Client


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.tools.remote.soap;
10
11 import javax.management.MBeanServerConnection JavaDoc;
12 import javax.management.MBeanServerDelegateMBean JavaDoc;
13 import javax.management.MBeanServerInvocationHandler JavaDoc;
14 import javax.management.Notification JavaDoc;
15 import javax.management.NotificationListener JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17 import javax.management.remote.JMXConnector JavaDoc;
18 import javax.management.remote.JMXConnectorFactory JavaDoc;
19 import javax.management.remote.JMXServiceURL JavaDoc;
20 import javax.management.timer.Timer JavaDoc;
21
22 /**
23  * This example shows how to connect to a JMXConnectorServer over the SOAP protocol.
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.4 $
47  */

48 public class Client
49 {
50    public static void main(String JavaDoc[] args) throws Exception JavaDoc
51    {
52       // This JMXServiceURL works only if the connector server is in-VM with
53
// the connector. If this is not the case, set the correct host name.
54
JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("soap", null, 8080, "/jmxconnector");
55
56       // Connect a JSR 160 JMXConnector to the server side
57
JMXConnector JavaDoc connector = JMXConnectorFactory.connect(address);
58
59       // Retrieve an MBeanServerConnection that represent the MBeanServer
60
// the remote connector server is bound to
61
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
62
63       // Call the server side as if it is a local MBeanServer
64
ObjectName JavaDoc delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
65       Object JavaDoc proxy = MBeanServerInvocationHandler.newProxyInstance(connection, delegateName, MBeanServerDelegateMBean JavaDoc.class, true);
66       MBeanServerDelegateMBean JavaDoc delegate = (MBeanServerDelegateMBean JavaDoc)proxy;
67
68       System.out.println(delegate.getImplementationVendor() + " is cool !");
69
70       // Register an MBean, and get notifications via the SOAP protocol
71
connection.addNotificationListener(delegateName, new NotificationListener JavaDoc()
72       {
73          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
74          {
75             System.out.println("Got the following notification: " + notification);
76          }
77       }, null, null);
78
79       ObjectName JavaDoc timerName = ObjectName.getInstance("services:type=Timer");
80       connection.createMBean(Timer JavaDoc.class.getName(), timerName, null);
81
82       // Unregistering the MBean to get another notification
83
connection.unregisterMBean(timerName);
84
85       // Allow the unregistration notification to arrive before killing this JVM
86
Thread.sleep(1000);
87
88       connector.close();
89    }
90 }
91
Popular Tags