Date Created: Thu 04-Aug-2011


    Getting WebSphere Application Server version 8 Runtime Status using JMX

    The code below is a very simple example of how to connect to a stand-alone local WebSphere instance without Global security enabled.

    ======================

    If you connect to WebSphere JConsole then you can see the attributes available for a given MBean. If you need help with how to setup JConsole for WebSphere then see this link

    http://www.webspheretools.com/sites/webspheretools.nsf/docs/Connecting%20JCosole%20to%20Websphere%208%20to%20view%20JMX%20MBeans

    ==========================

    Here is my JMX Connector's MBean tree using JConsole.

    version 8




    Here is the sample code to get the server's status:


    package com.screv;
    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 StandaloneWASJMXClient {

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

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

    StandaloneWASJMXClient client = new StandaloneWASJMXClient();

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

    String host="localhost";
    String port="9100";
    String nodeName = "node01";
    userid = "wasadmin";
    pwd = "wasadmin";

    client.connect(host,port);

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



    } 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);
    //JMXServiceURL url = new JMXServiceURL("service:jmx:iiop://192.168.0.175:9100/jndi/JMXConnector");

    Hashtable h = new Hashtable();

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

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


    //Establish the JMX connection.

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

    //Get the MBean server connection instance.

    mbsc = jmxc.getMBeanServerConnection();

    System.out.println("Connected to Application Server");
    }


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


    private void getServerAttributes(ObjectName attribObjName)
    {
    // Query for the object name of the node agent MBean on the given node
    try {


    String name = (String) mbsc.getAttribute(attribObjName,"name");
    String state = (String) mbsc.getAttribute(attribObjName, "state");
    System.out.println("Server name: " + name + ". Server state: " + state);

    } catch (Exception e) {
    System.out.println(e);
    System.exit(-1);
    }
    }


    =================

    Result of running Code


    Connected to Application Server
    Server mbean found WebSphere:name=server01,process=server01,platform=proxy,node=node01,j2eeType=J2EEServer,version=8.0.0.0,type=Server,mbeanIdentifier=cells/SteveRobinsonNode01Cell/nodes/node01/servers/server01/server.xml#Server_1183121908656,cell=SteveRobinsonNode01Cell,spec=1.0,processType=UnManagedProcess
    Server name: server01. Server state: STARTED
    }

Middleware Mentor - Steven Charles Robinson

About Me

Steve Robinson has been working in IT for over 15 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware consulting. Steve comes from both an administration and development background.

Before moving to JEE, Steve was an accomplished developer and consultant for both IBM Lotus Notes and Microsoft .NET Technologies.

Follow Steve as @stevencrobinson on twitter.

Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Oracle WebLogic Categories

JBoss Categories

Other Categories