Java Management Extensions リモート・アプリケーション・プログラミング・インターフェースを使用した Java Management Extensions クライアント・プログラムの開発

Java™ Management Extensions (JMX) コネクター仕様および JMX リモート・アプリケーション・プログラミング・インターフェース (API) (JSR 160) を開発することができます。このプログラムでは、Remote Method Invocation over Internet Inter-ORB Protocol (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 Management Extensions (JMX) プログラミングにより WebSphere® Application Server 環境を管理することができます。このタスクを実行して、JMX リモート API を使用する JMX リモート・クライアント・プログラムを作成すると、JMX プログラミングによってご使用の環境を管理できるようになります。

手順

  1. JMXServiceURL クラスで、サーバーに JMX コネクター・アドレスを指定します。
    JMX サービス URL の値は次のとおりです。
    service:jmx:rmi://" + host + ":" + port + "/jndi/JMXConnector"
    例えば、 ターゲット・サーバー・ホストが sales.xyz.com で、 listen ポートが 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 Naming and Directory Interface (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. セキュリティーが使用可能になっている場合は、サーバーでユーザー ID およびパスワードを指定します。
  5. JMX 接続を確立します。
  6. MBean サーバー接続インスタンスを取得します。

例

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