JMX によるカスタム管理ユーティリティの開発

     前  次    新しいウィンドウで目次を開く     
ここから内容の開始

JMX を使用した WebLogic Server MBean へのアクセス

以下の節では、JMX クライアントから WebLogic Server MBean にアクセスする方法について説明します。

 


リモート クライアントのクラスパスの設定

JMX クライアントが独自の JVM (つまり、WebLogic Server インスタンスでない JVM) で実行される場合は、クライアントのクラスパスに次の JAR ファイルを含めます。

WL_HOME\lib\wljmxclient.jar

WL_HOME は、WebLogic Server のインストール先ディレクトリです。

この JAR には、HTTP プロトコルと IIOP プロトコルの BEA 実装と、独自の T3 プロトコルが含まれます。BEA 実装を使用する場合は、JMX クライアントが接続要求とともにログイン資格を送信し、WebLogic Server のセキュリティ フレームワークがクライアントを認証します。認証されたクライアントだけが、WebLogic Server MBean サーバに登録されている MBean にアクセスできます。

注意 : HTTP プロトコルと IIOP プロトコルの BEA 実装または独自の T3 プロトコルを使用することをお勧めしますが、JMX クライアントでは標準の JDK で定義されている IIOP プロトコルを使用することもできます。「JDK クラスのみを使用したリモート接続」を参照してください。

 


MBean サーバへのリモート接続の作成

各 WebLogic Server ドメインには 3 種類の MBean サーバが含まれており、それぞれの MBean サーバが別々の MBean 階層へのアクセスを提供します。「MBean サーバ」を参照してください。

WebLogic MBean サーバに接続するには、次の手順に従います。

  1. javax.management.remote.JMXServiceURL オブジェクトを作成して、MBean サーバのアドレスを記述します。
  2. 以下のパラメータ値をコンストラクタに渡します (J2SE 5.0 API 仕様の JMXServiceURL を参照)。

    • MBean サーバとの通信に使用するプロトコルとして、以下の値からいずれか 1 つ。
      t3t3shttphttpsiiopiiops
    • MBean サーバをホストする WebLogic Server インスタンスのリスン アドレス。
    • WebLogic Server インスタンスのリスン ポート。
    • MBean サーバの絶対 JNDI 名。JNDI 名の先頭には /jndi/ を指定し、その後には表 4-1 で説明する JNDI 名のいずれかを続ける必要があります。
      表 4-1 WebLogic MBean サーバの JNDI 名
      MBean サーバ
      JNDI 名
      ドメイン実行時 MBean サーバ
      weblogic.management.mbeanservers.domainruntime
      実行時 MBean サーバ
      weblogic.management.mbeanservers.runtime
      編集 MBean サーバ
      weblogic.management.mbeanservers.edit
  3. javax.management.remote.JMXConnector オブジェクトを作成します。このオブジェクトには、JMX クライアントが MBean サーバへの接続に使用するメソッドがあります。
  4. JMXConnector のコンストラクタ メソッドは、javax.management.remote.JMXConnectorFactory.
    connector(JMXServiceURL
    serviceURL, Map<String,?> environment) です。

    以下のパラメータ値をコンストラクタに渡します (J2SE 5.0 API 仕様の JMXConnectorFactory を参照)。

    • 前の手順で作成した JMXServiceURL オブジェクト。
    • 以下の名前と値のペアを含むハッシュ マップ。
    • javax.naming.Context.SECURITY_PRINCIPAL, admin-user-name

      javax.naming.Context.SECURITY_CREDENTIALS, admin-user-password

      javax.management.remote.JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote"

      weblogic.management.remote パッケージは、WebLogic MBean サーバへの接続に使用できるプロトコルを定義します。リモート JMX クライアントは、クラスパスにこのパッケージのクラスを含める必要があります。「リモート クライアントのクラスパスの設定」を参照してください。

      必要に応じて、以下の名前と値のペアをハッシュ マップに含めます。

      jmx.remote.x.request.waiting.timeout, milliseconds
      milliseconds は、JMX クライアントが MBean サーバ メソッドの呼び出しが返されるまで待機するミリ秒数が格納された java.lang.Long オブジェクトです。メソッドがタイムアウト期間の終了までに返されない場合、クライアントは次の命令セットに移動します。デフォルトでは、クライアントはメソッドが返されるのを無期限に待機します。MBean サーバが呼び出しを完了できない場合には、JMX クライアントは無期限にハングします。

  5. JMXConnector.getMBeanServerConnection() メソッドを呼び出して、WebLogic MBean サーバに接続します。
  6. このメソッドは、javax.management.MBeanServerConnection 型のオブジェクトを返します。

    MBeanServerConnection オブジェクトは、WebLogic MBean サーバへの接続であり、ローカル接続にもリモート接続にも使用できます。J2SE 5.0 API 仕様の「MBeanServerConnection」を参照してください。

  7. クライアントが作業を終了したら、JMXConnector.close() メソッドを呼び出して MBean サーバとの接続を閉じることをお勧めします。

