使用 Java 管理扩展远程应用程序编程接口开发 Java 管理扩展客户机程序

可以开发 Java™ 管理扩展 (JMX) 连接器规范和 JMX 远程应用程序编程接口 (API) (JSR 160)。该程序可通过基于因特网 ORB 间协议 (RMI-IIOP) 的远程方法调用来进行通信

开始之前

了解有关 JSR 160、JMX API 和受管 bean (MBean) 的信息。有关 JSR 160 的更多信息,请参阅 JSR 160: Java Management Extensions (JMX) Remote API,网址为 http://www.jcp.org/en/jsr/detail?id=160。有关 JMX API 和 MBean 的更多信息,请查看应用程序编程接口文档。

关于此任务

可以通过管理控制台、wsadmin 实用程序或 Java 管理扩展 (JMX) 编程来管理 WebSphere® Application Server 环境。完成此任务以使用 JMX 远程 API 开发 JMX 远程客户机程序,以便可以通过 JMX 编程来管理您的环境。

过程

  1. 通过 JMXServiceURL 类来指定服务器的 JMX 连接器地址。
    JMX 服务 URL 的值为:
    service:jmx:rmi://" + host + ":" + port + "/jndi/JMXConnector"
    例如,如果目标服务器主机为 sales.xyz.com 并且侦听端口为 1234,那么 JMX 服务 URL 为:
    service:jmx:rmi://sales.xyz.com:1234/jndi/JMXConnector

    可在控制台服务器设置页面的“端口表”中查找 port 的值,或在包括目标服务器的 serverindex.xml 文件中找到。如果该 URL 未指定 host 的值,那么该产品将使用缺省值 localhost。如果该 URL 未指定 port 的值,那么该产品将使用缺省值 2809。

    连接至管理代理程序时,请将管理代理程序 JMX 连接器端口号添加至 URL 结尾。例如,如果管理代理程序 JMX 连接器主机为 sales.xyz.com 并且端口为 6789,请使用以下 URL:
    service:jmx:rmi://sales.xyz.com:6789/jndi/JMXConnector6789
  2. 设置 Java 命名和目录接口 (JNDI) 提供程序 URL 属性以对该产品使用管理名称服务。

    JNDI 提供程序 URL 属性是 javax.naming.Context.PROVIDER_URL。 管理名称服务是 WsnAdminNameService。

  3. 如果客户机使用安全性,请在客户机 Java 虚拟机 (JVM) 中设置 -Dcom.ibm.CORBA.ConfigURL 和 -Dcom.ibm.SSL.ConfigURL 系统属性。

    如果未将 -Dcom.ibm.CORBA.ConfigURL 和 -Dcom.ibm.SSL.ConfigURL 系统属性设置为有效系统属性文件,那么启用安全性后客户机无法正常工作。建议将 JMX 连接器客户机作为管理瘦客户机运行。

    通常,可从安装概要文件目录(最好是目标服务器概要文件目录)复制这些属性文件。

  4. 如果启用了安全性,那么指定服务器的用户标识和密码。
  5. 建立 JMX 连接。
  6. 获取 MBean Server 连接实例。

示例

使用以下瘦客户机代码示例来创建并使用 JMX 客户机。

为了打印方便,某些语句被拆分为几行。

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();

      //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 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();
                System.out.println("NodeAgent mbean found "+ nodeAgent.toString());
         } else {
            System.out.println("Node agent MBean was not found");
            System.exit(-1);
         }
      } catch (Exception e) {
         System.out.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)
            System.out.println(serverName + " was launched");
         else
            System.out.println(serverName + " was not launched");

      } catch (Exception e) {
         System.out.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);
         System.out.println("Registered for event notifications");
      } catch (Exception e) {
         System.out.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++;
      System.out.println("***************************************************");
      System.out.println("* Notification received at " + new Date().toString());
      System.out.println("* type      = " + ntfyObj.getType());
      System.out.println("* message   = " + ntfyObj.getMessage());
      System.out.println("* source    = " + ntfyObj.getSource());
      System.out.println(
                        "* seqNum    = " + Long.toString(ntfyObj.getSequenceNumber()));
      System.out.println("* timeStamp = " + new Date(ntfyObj.getTimeStamp()));
      System.out.println("* userData  = " + ntfyObj.getUserData());
      System.out.println("***************************************************");

   }

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

}

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-base-dist&topic=tjmx_develop_jsr160
文件名:tjmx_develop_jsr160.html