KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > sample > simple > Client


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package test.sample.simple;
8
9 import javax.management.MBeanServerConnection JavaDoc;
10 import javax.management.MBeanServerDelegateMBean JavaDoc;
11 import javax.management.MBeanServerInvocationHandler JavaDoc;
12 import javax.management.ObjectName JavaDoc;
13 import javax.management.NotificationListener JavaDoc;
14 import javax.management.Notification JavaDoc;
15 import javax.management.remote.JMXConnector JavaDoc;
16 import javax.management.remote.JMXConnectorFactory JavaDoc;
17 import javax.management.remote.JMXServiceURL JavaDoc;
18
19 /**
20  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
21  */

22 public class Client
23 {
24    public static void main(String JavaDoc[] args) throws Exception JavaDoc
25    {
26       // no host or port, since will be getting rmi stub
27
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxconnector");
28
29       JMXConnector JavaDoc connector = JMXConnectorFactory.connect(url);
30
31       MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
32
33       ObjectName JavaDoc delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
34       Object JavaDoc proxy = MBeanServerInvocationHandler.newProxyInstance(connection, delegateName, MBeanServerDelegateMBean JavaDoc.class, true);
35       MBeanServerDelegateMBean JavaDoc delegate = (MBeanServerDelegateMBean JavaDoc) proxy;
36
37       System.out.println("MBeanServer vendor is " + delegate.getImplementationVendor());
38       System.out.println("MBeanServer version is " + delegate.getImplementationVersion());
39       System.out.println("MBeanServer specification name is " + delegate.getSpecificationName());
40       System.out.println("MBeanServer specification vendor is " + delegate.getSpecificationVendor());
41       System.out.println("MBeanServer specification version is " + delegate.getSpecificationVersion());
42       System.out.println("MBeanServer MBeanCount is " + connection.getMBeanCount());
43
44       ObjectName JavaDoc objName = new ObjectName JavaDoc("test:name=sample");
45       connection.createMBean(Sample.class.getName(), objName);
46       Object JavaDoc ret = connection.invoke(objName, "doSomething", new Object JavaDoc[] {"foo"}, new String JavaDoc[] {String JavaDoc.class.getName()});
47       System.out.println("Return to doSomething() is " + ret);
48
49       NotificationListener JavaDoc listener = new Listener JavaDoc();
50       connection.addNotificationListener(objName,listener, null, null);
51
52       ret = connection.invoke(objName, "doSomething", new Object JavaDoc[] {"bar"}, new String JavaDoc[] {String JavaDoc.class.getName()});
53       System.out.println("Return to doSomething() is " + ret);
54
55       // give time for notification to come in
56
Thread.currentThread().sleep(5000);
57
58       connection.removeNotificationListener(objName, listener);
59
60       System.out.println("Removed notification listener.");
61
62    }
63
64
65    public static class Listener implements NotificationListener JavaDoc
66    {
67
68       /**
69        * Callback method from the broadcaster MBean this listener implementation
70        * is registered to.
71        *
72        * @param notification the notification object
73        * @param handback the handback object given to the broadcaster
74        * upon listener registration
75        */

76       public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
77       {
78          System.out.println("Got notification: " + notification);
79       }
80    }
81 }
Popular Tags