例 : ドメイン実行時 MBean サーバへの接続

コード リスト 4-1 のコードに関する以下の点に留意してください。

ベスト プラクティス : MBean サーバの選択

WebLogic Server ドメインは 3 種類の MBean サーバを保持しており、それぞれの MBean サーバが特定の機能を実行します。以下のように、実行しようとしているタスクをサポートしている MBean サーバを介して MBean にアクセスします。

JDK クラスのみを使用したリモート接続

リモート JMX クライアントからの接続では WebLogic Server クラスを使用することをお勧めします。ただし、リモート JMX クライアントで JDK のクラスのみを使用して WebLogic Server JMX エージェントに接続することもできます。そのためには、次の手順に従います。

  1. MBean をホストする WebLogic Server インスタンスの IIOP プロトコルを有効にします。管理者特権を持つ WebLogic Server ユーザになるように、デフォルトの IIOP ユーザをコンフィグレーションします。
  2. Administration Console オンライン ヘルプの「IIOP の有効化とコンフィグレーション」を参照してください。

  3. JMX クライアントで、javax.management.JMXConnector オブジェクトを次のように作成します。
  4. String hostname = "WLS-host"
    int port =
    WLS-port
    String protocol = "rmi";
    String jndiroot= new String("/jndi/iiop://" + hostname + ":" +
           port + "/");
    String mserver = "
    MBean-server-JNDI-name";

    JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port,
          jndiroot + mserver);

    Hashtable h = new Hashtable();
    h.put(Context.SECURITY_PRINCIPAL, username);
    h.put(Context.SECURITY_CREDENTIALS, password);

    connector = JMXConnectorFactory.connect(serviceURL, h);

    WLS-hostWLS-port は WebLogic Server インスタンスのリスン アドレスとリスン ポートであり、MBean-server-JNDI-name表 4-1 に示した値のいずれかです。

作成するハッシュ テーブルには、プロトコル パッケージの名前が含まれていません。この値を null のままにしておくことで、JMX クライアントは JDK の com.sun.jmx.remote.protocol パッケージからプロトコル定義を使用します。

 


実行時 MBean サーバへのローカル接続の作成

ローカル クライアントは、JMXServiceURL オブジェクトを作成するのではなく、JNDI ツリーを介して WebLogic Server インスタンスの実行時 MBean サーバにアクセスできます。実行時 MBean サーバのみが JNDI ツリーに自身を登録します。

JNDI からアクセスされると、実行時 MBean サーバはその javax.management.MBeanServer インタフェースを返します。このインタフェースには、MBeanServerConnection インタフェースのすべてのメソッドと、ローカル プロセスがカスタム MBean の登録に使用する registerMBean() などの追加のメソッドがあります (J2SE 5.0 API 仕様の MBeanServer を参照)。

JMX クライアントのクラスがエンタープライズ アプリケーションの最上位にある場合 (つまり、それらのクラスがアプリケーションの APP-INF ディレクトリからデプロイされている場合)、実行時 MBean サーバの JNDI 名は次のようになります。
java:comp/jmx/runtime

