Connecting to the JMX agent

You can access the JMX agent remotely using an RMI connector by writing a client that opens an RMI connection using the classes provided with this release. The client should obtain an object that implements the javax.management.MBeanServer interface over RMI. This interface provides a number of methods that allow the client to interact with the JMX agent, such as invoking methods on MBeans and obtaining MBean information. However, some methods that are exposed in the interface do not work remotely; for example, the registerMBean API call.

This is an example of a client that accesses the JMX agent:

package client;
import java.util.Properties;
import javax.management.MBeanServer;
import com.sybase.management.jmx.util.MBeanServerLocator;
import com.sybase.management.jmx.util.MX4JRMIConnectorServerLocator;

// Simple Client to the JMX agent - just prints number of MBeans

public class Client
{
  public static void main(String[] args)
  {
     MBeanServer server = null;

     if (args.length < 4)
     {
        System.err.println("usage: Client <host> <port> <user> <password>");
        System.exit(1);
     }

     String host = args[0];
     String port = args[1];
     String user = args[2];
     String password = args[3];

     Properties props = new Properties();

     props.put(MBeanServerLocator.PROVIDER_HOSTNAME, host);
     props.put(MBeanServerLocator.PROVIDER_PORT, port);
     props.put(MBeanServerLocator.SECURITY_PRINCIPAL, user);
     props.put(MBeanServerLocator.SECURITY_CREDENTIALS, password);

     // optional: SSL
     //
     // To verify the server's identity, pass in our truststore 
     // so that we can check that the server's certificate is valid
     //
     //if (args.length < 5)
     //{
     //    System.err.println("usage: Client <host> <port> <user> <password> <truststore>");
     //    System.exit(1);
     //}
     //String trustStoreFileName = args[4];
     //props.put(MX4JRMIConnectorServerLocator.TRUST_STORE_FILE, trustStoreFileName);
     //
     // To verify the client's identity, pass in a pointer to our keystore
     //
     //if (args.length < 6)
     //{
     //    System.err.println("usage: Client <host> <port> <user> <password> <truststore> <keystore>");
     //    System.exit(1);
     //}
     //String keyStoreFileName = args[6];
     //props.put(MX4JRMIConnectorServerLocator.KEY_STORE_FILE, keyStoreFileName);

     try
     {
     // get a connection with the remote JMX agent
     server = MX4JRMIConnectorServerLocator.getMBeanServer(props);
     }
     catch (Exception e)
     {
        System.err.println("Failed to connect to the JMX agent: " + e);
        e.printStackTrace(System.err);
        System.exit(1);
     }
     // use the connection ...
     System.out.println("There are " + server.getMBeanCount() + " MBeans");

     System.exit(0);
     }
}