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.helloworld;
10:
11: import javax.management.MBeanServer;
12: import javax.management.MBeanServerFactory;
13: import javax.management.ObjectName;
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: public static void main(String[] args) throws Exception {
34: // Create an instance of MBeanServer
35: MBeanServer server = MBeanServerFactory.createMBeanServer();
36:
37: // Create an ObjectName for the MBean
38: ObjectName name = new ObjectName(":mbean=helloworld");
39:
40: // Create and register the MBean in the MBeanServer
41: server.createMBean(
42: "mx4j.examples.mbeans.helloworld.HelloWorld", name,
43: null);
44:
45: // Invoke a method on it
46: server.invoke(name, "reloadConfiguration", new Object[0],
47: new String[0]);
48:
49: // Invoke an attribute on it
50: Integer times = (Integer) server.getAttribute(name,
51: "HowManyTimes");
52:
53: System.out.println("The configuration was reloaded " + times
54: + " times.");
55: }
56: }
|