Tuesday, January 3, 2012

Monitor WebSphere Application Server using JMX


Java Management Extensions
Java Management Extensions (JMX) is the Java standard for managing application resources. The management architecture that is defined by JMX is divided into three levels:

·         The bottom level is management instrumentation. Each manageable resource is described by an interface that specifies the attributes it has, the operations it supports, and the notifications it sends. This resource is a managed bean (MBean).

·         The middle level is the management agent. Each managed process contains a JMX agent that includes an MBean server, which provides a registry and access point for MBeans. Management clients must use the MBean server to access the registered MBeans.
·         The top level of the architecture is identified, but undefined in the current level of the JMX specification.
The top level of the architecture is the distributed services level, and its role is to facilitate remote access to JMX agents. This task is accomplished through connectors, which provide a protocol-independent, location-transparent, client-side interface to the MBean server (for example, a Remote Method Invocation (RMI) connector), or protocol adapters, which provide protocol-specific, server-side access to the MBean server (for example, an HTTP adapter).





Java Management Extensions in WebSphere Application Server

Java Management Extensions (JMX) is at the core of Application Server administration capabilities. The application server contains a JMX agent. All of the system components are defined as MBeans. The JMX agent in Application Server supports three types of connectors, Remote Method Invocation/Internet Inter-ORB Protocol (RMI/IIOP), Simple Object Access Protocol/Hypertext Transfer Protocol (SOAP/HTTP), and Simple Object Access Protocol/Hypertext Transfer Protocol Secure (SOAP/HTTPS), which provides remote access to the server resources. All of the administration tools included with Application Server use these JMX facilities to accomplish their functions.
In a stand-alone Application Server installation, servers exist and are administered individually. An administrative client connects directly to the Application Server in this environment.
Application Server provides an AdminService class that reflects the standard JMX MBeanServer interface, and wraps the MBeanServer interface so that it takes part in implementing this distributed management functionality.



Admin client
Admin client is a java program for accessing the WebSphere Application server administrative system by using the WAS API’s.

 

Developing an administrative client program

AdminClient with SOAP connector:

Step 1:

Create AdminClient object.

The following Properties are required to create AdminClient instance.

AdminClient.CONNECTOR_TYPE:  CONNECTOR_TYPE_SOAP:  The connector type which communicates via SOAP.

AdminClient.CONNECTOR_HOST:  Specify the IP address or host name.

AdminClient .CONNECTOR_PORT:  SOAP connector listening port (default port is 8880).
AdminClient.USERNAME and AdminClient.PASSWORD: specify the User name and password for authentication.
If you’re going to use the https URL, include the following properties also:
AdminClient.CONNECTOR_SECURITY_ENABLED: Set this flag to true.
javax.net.ssl.trustStore: specify the path where trustStore file is resided.
javax.net.ssl.keyStore: specify the path where keyStore file is resided.
javax.net.ssl.trustStorePassword: specify the trust store password (default password is WebAS).
 
javax.net.ssl.keyStorePassword: specify the key store password (default password is WebAS).
 
Pass all the configured properties to AdminClientFactory to create AdminClient object.
 
Example code: 
 
//create a java.util.Properties object
Properties jmxConnectionProperties = new Properties();
 
jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_TYPE, 
                                           AdminClient.CONNECTOR_TYPE_SOAP);
jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_HOST, host);
jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_PORT, port);
jmxConnectionProperties.setProperty(AdminClient.USERNAME, user);
jmxConnectionProperties.setProperty(AdminClient.PASSWORD, password);
 
//If you’re going to access HTTPS SOAP URL
if(isSSLEnabled) {
jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, 
                     ""+isSSLEnabled);
jmxConnectionProperties.setProperty("javax.net.ssl.trustStore", trustStorePath);
jmxConnectionProperties.setProperty("javax.net.ssl.keyStore", keyStorePath);
jmxConnectionProperties.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
jmxConnectionProperties.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
}
 
 
Create AdminClient object:
 
