ヘッダーをスキップ
Oracle® Fusion Middleware Oracle WebLogic Server JMXによるカスタム管理ユーティリティの開発
11g リリース1(10.3.6)
B61630-04
  ドキュメント・ライブラリへ移動
ライブラリ
製品リストへ移動
製品
目次へ移動
目次

前
 
次
 

4 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プロトコルのOracle実装が含まれます。Oracle実装を使用する場合は、JMXクライアントが接続リクエストとともにログイン資格証明を送信し、WebLogic Serverのセキュリティ・フレームワークがクライアントを認証します。認証されたクライアントだけが、WebLogic Server MBeanサーバーに登録されているMBeanにアクセスできます。


注意:

HTTPプロトコルとIIOPプロトコルのOracle実装を使用することをお薦めしますが、JMXクライアントでは標準のJDKで定義されているIIOPプロトコルを使用することもできます。「JDKクラスのみを使用したリモート接続」を参照してください。T3プロトコルが指定されている場合、IIOPを使用するようクライアントで自動的に変換されます。

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

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

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

  1. javax.management.remote.JMXServiceURLオブジェクトを作成して、MBeanサーバーのアドレスを記述します。

    次のパラメータ値をコンストラクタに渡します(http://download.oracle.com/javase/6/docs/api/javax/management/remote/JMXServiceURL.htmlJava SE 6.0 API仕様JMXServiceURLを参照してください)。

    • MBeanサーバーとの通信に使用するプロトコルとして、以下の値からいずれか1つ。

      t3, t3s, http, https, iiop, iiops 
      
    • 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
    

  2. javax.management.remote.JMXConnectorオブジェクトを作成します。このオブジェクトには、JMXクライアントがMBeanサーバーへの接続に使用するメソッドがあります。

    JMXConnectorのコンストラクタ・メソッドは、以下のとおりです。

    javax.management.remote.JMXConnectorFactory.
    connector(JMXServiceURL serviceURL, Map<String,?> environment) 
    

    次のパラメータ値をコンストラクタに渡します(http://download.oracle.com/javase/6/docs/api/javax/management/remote/JMXConnectorFactory.htmlJava SE 6.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クライアントは無期限にハングします。

  3. JMXConnector.getMBeanServerConnection()メソッドを呼び出して、WebLogic MBeanサーバーに接続します。

    このメソッドは、javax.management.MBeanServerConnection型のオブジェクトを返します。

    MBeanServerConnectionオブジェクトは、WebLogic MBeanサーバーへの接続です。このオブジェクトは、ローカルおよびリモートの接続に使用できます。Java SE 6.0 API仕様MBeanServerConnection(http://download.oracle.com/javase/6/docs/api/javax/management/MBeanServerConnection.html)を参照してください。

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

例:ドメイン・ランタイムMBeanサーバーへの接続

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

  • クラスは、グローバル変数connectionおよびconnectorを使用して、MBeanサーバーへの接続を表します。connection変数およびconnector変数に値を割り当てるinitConnection()メソッドは、クラス内で再利用できる1つの接続を確立するためにクラス・インスタンスごとに一度だけ呼び出される必要があります。

  • initConnection()メソッドは、クラスがインスタンス化されるときに渡される引数として、(サーバーのリスニング・アドレスとリスニング・ポートとともに)ユーザー名とパスワードを取ります。暗号化されていないユーザー資格証明がコード内に含まれないようにするため、この方法をお薦めします。これらの引数を保持するStringオブジェクトは破棄され、JVMのガベージ・コレクション・ルーチンによってメモリーから削除されます。

  • クライアントは、jmx.remote.x.request.waiting.timeout環境パラメータを10000に設定するので、MBeanサーバー・メソッド呼出しはすべて、呼出し後10000ミリ秒以内にメソッドが返されなければ、タイムアウトします。

  • クラスが作業を終了したら、JMXConnector.close()メソッドを呼び出してMBeanサーバーとの接続を閉じる(Java SE 6.0 API仕様JMXConnector(http://download.oracle.com/javase/6/docs/api/javax/management/remote/JMXConnector.html)を参照してください)。

例4-1 ドメイン・ランタイムMBeanサーバーへの接続

public class MyConnection {

   private static MBeanServerConnection connection;
   private static JMXConnector connector;
   private static final ObjectName service;
   /*
   * Initialize connection to the Domain Runtime MBean Server.
   */
   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");
      h.put("jmx.remote.x.request.waiting.timeout", new Long(10000));
      connector = JMXConnectorFactory.connect(serviceURL, h);
      connection = connector.getMBeanServerConnection();
   }

   public static void main(String[] args) throws Exception {
      String hostname = args[0];
      String portString = args[1];
      String username = args[2];
      String password = args[3];

      MyConnection c= new MyConnection();
      initConnection(hostname, portString, username, password);
...
      connector.close();
   }
}

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

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

  • ドメインの構成を変更するには、編集MBeanサーバーを使用します。

  • 保留中の構成MBeanの階層に対する変更をモニターするには、編集MBeanサーバーを使用します。

  • アクティブな構成MBeanのみをモニターする(ランタイムMBeanはモニターしない)ためには、ランタイムMBeanサーバーを使用します。

    ランタイムMBeanサーバーを使用したモニターのほうが、ドメイン・ランタイムMBeanサーバーを使用したモニターより、使用するメモリーとネットワーク・トラフィックの量が小さくなります。(WebLogic Serverは、クライアントが接続を要求するまでドメイン・ランタイムMBeanサーバーを初期化しません)。

    ほとんどの場合、ドメイン内のすべてのサーバー・インスタンスは同じ構成データのセットを保持しているため、管理サーバーまたは管理対象サーバーどちらのランタイムMBeanサーバーをモニターするかは重要ではありません。ただし、サーバーを再起動しないと消費できない変更を行うと、サーバーは変更を受け入れなくなり、そのサーバーの構成データが古くなるおそれがあります。この場合、このサーバーのランタイムMBeanサーバーをモニターすることは、その特定のサーバー・インスタンスの構成のみをモニターすることになります。WebLogic Serverドメインの変更、および変更のアクティブ化のプロセスを理解するには、『Oracle WebLogic Serverドメイン構成の理解』の構成の変更の管理に関する項を参照してください。

  • クライアントが複数のサーバーのランタイムMBeanをモニターする場合や、クライアントが別のJVMで実行されている場合は、ドメイン内の各サーバー・インスタンス上のそれぞれのランタイムMBeanサーバーに個別に接続するのではなく、管理サーバー上のドメイン・ランタイムMBeanサーバーに接続することをお薦めします。

    ドメイン・ランタイムMbeanサーバーのMbeanにJMXリスナーとフィルタを登録する場合は、JMXフィルタを、それがモニターするMBeanと同じJVMで実行します。たとえば、ある管理対象サーバー上のMBeanにフィルタを登録する場合は、フィルタをその管理対象サーバーで実行し、フィルタ条件を満たすメッセージのみをリスナーに転送します。

    一般的に、ドメイン・ランタイムMBeanサーバーを使用するコードのほうが、各サーバー・インスタンス上のランタイムMBeanサーバーを使用するコードより管理がしやすく安全性も高くなっています。その理由は以下のとおりです。

    • コードで必要なのは、管理サーバー上のドメイン・ランタイムMBeanサーバーに接続するための1つのURLを作成することだけです。以後、コードですべてのサーバー・インスタンスの値をルックアップしたり、必要に応じて結果をフィルタ処理したりできます。

    • コードでランタイムMBeanサーバーを使用して複数のサーバー・インスタンス上のMBeanの値を読み込む場合には、各サーバー・インスタンス用のURLを作成する必要があり、URLごとに一意のリスニング・アドレスとリスニング・ポートの組合せが必要になります。

    • 管理サーバーのセキュアな管理ポートを介してWebLogic Serverドメイン内のすべての管理トラフィックをルーティングでき、ファイアウォールを使用して管理対象サーバーの管理ポートへのファイアウォール外部からの接続を回避できます。

    ドメイン・ランタイムMBeanサーバーを使用してすべてのJMXリクエストを転送することによるトレード・オフとして、ネットワーク・レイテンシとメモリー使用量の増加のためにパフォーマンスが若干低下します。MBean値を読み込むために各管理対象サーバーのランタイムMBeanサーバーに直接接続すると、管理対象サーバーから値を取得するためにドメイン・ランタイムMBeanサーバーが作成するネットワーク・ホップが削除されます。ただし、大半のネットワーク・トポロジとパフォーマンス要件では、ドメイン・ランタイムMBeanサーバーによって有効化される簡略化されたコード・メンテナンスおよび強化セキュリティが推奨されます。

図4-1 ドメイン・ランタイムMBeanサーバーとランタイムMBeanサーバー

図4-1の説明が続きます
「図4-1 ドメイン・ランタイムMBeanサーバーとランタイムMBeanサーバー」の説明

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

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

  1. wljmxclient.jarwlclient.jarクライアント・クラスパスに指定しない場合、

    1. MBeanをホストするWebLogic ServerインスタンスのIIOPプロトコルを有効にします。

    2. 管理者権限を持つWebLogic Serverユーザーになるように、デフォルトのIIOPユーザーを構成します。

    管理コンソール・オンライン・ヘルプの「IIOPの有効化と構成」を参照してください。

    wljmxclient.jarwlclient.jarクライアント・クラスパスに指定した場合、デフォルトのIIOPユーザーを有効にする必要はありません。ステップ2に進みます。


    注意:

    wlclient.jarwljmxclient.jarのMANIFEST ClassPathエントリにあるので、wlclient.jarwljmxclient.jarは同じディレクトリにあるか、または両方のjarはクラスパスで指定する必要があります。

    クラスパスにwljmxclient.jarがある場合、weblogic.jarまたはwlfullclient.jarは、クラスパスに含まれていないことを確認してください。シン・クライアントwljmxclient.jar/wlclient.jarまたはシック・クライアントwlfullclient.jarのみ使用し、両方のクライアントを組み合せて使用しないでください。


  2. JMXクライアントで、javax.management.JMXConnectorオブジェクトを次のように作成します。

    String hostname = "WLS-host" 
    int port = WLS-port 
    String protocol = "iiop";
    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ツリーを介してWebLogic Serverのドメイン・ランタイムMBeanサーバーにもアクセスできます。

JNDIからアクセスされると、ランタイムMBeanサーバーはそのjavax.management.MBeanServerインタフェースを返します。このインタフェースには、MBeanServerConnectionインタフェースのすべてのメソッドと、ローカル・プロセスがカスタムMBeanの登録に使用するregisterMBean()などの追加のメソッドがあります。(Java SE 6.0 API仕様MBeanServer(http://download.oracle.com/javase/6/docs/api/javax/management/MBeanServer.html)を参照してください)。

JMXクライアントのクラスが、EJBまたはWebアプリケーションなどのJava EEモジュールにある場合、ランタイムMBeanServerのJNDI名は次のようになります。

java:comp/env/jmx/runtime

例:

InitialContext ctx = new InitialContext();
server = (MBeanServer)ctx.lookup("java:comp/env/jmx/runtime"); 

JMXクライアントのクラスがJava EEモジュールの一部ではない場合、ランタイムMBeanサーバーのJNDI名は次のようになります。

java:comp/jmx/runtime 

ドメイン・ランタイムMBeanサーバーへのローカル接続の作成

ローカル・クライアントは、JMXServiceURLオブジェクトを作成するのではなく、JNDIツリーを介してWebLogic Serverインスタンスのドメイン・ランタイムMBeanサーバーにアクセスできます。

JNDIからアクセスされると、ドメイン・ランタイムMBeanサーバーはそのjavax.management.MBeanServerインタフェースを返します。このインタフェースには、MBeanServerConnectionインタフェースのすべてのメソッドと、ローカル・プロセスがカスタムMBeanの登録に使用するregisterMBean()などの追加のメソッドの他にgetMBeanCount()instatiate()、とgetClassLoader()などのメソッドもあります。(Java SE 6.0 API仕様MBeanServer(http://download.oracle.com/javase/6/docs/api/javax/management/MBeanServer.html)を参照してください)。


注意:

ベスト・プラクティスとして、Oracleでは、ドメイン・ランタイムMBeanサーバーをドメイン全体の操作を実行するMBeanにのみ使用することをお薦めします。MBeanの処理とネットワーク・アクティビティにより管理サーバーの処理速度が低下し、管理操作の開始が回避されていないことを確認してください。

JMXクライアントのクラスが、EJBまたはWebアプリケーションなどのJava EEモジュールにある場合、ドメイン・ランタイムMBeanServerのJNDI名は次のようになります。

java:comp/env/jmx/domainRuntime

例:

InitialContext ctx = new InitialContext();
server = (MBeanServer)ctx.lookup("java:comp/env/jmx/domainRuntime"); 

JMXクライアントのクラスがJava EEモジュールの一部ではない場合、ドメイン・ランタイムMBeanサーバーのJNDI名は次のようになります。

java:comp/jmx/domainRuntime 

ドメイン・ランタイムMBeanサーバーは、管理サーバー上にのみ存在します。呼出しctx.lookup()は、ローカルMBeanServerに参照を返すため、ルックアップ・メソッドは管理サーバー上で実行する場合にのみ呼び出すことができます。管理対象サーバー上で実行するときに呼び出された場合、NameNotFound例外がスローされます。

MBean階層内の移動

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

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

  1. MBeanサーバーへの接続を開始します。

    上記の項「MBeanサーバーへのリモート接続の作成」を参照してください。

    接続を開始すると、次の型のオブジェクトを返します。

    javax.management.MBeanServerConnection
    
  2. MBeanServerConnection.getAttribute(ObjectName object-name, String attribute)メソッドを呼び出して、MBean階層のルートにあるMBeanのオブジェクト名を取得します。各値の説明は次のとおりです。

    • object-nameは、MBeanサーバーに登録されているサービスMBeanのオブジェクト名。(「サービスMBean」を参照してください。)

      表2-3には、各種のMBeanサーバーで使用可能なサービスMBeanのタイプが示されています。

    • attributeは、ルートMBeanを含むサービスMBean属性の名前。

  3. 次のようなコードを連続して呼び出します。

    ObjectName on = 
    MBeanServerConnection.getAttribute(object-name, attribute) 
    

    説明:

    • object-nameは、MBean階層内の現在のノード(MBean)のオブジェクト名。

    • attributeは、子MBeanの1つまたは複数のインスタンスを含む、現在のMBeanの属性の名前。属性に複数の子がある場合は、その出力をオブジェクト名の配列ObjectName[]に割り当てます。

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

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

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

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

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

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 PrintServerState {

   private static MBeanServerConnection connection;
   private static JMXConnector connector;
   private static final ObjectName service;

   // Initializing the object name for DomainRuntimeServiceMBean
   // so it can be used throughout the class.
   static {
      try {
         service = new ObjectName(
            "com.bea:Name=DomainRuntimeService,Type=weblogic.management.
             mbeanservers.domainruntime.DomainRuntimeServiceMBean");
      }catch (MalformedObjectNameException e) {
         throw new AssertionError(e.getMessage());
      }
   }

   /*
   * Initialize connection to the Domain Runtime MBean Server
   */
   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();
   }

   /* 
   * Print an array of ServerRuntimeMBeans.
   * This MBean is the root of the runtime MBean hierarchy, and
   * each server in the domain hosts its own instance.
   */
   public static ObjectName[] getServerRuntimes() throws Exception {
      return (ObjectName[]) connection.getAttribute(service,
         "ServerRuntimes");
   }

   /* 
   * Iterate through ServerRuntimeMBeans and get the name and state
   */
   public void printNameAndState() throws Exception {
      ObjectName[] serverRT = getServerRuntimes();
      System.out.println("got server runtimes");
      int length = (int) serverRT.length;
      for (int i = 0; i < length; i++) {
         String name = (String) connection.getAttribute(serverRT[i],
            "Name");
         String state = (String) connection.getAttribute(serverRT[i],
            "State");
         System.out.println("Server name: " + name + ".   Server state: "
            + state);
      }
   }

   public static void main(String[] args) throws Exception {
      String hostname = args[0];
      String portString = args[1];
      String username = args[2];
      String password = args[3];

      PrintServerState s = new PrintServerState();
      initConnection(hostname, portString, username, password);
      s.printNameAndState();
      connector.close();
   }
}

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

Webアプリケーションの各サーブレットは、サーブレットのランタイムの状態についての情報を保持するServletRuntimeMBeanのインスタンスを提供します。(Oracle WebLogic Server MBeanリファレンスServletRuntimeMBeanに関する項を参照してください。)

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

  1. ドメイン・ランタイムMBeanサーバー(ドメイン内の全サーバー上にあるすべてのサーブレット用)、または特定のサーバー・インスタンス上のランタイムMBeanサーバー。

  2. DomainRuntimeServiceMBeanまたはRuntimeServiceMBeanServerRuntimes属性。

  3. ServerRuntimeMBeanApplicationRuntimes属性。

  4. ApplicationRuntimeMBeanComponentRuntimes属性。

    ComponentRuntimes属性には、様々なタイプのコンポーネントのランタイムMBeanが保持されます。それらの1つがWebAppComponentRuntimeMBeanです。この属性の値を取得したら、子MBeanのType属性を使用して特定のタイプのコンポーネントのランタイムMBeanを取得します。

  5. 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;

   // Initializing the object name for DomainRuntimeServiceMBean
   // so it can be used throughout the class.
   static {
      try {
         service = new ObjectName(
          "com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanser
           vers.domainruntime.DomainRuntimeServiceMBean");
      }catch (MalformedObjectNameException e) {
         throw new AssertionError(e.getMessage());
      }
   }

   /*
   * Initialize connection to the Domain Runtime MBean Server
   */
   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();
   }

   /*
   * Get an array of ServerRuntimeMBeans
   */
   public static ObjectName[] getServerRuntimes() throws Exception {
      return (ObjectName[]) connection.getAttribute(service,
         "ServerRuntimes");
   }

   /*
   * Get an array of WebAppComponentRuntimeMBeans
   */
   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();
   }
}