MX4J Remote Tools and Utilities
Prev | Chapter 3. JSR 160 (JMX Remoting) Explained | Next

## MX4J Remote Tools and Utilities

### Remote MBean Proxying

Complex systems based on JMX are also, most of the times, distributed systems.
Such systems are often made of nodes, and each node is made of components.
When the underlying architecture is JMX, in each node there is one (possibly more) MBeanServer that manages the components, and each component is an MBean.

Sometimes there is the need to have one MBeanServer gathering information from many remote MBeanServers. However, the information needed is only part of the information exposed by each remote MBeanServer: you may want to collect only some information from each remote MBeanServer.
For this purpose, it is useful to write an MBean that exposes this information, and register one instance of this MBean per each remote MBeanServer.

A client application may want to connect to the gathering MBeanServer to see the gathered information, without having the hassle to connect to each individual remote MBeanServer and choose the information MBean among the others.

In short, the gathering MBeanServer acts as a proxy for certain MBeans hosted elsewhere: client applications only need to contact the gathering MBeanServer to get the information they need.

For this purpose MX4J provides a remote MBean proxy: an MBean you can register in the gathering MBeanServer that acts as a proxy for a remote MBean hosted in a remote MBeanServer.
Refer anyway to the javadocs of the RemoteMBeanProxy class for further information.
Below there is an example where remote MBeanServers publish information about the downloads of products, and a gathering MBeanServer proxies these information for easier access via the HTTPAdaptor.

Example 3.27. Remote MBean proxying

    // A remote MBeanServer
    // It exposes also a JMXConnectorServer at the address 'service:jmx:rmi://localhost/jndi/jmx1'
    MBeanServer remoteMBeanServer1 = ...;

    // A remote MBean
    Object remoteMBean1 = ...;
    ObjectName remoteMBeanName1 = ObjectName.getInstance("sytem.information:type=downloads,product=mx4j");
    remoteMBeanServer1.registerMBean(remoteMBean1, remoteMBeanName1);

    // Another remote MBeanServer
    // It exposes also a JMXConnectorServer at the address 'service:jmx:rmi://localhost/jndi/jmx2'
    MBeanServer remoteMBeanServer2 = ...;

    // A remote MBean
    Object remoteMBean2 = ...;
    ObjectName remoteMBeanName2 = ObjectName.getInstance("information.data:type=downloads,product=tomcat");
    remoteMBeanServer2.registerMBean(remoteMBean2, remoteMBeanName2);

    // The proxy for the MBean in the first MBeanServer
    JMXServiceURL url1 = new JMXServiceURL("rmi", "localhost", 0, "/jndi/jmx1");
    RemoteMBeanProxy proxy1 = new RemoteMBeanProxy(remoteMBeanName1, url1);
    ObjectName proxyName1 = ObjectName.getInstance("system.download:product=mx4j");

    // The proxy for the MBean in the second MBeanServer
    JMXServiceURL url2 = new JMXServiceURL("rmi", "localhost", 0, "/jndi/jmx2");
    RemoteMBeanProxy proxy2 = new RemoteMBeanProxy(remoteMBeanName2, url2);
    ObjectName proxyName2 = ObjectName.getInstance("system.download:product=tomcat");

    // The gathering MBeanServer
    // It exposes also a HTTPAdaptor on port 8082
    MBeanServer gatheringMBeanServer = ...;
    gatheringMBeanServer.registerMBean(proxy1, proxyName1);
    gatheringMBeanServer.registerMBean(proxy2, proxyName2);

    // Browsing on port 8082 you can see, on the gathering MBeanServer, the proxied MBeans.

The `mx4j.remote.RemoteMBeanProxy` MBean is a transparent proxy for the remote MBean: invoking a method on the proxy results in the method being invoked on the remote MBean; registering a local NotificationListener on the proxy results in registration on the remote MBean, and thus the local listener will receive notifications emitted by the remote MBean.
For example, by proxying the remote MBeanServerDelegate MBean, you can receive locally notifications of MBean registrations/unregistrations that happens in the remote MBeanServer.

Prev | Home | Next
Porting old MX4J remoting code to JSR 160 | Up | Chapter 4. MX4J Extensions and utilities