HowDoIGetRemoteAccessToMyMBean

Version 19

    How do I get remote access to my MBean?

    More complete information for some of these answers can be found in Chapter 2 of the admin

    docs.

     

    Using twiddle.sh

    A command line script is shipped with JBoss to twiddle bits in JMX; i.e. invoke operations, get and update attributes, and query available MBeans.

     

    Using the RMIAdaptor (weakly typed interface)

    The RMIAdaptor provides a remote view of the MBeanServer Note: Use the MBeanServerConnection interface rather than RMIAdaptor on the most recent versions of JBoss.  RMIAdaptor should not be used in version 6.0 (M3) or greater (you must use MBeanServerConnection).

    //import org.jboss.jmx.adapter.rmi.RMIAdaptor;
    import javax.management.MBeanServerConnection;
    
    public void doSomething() throws Exception
    {
       InitialContext ctx = new InitialContext(); // From jndi.properties
       //RMIAdaptor server = (RMIAdaptor) ctx.lookup("jmx/invoker/RMIAdaptor");
       MBeanServerConnection server = (MBeanServerConnection) ctx.lookup("jmx/invoker/RMIAdaptor");
       System.out.println(server.getAttribute(new ObjectName("MyDomain:key=property"), "AnAttribute"));
       server.invoke(new ObjectName("MyDomain:key=property"), "doSomething", new Object[0], new String[0]);
    }
    
    // For AS 6.0 (M3) or greater, use the following example
    import javax.management.MBeanServerConnection;
    import javax.management.ObjectName;
    import javax.management.remote.JMXConnector;
    import javax.management.remote.JMXConnectorFactory;
    import javax.management.remote.JMXServiceURL;
    
    public void doSomething() throws Exception
    {
       String serverURL = "service:jmx:rmi:///jndi/rmi://localhost:1090/jmxrmi"
       String username = null;
       String password = null;
       HashMap env = new HashMap();
       if (username != null && password != null)
       {
          String[] creds = new String[2];
          creds[0] = username;
          creds[1] = password;
          env.put(JMXConnector.CREDENTIALS, creds);
       }
       JMXServiceURL url = new JMXServiceURL(serverURL);
       JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
       // Remember to call jmxc.close() when you are done with server connection.
       MbeanServerConnection server = jmxc.getMBeanServerConnection();
       System.out.println(server.getAttribute(new ObjectName("MyDomain:key=property"), "AnAttribute"));
       server.invoke(new ObjectName("MyDomain:key=property"), "doSomething", new Object[0], new String[0]);
    }
    
    
    
    

     

    Using the detached invokers (strongly typed interface)

    Note:  This is only for versions prior to AS 6.0 (M3).

    This binds a jboss detached invoker proxy with a specified interface into jndi. You can choose the transport on the proxy deployment. See the admin guide for more info.  Also see JMXMBeanRemoteProxy for an example of how to do this.

     

    Use an http transport

    Note:  This is only for versions prior to AS 6.0 (M3).

    See FindMBeanServer.

     

    Use RMI

    Make your object a java.rmi.RemoteObject, and compile it with rmic. You can then bind it into jndi. This is not the most efficient way of doing it.

     

    Use a stateless session bean

    Stateless beans are natural remote objects that can delegate to MBeans. You also get standard EJB security config.

     

    Write a servlet

    Similar to the EJB solution, except using a servlet. The disadvantage is that you have to program using URL requests rather than java on the client.

     

    Just make it java.io.Serializable and bind it into jndi

    NO!!! The client will receive a copy of the object, not remote access.

     

    NonSerializableFactory

    NO!!! This seems to be a common misunderstanding. The NonSerializableFactory takes objects that are not java.io.Serializable (and hence can't be bound into jndi by spec) and creates an indirection such that they can be bound. The object is still not serializable and in no way will it be available on a remote machine. The indirected object can be accessed inside the same JVM as where it is bound.

     

    Even if it was Serializable you would get a copy of the object not remote access, see above.

     

    Question: Why do people keep making this basic misunderstanding?