Network Deployment (Distributed operating systems), v8.0 > Administer applications and their environment > Use the administrative clients > Use administrative programs (JMX) > Create a Java Management Extensions client program using the Java Management Extensions Remote application programming interface


Develop a Java Management Extensions client program using Java Management Extensions Remote application programming interface

This topic describes how to develop a JMX connector specification and JMX Remote application programming interface (API) (JSR 160). The program can communicate by Remote Method Invocation over Internet Inter-ORB Protocol (RMI-IIOP)

This topic assumes a basic understanding of JSR 160, JMX APIs, and managed beans (MBeans). For more information on JSR 160, see the JSR 160: JMX Remote API at http://www.jcp.org/en/jsr/detail?id=160. For more information on the JMX APIs and on MBeans, view the application programming interfaces documentation.

We can administer your WAS environment through the administrative console, the wsadmin utility, or JMX programming. Complete this task to develop a JMX remote client program using the JMX remote API so that you can administer the environment through JMX programming.


Procedure

  1. Specify the JMX connector address for the server through the JMXServiceURL class.

    The value of the JMX service URL is:

    service:jmx:rmi://" + host + ":" + port + "/jndi/JMXConnector"
    
    For example, if the target server host is sales.xyz.com and the listening port is 1234, the JMX service URL is:
    service:jmx:rmi://sales.xyz.com:1234/jndi/JMXConnector
    

    You can find the value for port in the Ports table of the console server settings page or in serverindex.xml that includes the target server. If the URL does not specify a value for host, the product uses the default value of localhost. If the URL does not specify a value for port, the product uses the default value of 2809.

    When connecting to an admin agent, add the admin agent JMX connector port number to the end of the URL. For example, if the admin agent JMX connector host is sales.xyz.com and the port is 6789, then use the following URL:

    service:jmx:rmi://sales.xyz.com:6789/jndi/JMXConnector6789
    

  2. Set the JNDI provider URL property to use the administrative name service for the product.

    The JNDI provider URL property is javax.naming.Context.PROVIDER_URL. The administrative name service is WsnAdminNameService.

  3. If the client uses security, set the -Dcom.ibm.CORBA.ConfigURL and -Dcom.ibm.SSL.ConfigURL system properties in the client Java virtual machine (JVM).

    Without the -Dcom.ibm.CORBA.ConfigURL and -Dcom.ibm.SSL.ConfigURL system properties set to valid system properties files, the client does not work properly when security is enabled. The recommended way to run the JMX connector client is as an administrative thin client.

    • -Dcom.ibm.CORBA.ConfigURL=file:
      APP_CLIENT_ROOT/properties/sas.client.props
      
    • -Dcom.ibm.SSL.ConfigURL=file:
      APP_CLIENT_ROOT/properties/ssl.client.props
      

    Typically, you can copy the properties files from an installation profile directory, preferably from the target server profile directory.

  4. User ID and password for the server, if security is enabled.
  5. Establish the JMX connection.
  6. Get the MBean server connection instance.


Results

You have established a connection to the dmgr through an RMI connection and started the application server through the node agent MBean.


Example

Use the following thin client code example to create and use the JMX client.

Some statements are split on multiple lines for printing purposes.

import java.io.File;
import java.util.Date;
import java.util.Set;
import java.util.Hashtable;

