KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > mbeans > helloworld > HelloWorldExample


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.helloworld;
10
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.MBeanServerFactory JavaDoc;
13 import javax.management.ObjectName JavaDoc;
14
15 /**
16  * JMX HelloWorld example. <p>
17  * Instead of saying "Hello", this simple class shows how is possible to create services,
18  * register them in the JMX Agent, and invoke methods on them <strong>without</strong>
19  * having a reference to them. <br>
20  * One can create a service that can reload its configuration at runtime, and be queried
21  * on how many times the configuration is reloaded, and expose it as standard MBean. <br>
22  * This class shows in code what is possible to do via a management interface: once the
23  * service is registered, one can connect via (for example) the HTTP adaptor and
24  * invoke methods on the MBean. <br>
25  * The service (the MBean) can be registered in one host, while the system administrator
26  * can connect to the HTTP adaptor from another host using a browser and ask the service
27  * to reload its configuration, without stopping it nor being forced to login to the
28  * remote host.
29  *
30  * @version $Revision: 1.1 $
31  */

32 public class HelloWorldExample
33 {
34    public static void main(String JavaDoc[] args) throws Exception JavaDoc
35    {
36       // Create an instance of MBeanServer
37
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
38
39       // Create an ObjectName for the MBean
40
ObjectName JavaDoc name = new ObjectName JavaDoc(":mbean=helloworld");
41
42       // Create and register the MBean in the MBeanServer
43
server.createMBean("mx4j.examples.mbeans.helloworld.HelloWorld", name, null);
44
45       // Invoke a method on it
46
server.invoke(name, "reloadConfiguration", new Object JavaDoc[0], new String JavaDoc[0]);
47
48       // Invoke an attribute on it
49
Integer JavaDoc times = (Integer JavaDoc)server.getAttribute(name, "HowManyTimes");
50
51       System.out.println("The configuration was reloaded " + times + " times.");
52    }
53 }
54
Popular Tags