WebSphere Application Server Version 6.1 Feature Pack for Web Services   
             オペレーティング・システム: AIX , HP-UX, i5/OS, Linux, Solaris, Windows, Windows Vista, z/OS

             目次と検索結果のパーソナライズ化

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 APIs、および 管理対象 Bean (MBean) について、基本的な知識があることが前提になります。JSR 160 について詳しくは、http://www.jcp.org/en/jsr/detail?id=160を参照してください。 JMX API の詳細については、JMX API 資料を参照してください。 MBean について詳しくは、MBean API 資料を参照してください。

このタスクについて

管理コンソール、wsadmin ユーティリティー、 または Java Management Extensions (JMX) プログラミングにより WebSphere Application Server 環境を 管理することができます。このトピックでは、JMX プログラミングにより WebSphere Application Server 環境を 管理できるように、JMX リモート API を使用した JMX リモート・クライアント・プログラムの開発方法について説明します。

プロシージャー

  1. JMXServiceURL クラスで、サーバーに JMX コネクター・アドレスを指定します。

    JMXServiceURL クラスの形式は、service:jmx:rmi://host name:port/jndi/JMXConnector です。

  2. セキュリティーが使用可能になっている場合は、サーバーでユーザー ID およびパスワードを指定します。
  3. JMX 接続を確立します。
  4. MBean サーバー接続インスタンスを取得します。

結果

RMI 接続によって WebSphere Application Server への接続が確立され、 ノード・エージェントによって WebSphere Application Server が開始されます。

以下の例は、シン・アプリケーション・クライアント用です。

import java.util.Date;
import java.util.Set;
import java.util.Hashtable;

import javax.management.InstanceNotFoundException;
import javax.management.MalformedObjectNameException;
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 long ntfyCount = 0;

   public static void main(String[] args)
   {
      try {

         JMXRemoteClientApp client = new JMXRemoteClientApp();

         String hostname=args[0];
         String port=args[1];
         String nodeName =args[2];

         client.connect(hostname,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 hostname,String port) throws Exception
   {
      String jndiPath="/jndi/JMXConnector";

      JMXServiceURL url = new JMXServiceURL("service:jmx:iiop://"+hostname+":"+port+jndiPath);

      Hashtable h  = new Hashtable();

      //Specify the user ID and password for the server if security is enabled on server.

      // String[] credentials = new String[] {username ,password };
      //h.put("jmx.remote.credentials", credentials);


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

      System.out.println("Found NodeAgent MBean for node " + nodeName);
   }

   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) {
      }
   }

}



関連タスク
Java Management Extensions リモート・アプリケーション・プログラミング・インターフェースを使用した Java Management Extensions クライアント・プログラムの作成
アプリケーション・クライアントの使用
タスク・トピック    

ご利用条件 | フィードバック

最終更新: Jan 21, 2008 4:10:06 PM EST
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.wsfep.multiplatform.doc/info/ae/ae/tjmx_develop_jsr160.html