import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class JMXRemoteClientApp implements NotificationListener {

   private MBeanServerConnection mbsc = null;
   private ObjectName nodeAgent;
   private ObjectName jvm;
   private long ntfyCount = 0;
   private static String userid = null;
   private static String pwd = null;

   public static void main(String[] args)
   {
      try {

         JMXRemoteClientApp client = new JMXRemoteClientApp();

         String host=args[0];
         String port=args[1];
         String nodeName =args[2];
         userid =args[3];
         pwd = args[4];

         client.connect(host,port);

         // Find a node agent MBean
         client.getNodeAgentMBean(nodeName);

         // Invoke the launch process.
         client.invokeLaunchProcess("server1");

         // Register for node agent events
         client.registerNotificationListener();

         // Run until interrupted.
         client.countNotifications();
  
      } catch (Exception e) {
         e.printStackTrace();
      }


   private void connect(String host,String port) throws Exception
   {
      String jndiPath="/WsnAdminNameService#JMXConnector";

      JMXServiceURL url =
        new JMXServiceURL("service:jmx:iiop://"+host+"/jndi/corbaname:iiop:"+host+":"+port+jndiPath);

      Hashtable h  = new Hashtable();

      //User ID and password for the server if security is enabled on server.

      .println("Userid is " + userid);
      .println("Password is " + pwd);
      if ((userid.length() != 0) && (pwd.length() != 0)) {
             .println("adding userid and password to credentials...");
             String[] credentials = new String[] {userid , pwd };
             h.put("jmx.remote.credentials", credentials);
      } else {
             .println("No credentials provided.");
      }


      //Establish the JMX connection.

      JMXConnector jmxc = JMXConnectorFactory.connect(url, h);

      //Get the MBean server connection instance.

      mbsc = jmxc.getMBeanServerConnection();

      .println("Connected to DeploymentManager");



   private void getNodeAgentMBean(String nodeName)
   {

     // Query for the object name of the node agent MBean on the given node
      try {
         String query = "WebSphere:type=NodeAgent,node=" + nodeName + ",*";
         ObjectName queryName = new ObjectName(query);
         Set s = mbsc.queryNames(queryName, null);
         if (!s.isEmpty()) {
            nodeAgent = (ObjectName)s.iterator().next();
                .println("NodeAgent mbean found "+ nodeAgent.toString());
         } else {
            .println("Node agent MBean was not found");
            System.exit(-1);
         }
      } catch (Exception e) {
         .println(e);
         System.exit(-1);
      }



   private void invokeLaunchProcess(String serverName)
   {
      // Use the launch process on the node agent MBean to start       // the given server.
      String opName = "launchProcess";
      String signature[] = { "java.lang.String"};
      String params[] = { serverName};
      boolean launched = false;
      try {
         Boolean b = (Boolean)mbsc.invoke(nodeAgent, opName, params, signature);
         launched = b.booleanValue();
         if (launched)
            .println(serverName + " was launched");
         else
            .println(serverName + " was not launched");

      } catch (Exception e) {
         .println("Exception invoking launchProcess: " + e);
      }



   private void registerNotificationListener()
   {

     // Register this object as a listener for notifications from the
      // node agent MBean.  Do not use a filter and do not use a handback
      // object.
      try {
         mbsc.addNotificationListener(nodeAgent, this, null, null);
         .println("Registered for event notifications");
      } catch (Exception e) {
         .println(e);
      }


   public void handleNotification(Notification ntfyObj, Object handback)
   {

     // Each notification that the node agent MBean generates results in       // a call to this method.
      ntfyCount++;
      .println("***************************************************");
      .println("* Notification received at " + new Date().toString());

     .println("* type
     = " + ntfyObj.getType());
      .println("* message   = " + ntfyObj.getMessage());
      .println("* source    = " + ntfyObj.getSource());
      .println(
                        "* seqNum    = " + Long.toString(ntfyObj.getSequenceNumber()));
      .println("* timeStamp = " + new Date(ntfyObj.getTimeStamp()));
      .println("* userData  = " + ntfyObj.getUserData());
      .println("***************************************************");



   private void countNotifications()
   {
      // Run until stopped.
      try {
         while (true) {
            Thread.currentThread().sleep(60000);
            .println(ntfyCount + " notification have been received");
         }
      } catch (InterruptedException e) {
      }


}

Additional Application Programming Interfaces (APIs)
Create a Java Management Extensions client program using the Java Management Extensions Remote application programming interface
Use application clients
http://www.jcp.org/en/jsr/detail?id=160

+

Search Tips   |   Advanced Search