利用基于 JMX 的 AdminClient 获取 WebSphere Application Server 的环境参数

在开发基于的 WebSphere Application Server 的应用时,有时会需要获取 WAS 的
环境参数,如应用程序配置的数据源,JVM 的参数设置等等。WAS 提供了 AdminClient,
一个基于 JMX 的程序包。它通过连接 WAS 的 SOAP 端口读取所需的信息。

package com.ibm.admin.config.check;

import java.util.Properties;

import javax.management.InstanceNotFoundException;
import javax.management.ObjectName;

import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.configservice.ConfigService;
import com.ibm.websphere.management.configservice.ConfigServiceProxy;
import com.ibm.websphere.management.exception.ConfigServiceException;
import com.ibm.websphere.management.exception.ConnectorException;

public class AdminClientExample
{
       AdminClient adminClient = null;
       ConfigService configService = null;
 
       public void createConncetion()
       {
              // Set up a Properties object for the JMX connector attributes
              String wasInstallLocation = "/opt/IBM/WebSphere/AppServer";
              String trustStore = "/profiles/dwe/etc/DummyClientTrustFile.jks";
              String keyStore = "/profiles/dwe/etc/DummyClientKeyFile.jks";
              String trustStorePW = "WebAS";
              String keyStorePW = "WebAS";

              Properties connectProps = new Properties();

              connectProps.setProperty( AdminClient.USERNAME, "administrator" );
              connectProps.setProperty( AdminClient.PASSWORD, "passw0rd" );
              connectProps.setProperty( AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP );
              connectProps.setProperty( AdminClient.CONNECTOR_HOST, "localhost" );
              connectProps.setProperty( AdminClient.CONNECTOR_PORT, "" + "8880" );
              connectProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "true");
              connectProps.setProperty( "javax.net.ssl.trustStore", wasInstallLocation + trustStore );
              connectProps.setProperty( "javax.net.ssl.keyStore", wasInstallLocation  + keyStore );
              connectProps.setProperty( "javax.net.ssl.trustStorePassword", trustStorePW );
              connectProps.setProperty( "javax.net.ssl.keyStorePassword", keyStorePW );
  
              // create a new connection
              try
              {
                     adminClient = AdminClientFactory.createAdminClient( connectProps );
              }
              catch (ConnectorException e)
              {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
       }
 
       public String getGenericJVMArguments()
       {
              String genericJvmArgs = "";
  
              try
              {
                     configService = new ConfigServiceProxy( adminClient );
         
                     ObjectName server = configService.resolve( null, null, "Server" )[0];
                     ObjectName javavm = configService.resolve( null, server, "JavaVirtualMachine" )[0];
   
                     genericJvmArgs = configService.getAttribute( null,  javavm, "genericJvmArguments" ).toString();
              }
              catch (InstanceNotFoundException e)
              {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
              catch (ConfigServiceException e)
              {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
              catch (ConnectorException e)
              {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
  
              return genericJvmArgs;
       }

       public static void main(String[] args)
       {
              AdminClientExample adminClientExamp = new AdminClientExample();
              adminClientExamp.createConncetion();
              String jvmArgs = adminClientExamp.getGenericJVMArguments();
              System.out.println(jvmArgs);
       }
}

posted @ 2009-05-31 17:52  coding_rabbit  阅读(959)  评论(1编辑  收藏  举报