JMX クライアントのクラスが、EJB または Web アプリケーションなどの Java EE モジュールにある場合、実行時 MBean サーバの JNDI 名は次のようになります。
java:comp/env/jmx/runtime

次に例を示します。
InitialContext ctx = new InitialContext();
server = (MBeanServer)ctx.lookup("java:comp/env/jmx/runtime");

 


MBean 階層内の移動

WebLogic Server は、MBean を階層データ モデルにまとめます (「WebLogic Server MBean のデータ モデル」を参照)。このモデルでは、すべての親 MBean に自身の子のオブジェクト名を保持する属性が含まれています。標準の JMX API で子のオブジェクト名を使用して、子 MBean の属性の値を取得または設定したり、そのメソッドを呼び出したりします。

WebLogic Server MBean 階層を移動するには、次の手順に従います。

  1. MBean サーバへの接続を開始します。
  2. 上記の節「MBean サーバへのリモート接続の作成」を参照してください。

    接続を開始すると、javax.management.MBeanServerConnection 型のオブジェクトが返されます。

  3. MBeanServerConnection.getAttribute(ObjectName object-name,
    String
    attribute) メソッドを呼び出して、MBean 階層のルートにある MBean のオブジェクト名を取得します。各値の説明は次のとおりです。
    • object-name は、MBean サーバに登録されているサービス MBean のオブジェクト名 (「サービス MBean」を参照)。
    • 表 2-3 には、各種の MBean サーバで使用可能なサービス MBean のタイプが示されています。

    • attribute は、ルート MBean を保持するサービス MBean 属性の名前。
  4. 次のようなコードを連続して呼び出します。
    ObjectName on =
    MBeanServerConnection
    .getAttribute(object-name, attribute)
    各値の説明は次のとおりです。
    • object-name は、MBean 階層内の現在のノード (MBean) のオブジェクト名。
    • attribute は、子 MBean の 1 つまたは複数のインスタンスを保持する、現在の MBean の属性の名前。属性に複数の子がある場合は、その出力をオブジェクト名の配列 ObjectName[] に割り当てます。

MBean 階層での MBean の場所を判別するには、『WebLogic Server MBean リファレンス』でその MBean の説明を参照してください。『WebLogic Server MBean リファレンス』では、各 MBean について、現在の MBean のファクトリ メソッドを保持する親 MBean が記載されています。ファクトリ メソッドがパブリックでない MBean については、『WebLogic Server MBean リファレンス』では、現在の MBean にアクセスできる他の MBean が記載されています。

 


例 : サーバの名前と状態の出力

コード リスト 4-2 のサンプル コードでは、ドメイン実行時 MBean サーバに接続し、DomainRuntimeServiceMBean を使用してドメイン内の各 ServerRuntimeMBean のオブジェクト名を取得します。その後、各サーバの ServerRuntimeMBeanName 属性および State 属性の値を取得し、出力します。

コード リスト 4-2 のコードに関する以下の点に留意してください。

 


例 : サーブレットのモニタ

Web アプリケーションの各サーブレットは、サーブレットの実行時状態に関する情報を保持する ServletRuntimeMBean のインスタンスを提供します (『WebLogic Server MBean リファレンス』の「ServletRuntimeMBean」を参照)。

WebLogic Server のデータ モデルでは、ServletRuntimeMBean へのパスは次のようになります。

  1. ドメイン実行時 MBean サーバ (ドメイン内の全サーバ上にあるすべてのサーブレット用)、または特定のサーバ インスタンス上の実行時 MBean サーバ。
  2. DomainRuntimeServiceMBean または RuntimeServiceMBeanServerRuntimes 属性。
  3. ServerRuntimeMBeanApplicationRuntimes 属性。
  4. ApplicationRuntimeMBeanComponentRuntimes 属性。
  5. ComponentRuntimes 属性には、さまざまなタイプのコンポーネントの実行時 MBean が保持されます。それらの 1 つが WebAppComponentRuntimeMBean です。この属性の値を取得したら、子 MBean の Type 属性を使用して特定のタイプのコンポーネントの実行時 MBean を取得します。

  6. WebAppComponentRuntimeMBeanServletRuntimes 属性。

