Accessing Standard and Dynamic MBeans via the RMI Connector

The purpose of this examples is to demonstrate the implementation of a standard MBean and a dynamic MBean. It also shows how to perform operations on them, both locally, and remotely through an RMI connection between a server and a remote client.

A standard MBean is one that statically defines its management interface through the name of the methods it contains. A dynamic MBean implements a specific java interface and reveals its attributes and opterations at run time.

The JMX technology defines a connector base on RMI. The RMI connector supports the standard RMI transports, Java Remote Method Protocol(JRMP) and the Internet Inter-Lbject Request Broker(ORB) Protocol(IIOP). This connector allows you to connect to an MBean server from a remote location, and perform operations on it, exactly as if the operations were being performed locally.

due to its size, the Server.java class is shown in several code excerpts.

public static void main(String[] args) {

try {

  • // Firstly, the Server.java class creates a new MBean server called mbs by calling the 
    // createMBeanServer() method of the MBeanServerFactory class.    

    MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 

    waitForEnterPressed();

    // Then, the default domain in which the MBean server is registered is obtained with a call to the 
    // getDefaultDomain() method of the MBeanServer interface.
    //The domain is identified by the the string domain
    String domain = mbs.getDefaultDomain(); 

    waitForEnterPressed();

    // The MBean class named SimpleStandard is also identified by a variable mbeanClassName
    // SimpleStandard is the name of java class for the java object of which this MBean is an instance. 
    String mbeanClassName = "SimpleStandard"; 

    // Anothere variable, the string mbeanObjectNameStr, is defined as the combination of the domain, 
    // plus the following key=value pairs:
    // The type, which in this case is the variable value of mbeanClassName
    // A name, to differentiate this MBean from other MBeans of the same type that might be created subsequently.
    // In this case the name number is 1.
    // The purpose of mbeanObjectNameStr is to give the MBean a human-readble identifier.
    String mbeanObjectNameStr = domain + ":type=" + mbeanClassName + ",name=1";

    // A call to createSimpleMBean creates and registers the SimpleStandard MBean in the local MBean server,  with the given object name. 
    ObjectName mbeanObjectName = createSimpleMBean(mbs, mbeanClassName, mbeanObjectNameStr); 
    waitForEnterPressed(); 
    printMBeanInfo(mbs, mbeanObjectName, mbeanClassName); 
    waitForEnterPressed(); 
    manageSimpleMBean(mbs, mbeanObjectName, mbeanClassName); 
    waitForEnterPressed();

    // This MBean is an instance of SimpleDynamic class 
    mbeanClassName = "SimpleDynamic"; 
    mbeanObjectNameStr = domain + ":type=" + mbeanClassName + ",name=1"; 
    mbeanObjectName = createSimpleMBean(mbs, mbeanClassName, mbeanObjectNameStr);
    waitForEnterPressed();
    printMBeanInfo(mbs, mbeanObjectName, mbeanClassName);
    waitForEnterPressed();
    manageSimpleMBean(mbs, mbeanObjectName, mbeanClassName);
    waitForEnterPressed();
    [...]

    // Create a new service URL called url, which serves as an address for the connector server.
    // In this example, the service URL is given in JNDI form. This service URL defines the following:
    // 1. The connector will use default RMI transport, denoted by rmi .
    // 2. The RMI registry in which the RMI connector stub will be running on port 9999 on the local host, and the server address will be registered under the name
    server. The 9999 specified in this example is arbitrary; you can use any available port.
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");

    // An RMI connector server named cm is created by calling the static method
    // newJMXConnectorServer of JMXConnectorFactory, with the service URL url , a null environment map,
    // and the MBean server mbs as parameters.
    JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);

    // The connector server cs is launched by calling the start() method of JMXConnectorServer,
    // whereupon RMIConnectorServer exports the RMI object server to the RMI registry.
    // The connector will remain open until the Enter key is pressed,
    // as instructed by the simple method waitForEnterPressed, that is defined later in the Server code.
    cs.start();
    waitForEnterPressed();
    cs.stop();

Code snippet of the class Server.java shows the definition of the method createSimpleMBean:

[...]
private static ObjectName createSimpleMBean(MBeanServer mbs, String mbeanClassName, String mbeanObjectNameStr) {
     echo("/n>>> Create the " + mbeanClassName + " MBean within the MBeanServer");
     echo("ObjectName = " + mbeanObjectNameStr);
     try {
          // The MBean instance with the object name  mbeanObjectNameStr is passed to getInstance method of the ObjectName interface
          // to create a new object name for registering the MBean inside the MBean server.
          //
The resulting object name instance is named mbeanObjectName.
          ObjectName mbeanObjectName = ObjectName.getInstance(mbeanObjectNameStr);

          // A call to MBeanServer’s method createMBean then instantiates an MBean defined by the combination of the java object 
          // identified by mbeanClassName and MBean instance mbeanObjectName and registers this MBean in the MBean server mbs.

          mbs.createMBean(mbeanClassName, mbeanObjectName); 
          return mbeanObjectName; 
     } catch (Exception e) {
            echo("!!! Could not create the " +  mbeanClassName + " MBean !!!");
            e.printStackTrace();
            echo("/nEXITING.../n");
            System.exit(1); 
     } 
     return null; 
}
[...]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值