private AdminClient getAdminClient () {
                                         
AdminClient adminClient = null;
try {
                    adminClient = AdminClientFactory.createAdminClient(jmxConnectionProperties);
} catch (ConnectorException e) {
                    System.out.println("Failed to create Client..." + e);
}
                    return adminClient;
}
 
Accessing MBeans 
 
Once the AdminClient created, you can use it to access managed resources in the application and 
administration servers. Each managed resource registers an MBean with the MBeanServer in its 
process. In JMX, management programs do not directly access MBeans. Instead, they must direct 
requests to the MBeanServer, and reference MBeans by name. An MBean name takes the form of 
an ObjectName, which consists of a domain name followed by an unordered list of one or more 
key properties.
 
Syntax:     [domainName]:property=value[,property=value]*
 
The domain name provides a partitioning of the ObjectName namespace within the JMX agent. 
It is optional; if it is not present, then the MBean server will use a default domain name. 
For WebSphere Application Server, the domain name is conveniently WebSphere.
 
A key property is a name-value pair. An ObjectName's list of key properties can consist of any 
number of key properties, but at least one needs to exist. The ordering of key properties in the 
list does not matter. 
 
The following are the most important property name that WAS an administrations use:
 
type:          The resource type that the MBean represents.
name:       The name identifier for the individual instance of the MBean.
cell:            The name of the cell in which the MBean is executing.
node:        The name of the node in which the MBean is executing.
process: The name of the process in which the MBean is executing.
 
 
Example: 
                    Websphere:* will return all the MBeans in the WebSphere
 
public Set<ObjectName> getNodeAgents(String query)  
                throws ConnectorException {
            ObjectName queryName = null;
            Set<ObjectName> set = null;
            try {
                  queryName = new ObjectName(query);
                  set = getAdminClient().queryNames(queryName, null);
            } catch (MalformedObjectNameException e) {
                  System.out.println(e);
            } catch (NullPointerException e) {
                  System.out.println(e);
            } catch (ConnectorException e) {
                  System.out.println(e);
            }          
            return set;
        }
 
 
Sample source code:
 
WebSphere Admin Client with SOAP and RMI connectors:
 

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.exception.ConnectorException;

/**
 * 
 * @author Hari haran N
 * 
 *
 */


public class WebSphereJMX {
 
 private Properties jmxConnectionProperties = new Properties();
 
 private String user = null;
 private String password = null;
 private String port = null;
 private String host = null;
 private String node = null;
 private String keyStorePath = null;
 private String trustStorePath = null;
 private String trustStorePassword = null;
 private String keyStorePassword = null;
 
 private boolean isSSLEnabled = false;
 private boolean isSOAPConnector = false;
 
 public static void main(String var[]) throws Exception {
  WebSphereJMX obj = new WebSphereJMX(true);
  // "WebSphere:*" will query all the resource details in the application server.
  Set<ObjectName> set = obj.getNodeAgents("WebSphere:*");
  obj.displayResult(set);
  
 }
 
 private void setProperties(WebSphereJMX obj) {
  //set connection properties
  obj.setNode("node1");
  obj.setHost("localhost");
  obj.setUser("root");
  obj.setPassword("root");
  obj.setPort("8880");
  obj.setKeyStorePath("E:/Program Files/IBM/profiles/AppSrv01/etc/DummyClientKeyFile.jks");
  obj.setTrustStorePath("E:/Program Files/IBM/profiles/AppSrv01/etc/DummyClientTrustFile.jks");
  obj.setKeyStorePassword("WebAS");
  obj.setTrustStorePassword("WebAS");
  obj.setSSLEnabled(true);
 }
 
 WebSphereJMX(boolean isSOAPConnector) throws Exception {
  
  setProperties(this);
  
  if(isSOAPConnector) {
   //set SOAP connection properties
   loadSOAPProperties();
  } else { 
   //set RMI connection properties
   loadRMIProperties();
  }
 }
 
