WAS JMX 监视应用开发

在使用WebSphere的过程中,我们可以开发自己的监视应用程序,was给我们提供了获取底层数据的三种接口:

1、基于 Java机器扩展 (JMX) 的接口

2、PMI 客户机接口

3、servlet 接口

在 was 7.0 中,官方已不推荐使用 PMI 客户机接口,它建议我们使用 JMX 接口来收集 PMI 数据。

Java管理扩展 JMX(Java Management Extentions) 连接器有以下几种:

远程方法调用 (RMI) 连接器(Java Remote Method Protocol)

简单对象访问协议 (SOAP) 连接器(Simple Object Access Protocol)

(JSR 160)远程应用程序编程接口 (Java Specification Requests)

远程方法调用 (JSR160RMI) 连接器

进程通讯 (IPC)连接器(Inter-Process Communication)、

我们可以通过以上几种方式编写自己的应用程序,这里我们使用SOAP连接器:

首先我们需要从服务器上找到 %WAS_HOME%/plugins/com.ibm.ws.runtime7.0.0.jar 包,导入到类路径中。

当我们开启了控制台的安全性后,我们需要将was默认的证书库和密钥库下载到本地,在通过soap连接was的时候,需要用到。

下面是一段示例代码:

package admin ;

import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.exception.ConnectorException;
import java.util.*;
import javax.management.*;

public class getjvm {
    static ObjectName serverOName = null;
    static ObjectName jvmOName = null;
    public static void main(String[] args){
        AdminClient ac = null;
        Properties props = new Properties();
        props.setProperty(AdminClient.CONNECTOR_HOST, "192.168.137.211");
        props.setProperty(AdminClient.CONNECTOR_PORT, "8880");
        props.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
        props.setProperty(AdminClient.USERNAME, "wasadmin");
        props.setProperty(AdminClient.PASSWORD, "wasadmin");
        props.setProperty("javax.net.ssl.trustStore", "D:/ssl/DummyClientTrustFile.jks");
        props.setProperty("javax.net.ssl.keyStore", "D:/ssl/DummyClientKeyFile.jks");
        props.setProperty("javax.net.ssl.trustStorePassword", "WebAS");
        props.setProperty("javax.net.ssl.keyStorePassword", "WebAS");
       try {
                ac = AdminClientFactory.createAdminClient(props);
        }
                catch (ConnectorException e)
                {
                       System.out.println("Exception creating admin client: " + e);
                }
        ObjectName on = null;
        try {
            on = new ObjectName("WebSphere:*");
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        Set objectNameSet = null;
        try {
            objectNameSet = ac.queryNames(on, null);
        } catch (ConnectorException e) {
            e.printStackTrace();
        }
        System.out.println("adminClient="+ac);
        System.out.println("Connect to DeployManagement is OK!");
        if(objectNameSet != null) {
            Iterator i = objectNameSet.iterator();
            while (i.hasNext()) {
                on = (ObjectName)i.next();
                String type = on.getKeyProperty("type");
                if(type != null && type.equals("Server")) {
                    serverOName = on;
                    Object value_serverName = null;
                    try {
                        value_serverName = ac.getAttribute(serverOName, "name");
                    } catch (AttributeNotFoundException e) {
                        e.printStackTrace();
                    } catch (InstanceNotFoundException e) {
                        e.printStackTrace();
                    } catch (MBeanException e) {
                        e.printStackTrace();
                    } catch (ReflectionException e) {
                        e.printStackTrace();
                    } catch (ConnectorException e) {
                        e.printStackTrace();
                    }
                    System.out.println("serverName =" + value_serverName);
                }           
                if(type != null && type.equals("JVM")) {
                    jvmOName = on;
                    Object value_freeMemory = null;
                    Object value_heapSize = null;
                try {
                    value_freeMemory = ac.getAttribute(jvmOName, "freeMemory");
                    value_heapSize = ac.getAttribute(jvmOName, "heapSize");
                    } catch (AttributeNotFoundException e) {
                            e.printStackTrace();
                    } catch (InstanceNotFoundException e) {
                            e.printStackTrace();
                    } catch (MBeanException e) {
                            e.printStackTrace();
                    } catch (ReflectionException e) {
                            e.printStackTrace();
                    } catch (ConnectorException e) {
                            e.printStackTrace();
                    }

                    System.out.println("freeMemory =" + value_freeMemory);
                    System.out.println("heapSize =" + value_heapSize);              
                    }
            }
        }
        else {
            System.err.println("ERROR: not found object names!");
        }

}
}




你可能感兴趣的:(WebSphere)