コード リスト 4-3 のコードでは、上記の節で説明したように階層を移動し、ServletRuntimeMBean の値を取得します。

コード リスト 4-3 サーブレットのモニタ
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
public class MonitorServlets {
   private static MBeanServerConnection connection;
   private static JMXConnector connector;
   private static final ObjectName service;
   // クラス内で使用できるように
   // DomainRuntimeServiceMBean のオブジェクト名を初期化する
   static {
      try {
         service = new ObjectName(
          "com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanser
           vers.domainruntime.DomainRuntimeServiceMBean");
      }catch (MalformedObjectNameException e) {
         throw new AssertionError(e.getMessage());
      }
   }
   /*
   * ドメイン実行時 MBean サーバへの接続を初期化する
   */
   public static void initConnection(String hostname, String portString,
      String username, String password) throws IOException,
      MalformedURLException {
      String protocol = "t3";
      Integer portInteger = Integer.valueOf(portString);
      int port = portInteger.intValue();
      String jndiroot = "/jndi/";
      String mserver = "weblogic.management.mbeanservers.domainruntime";
      JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
         port, jndiroot + mserver);
      Hashtable h = new Hashtable();
      h.put(Context.SECURITY_PRINCIPAL, username);
      h.put(Context.SECURITY_CREDENTIALS, password);
      h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
         "weblogic.management.remote");
      connector = JMXConnectorFactory.connect(serviceURL, h);
      connection = connector.getMBeanServerConnection();
   }
   /*
   * ServerRuntimeMBean の配列を取得する
   */
   public static ObjectName[] getServerRuntimes() throws Exception {
      return (ObjectName[]) connection.getAttribute(service,
         "ServerRuntimes");
   }
   /*
   * WebApplicationComponentRuntimeMBean の配列を取得する
   */
   public void getServletData() throws Exception {
      ObjectName[] serverRT = getServerRuntimes();
      int length = (int) serverRT.length;
      for (int i = 0; i < length; i++) {
         ObjectName[] appRT =
            (ObjectName[]) connection.getAttribute(serverRT[i],
            "ApplicationRuntimes");
         int appLength = (int) appRT.length;
         for (int x = 0; x < appLength; x++) {
            System.out.println("Application name: " +
              (String)connection.getAttribute(appRT[x], "Name"));
            ObjectName[] compRT =
               (ObjectName[]) connection.getAttribute(appRT[x],
               "ComponentRuntimes");
            int compLength = (int) compRT.length;
            for (int y = 0; y < compLength; y++) {
               System.out.println(" Component name: " +
                 (String)connection.getAttribute(compRT[y], "Name"));
               String componentType =
                  (String) connection.getAttribute(compRT[y], "Type");
               System.out.println(componentType.toString());
               if (componentType.toString().equals("WebAppComponentRuntime")){
                  ObjectName[] servletRTs = (ObjectName[])
                     connection.getAttribute(compRT[y], "Servlets");
                  int servletLength = (int) servletRTs.length;
                  for (int z = 0; z < servletLength; z++) {
                     System.out.println(" Servlet name: " +
                        (String)connection.getAttribute(servletRTs[z],
                         "Name"));
                     System.out.println(" Servlet context path: " +
                        (String)connection.getAttribute(servletRTs[z],
                         "ContextPath"));
                     System.out.println(" Invocation Total Count : " +
                        (Object)connection.getAttribute(servletRTs[z],
                         "InvocationTotalCount"));
                  }
               }
            }
         }
      }
   }
   public static void main(String[] args) throws Exception {
      String hostname = args[0];
      String portString = args[1];
      String username = args[2];
      String password = args[3];
      MonitorServlets s = new MonitorServlets();
      initConnection(hostname, portString, username, password);
      s.getServletData();
      connector.close();
   }
}

ページの先頭       前  次