 private void loadRMIProperties() {
  //set connection properties for RMI connector
  jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_RMI);
  jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_HOST, "localhost");
  jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_PORT, "9633");
  jmxConnectionProperties.setProperty(AdminClient.USERNAME, "root");
  jmxConnectionProperties.setProperty(AdminClient.PASSWORD, "root");
  System.setProperty("com.ibm.CORBA.ConfigURL",  "file:E:/Program Files/IBM/profiles/AppSrv01/properties/sas.client.props");
  System.setProperty("com.ibm.SSL.ConfigURL",  "file:E:/Program Files/IBM/profiles/AppSrv01/properties/ssl.client.props");
 }

 
 private void loadSOAPProperties() {
  //set connection properties for SOAP connector
  jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
  jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_HOST, host);
  jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_PORT, port);
  jmxConnectionProperties.setProperty(AdminClient.USERNAME, user);
  jmxConnectionProperties.setProperty(AdminClient.PASSWORD, password);
  
  //If https is enabled, add the following properties also.
  if(isSSLEnabled) {
   jmxConnectionProperties.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, ""+isSSLEnabled);
   jmxConnectionProperties.setProperty("javax.net.ssl.trustStore", trustStorePath);
   jmxConnectionProperties.setProperty("javax.net.ssl.keyStore", keyStorePath);
   jmxConnectionProperties.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
   jmxConnectionProperties.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
  }

 }
 
 public AdminClient getAdminClient () {
  
  AdminClient adminClient = null;
  try {
   //create admin client object
   adminClient = AdminClientFactory.createAdminClient(jmxConnectionProperties);
  } catch (ConnectorException e) {
   System.out.println("Failed to create Client..." + e);
  }
  return adminClient;
 }

 public Set<ObjectName> getNodeAgents(String query) {
  ObjectName queryName = null;
  Set<ObjectName> set = null;
  
  try {
   //Create ObjectName
   queryName = new ObjectName(query);
   set = getAdminClient().queryNames(queryName, null); 
  } catch (MalformedObjectNameException e) {
   System.out.println(e);
  } catch (NullPointerException e) {
   System.out.println(e);
  } catch (ConnectorException e) {
   System.out.println(e);
  }
  
  return set;
 }
 
 public void displayResult(Set<ObjectName> set) {
  if (!set.isEmpty()) {
   Iterator<ObjectName> it = set.iterator();
   while(it.hasNext()) {
    ObjectName nodeAgent = it.next();
    System.out.println(nodeAgent.getKeyPropertyListString());
   }
  } else {
   System.out.println("Node agent MBean was not found.");
  }
 }
 
 public String getNode() {
  return node;
 }

 public void setNode(String node) {
  this.node = node;
 }
 
 public Properties getJmxConnectionProperties() {
  return jmxConnectionProperties;
 }

 public void setJmxConnectionProperties(Properties jmxConnectionProperties) {
  this.jmxConnectionProperties = jmxConnectionProperties;
 }

 public String getUser() {
  return user;
 }

 public void setUser(String user) {
  this.user = user;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getPort() {
  return port;
 }

 public void setPort(String port) {
  this.port = port;
 }

 public String getHost() {
  return host;
 }

 public void setHost(String host) {
  this.host = host;
 }

 public String getKeyStorePath() {
  return keyStorePath;
 }

 public void setKeyStorePath(String keyStorePath) {
  this.keyStorePath = keyStorePath;
 }

 public String getTrustStorePath() {
  return trustStorePath;
 }

 public void setTrustStorePath(String trustStorePath) {
  this.trustStorePath = trustStorePath;
 }

 public String getTrustStorePassword() {
  return trustStorePassword;
 }

 public void setTrustStorePassword(String trustStorePassword) {
  this.trustStorePassword = trustStorePassword;
 }

 public String getKeyStorePassword() {
  return keyStorePassword;
 }

 public void setKeyStorePassword(String keyStorePassword) {
  this.keyStorePassword = keyStorePassword;
 }

 public boolean isSSLEnabled() {
  return isSSLEnabled;
 }

 public void setSSLEnabled(boolean isSSLEnabled) {
  this.isSSLEnabled = isSSLEnabled;
 }

 public boolean isSOAPConnector() {
  return isSOAPConnector;
 }

 public void setSOAPConnector(boolean isSOAPConnector) {
  this.isSOAPConnector = isSOAPConnector;
 }

 
} 
 
 

No comments:

Post a Comment