ナビゲーションをスキップ

WebLogic JMX Service プログラマーズ ガイド

  前 次 前/次ボタンと目次ボタンとの区切り線 目次  

実行時の情報へのアクセス

WebLogic Server には、管理対象リソースの実行時の状態に関する情報を提供する MBean が数多く用意されています。この実行時データを参照および変更するアプリケーションを作成するには、まず MBeanServer インタフェースまたは WebLogic Server 型保障インタフェースを使用して実行時 MBean を取得する必要があります。その後に、weblogic.management.runtime パッケージの API を使用して実行時データを参照または変更します。API ドキュメントの表示については、「実行時 MBean API のドキュメント」を参照してください。

以下の節では、WebLogic Server ドメインとサーバ インスタンスに関する実行時情報を取得および修正する例を示します。

 


例 : アクティブなドメインとサーバの判別

MBeanHome インタフェースには、現在アクティブなドメインの名前およびサーバ インスタンスの名前の判別に使用できる API があります。

The example class in コード リスト 5-1 に、現在のドメインの名前とドメイン内のすべてのアクティブなサーバの名前を取得するサンプル クラスを示します。

  1. 管理 MBeanHome インタフェースを取得します。
  2. 注意 : 現在のサーバ インスタンスの名前のみを取得するには、管理 MBeanHome ではなくローカル MBeanHome インタフェースを使用します。「現在のサーバ インスタンスの名前を取得する」を参照してください。

  3. MBeanHome.getActiveDomain().getName() を使用して、ドメインの名前を取得します。
  4. getMBeansByType メソッドを使用して、ドメイン内のすべての ServerRuntime MBean のセットを取得します。
  5. 取得したセットを検索し、ServerRuntimeMBean インスタンスの名前と WebLogic Server インスタンスの名前を比較します。
  6. serverRuntime.getState メソッドを呼び出して、各サーバ インスタンスの状態を取得します。
  7. getState メソッドが返す値を weblogic.management.runtime.ServerStates クラスのフィールド名と比較します。このクラスには、サーバのライフ サイクルの各状態に対するフィールドが 1 つずつあります。weblogic.management.runtime.ServerStates の詳細については、「WebLogic Server の Javadoc」を参照してください。
  8. インスタンスがアクティブであれば、そのサーバの名前を出力します。

次の例で、weblogic は MBean の属性を参照および変更するパーミッションを持つユーザのユーザ名とパスワードです。MBean を変更するパーミッションの詳細については、『WebLogic リソースのセキュリティ』の「セキュリティ ロール」を参照してください。

コード リスト 5-1アクティブなドメインとサーバの判別

import java.util.Set;
import java.util.Iterator;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.ServerRuntimeMBean;
import weblogic.management.runtime.ServerStates;
public class getActiveDomainAndServers {
    public static void main(String[] args) {
        MBeanHome home = null;
        //管理サーバの url
        String url = "t3://localhost:7001";
        String username = "weblogic";
        String password = "weblogic";
        //初期コンテキストを設定
        try {
            Environment env = new Environment();
            env.setProviderUrl(url);
            env.setSecurityPrincipal(username);
            env.setSecurityCredentials(password);
            Context ctx = env.getInitialContext();
            //管理 MBeanHome を取得
            home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
        } catch (Exception e) {
            System.out.println("Exception caught: " + e);
        }
        //アクティブなドメインの名前を取得
        try {
            System.out.println("Active Domain: " +
                    home.getActiveDomain().getName() );
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
        //ドメイン内のサーバの名前を取得
        System.out.println("Active Servers: ");
        Set mbeanSet = home.getMBeansByType("ServerRuntime");
        Iterator mbeanIterator = mbeanSet.iterator();
        while(mbeanIterator.hasNext()) {
            ServerRuntimeMBean serverRuntime =
                (ServerRuntimeMBean)mbeanIterator.next();
            //アクティブなサーバの名前を出力
            if(serverRuntime.getState().equals(ServerStates.RUNNING)){
                System.out.println("Name: " + serverRuntime.getName());
                System.out.println("ListenAddress: " +
                         serverRuntime.getListenAddress());
                 System.out.println("ListenPort: " +
                         serverRuntime.getListenPort());
                 //count++;
            }
        }
        System.out.println("Number of servers active in the domain: " +
                            mbeanSet.size());
     }
}

現在のサーバ インスタンスの名前を取得する

現在のサーバ インスタンスの名前のみを取得するには、JMX クライアントを作成します。JMX クライアントでは以下のことを実施します。

  1. ローカル MBeanHome インタフェースを取得します。
  2. MBeanHome.getMBeansByType() を使用して、サーバ内のすべての ServerRuntime MBean を取得します。
  3. ローカル MBeanHome インタフェースは現在のサーバ インスタンスの実行時 MBean にのみアクセスできるため、getMBeansByType() メソッドからは、現在のサーバの ServerRuntimeMBean のみが返されます。

  4. ServerRuntimeMBean.getName メソッドを呼び出します。

コード リスト 5-2 は、WebLogic Server JVM 内で動作する JMX クライアント用に使用できるコード セグメントです。

コード リスト 5-2

// JNDI コンテキストを取得する
weblogic.jndi.Environment env = new Environment();
env.setSecurityPrincipal(USERNAME);
env.setSecurityCredentials(PASSWORD);
Context ctx = env.getInitialContext();
// ローカル MBeanHome を取得する
MBeanHome home =
   (MBeanHome)ctx.lookup(MBeanHome.LOCAL_JNDI_NAME);
Set s = home.getMBeansByType("ServerRuntime");
Iterator i = s.iterator();
ServerRuntimeMBean serverRT = (ServerRuntimeMBean) i.next();
String serverName = serverRT.getName();
return serverName;

weblogic.Admin を使用したアクティブなドメインおよびサーバの判別

コード リスト 5-1 のサンプル コードをコンパイルして実行すると、アクティブなドメインおよびサーバを判別できますが、weblogic.Admin ユーティリティを使用すると Java クラスをコンパイルせずに同様のタスクを実行できます。

次のコマンドでは、現在アクティブなドメインの名前が返されます。AdminServer はドメインの管理サーバ、MyHost は管理サーバのホスト コンピュータ、weblogic は MBean の属性を参照するパーミッションを持つユーザの名前とパスワードです。

java weblogic.Admin -url MyHost:8001 -username weblogic -password weblogic GET -type DomainRuntime -property Name

コマンドの出力には、DomainRuntimeMBeanWebLogicObjectName およびその Name 属性の値が含まれます。

{MBeanName="myDomain:Location=AdminServer,Name=myDomain,ServerRuntime=Admi
nServer,Type=DomainRuntime"{Name=myDomain}}

現在アクティブなサーバ インスタンスのリストを表示するには、管理サーバを使用して、管理 MBeanHome インタフェースで登録されているすべての ServerRuntime MBean を取得します (アクティブなサーバ インスタンスのみが、ServerRuntime MBean を管理 MBeanHome インタフェースに登録します)。

次のように -adminurl 引数を指定して、管理サーバの管理 MBeanHome インタフェースを使用するように GET コマンドに指示する必要があります。

java weblogic.Admin -adminurl MyHost:8001 -username weblogic -password
weblogic GET -type ServerRuntime -property State

コマンドの出力には、すべての ServerRuntime MBean の WebLogicObjectName および各 State 属性の値が含まれます。

---------------------------
MBeanName: "myDomain:Location=MedRecMS2,Name=MedRecMS2,Type=ServerRuntime"
State: RUNNING
---------------------------
MBeanName: "myDomain:Location=AdminServer,Name=AdminServer,Type=ServerRuntime"
State: RUNNING
---------------------------
MBeanName: "myDomain:Location=MedRecMS1,Name=MedRecMS1,Type=ServerRuntime"
State: RUNNING

 


例 : WebLogic Server インスタンスの実行時の状態の参照および変更

weblogic.management.runtime.ServerRuntimeMBean インタフェースは、WebLogic Server インスタンスの実行時の情報を提供します。たとえば、サーバが使用しているリスン ポートとアドレスを示します。また、正常にまたは強制的にサーバを停止できる操作も組み込まれています。

この節では、ServerRuntimeMBean を検索し、その MBean を使用してサーバ インスタンスを正常に停止する例を示します。正常な停止とその期間の制御については、『WebLogic Server のコンフィグレーションと管理』の「正常な停止」を参照してください。

各例に、ServerRuntimeMBean を取得するためのさまざまな方法を示します。

weblogic.Admin ユーティリティでは、実行時 MBean の属性値を変更できません。

ローカル MBeanHome と getRuntimeMBean() の使用

各 WebLogic Server インスタンスには、独自の MBeanHome インタフェースがホストされます。このインタフェースは、そのサーバ インスタンス上のローカル コンフィグレーション MBean と実行時 MBean へのアクセスを提供します。管理 MBeanHome インタフェースを使用する場合とは異なり、ローカル MBeanHome を使用すると、フィルタ処理によって現在のサーバに適用される実行時 MBean を見つけ出す手間が省けます。また、管理サーバを経由して要求をルーティングするのではなく、直接ローカル サーバに接続しているので、より少ないネットワーク ホップで MBean にアクセスできます。

MBeanHome インタフェースには、現在の WebLogic Server に存在する実行時 MBean のみを返す getRuntimeMBean() メソッドがあります。MBeanHome.getRuntimeMBean() メソッドを管理サーバ上で呼び出した場合、管理サーバを管理およびモニタする実行時 MBean のみが返されます。

コード リスト 5-3 のクラス例は次のように機能します。

  1. t3://ServerHost:7001 で要求をリスンするサーバ インスタンスに接続するための情報で javax.naming.Context オブジェクトをコンフィグレーションします。
  2. Context.lookup メソッドを使用して、サーバ インスタンスのローカル MBeanHome インタフェースを取得します。
  3. MBeanHome.LOCAL_JNDI_NAME フィールドは、現在のサーバのローカル MBeanHome の JNDI 名を返します。

  4. MBeanHome.getRuntimeMBean(String name,String type) メソッドを使用して、現在のサーバ インスタンスの ServerRuntimeMBean を取得します。
  5. ServerRuntimeMBean のメソッドを呼び出して、サーバの状態を取得および変更します。

次の例において、weblogic は MBean の属性を参照および修正するパーミッションを持つユーザのユーザ名とパスワードで、Server1 は状態を参照および変更する必要のある WebLogic Server インスタンスの名前です。MBean を変更するパーミッションの詳細については、『WebLogic リソースのセキュリティ』の「セキュリティ ロール」を参照してください。

コード リスト 5-3ローカル MBeanHome と getRuntimeMBean() の使用

import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.ServerRuntimeMBean;
public class serverRuntime1 {
    public static void main(String[] args) {
        MBeanHome home = null;
        //ドメイン変数
        String url = "t3://ServerHost:7001";
        String serverName = "Server1";
        String username = "weblogic";
        String password = "weblogic";
        ServerRuntimeMBean serverRuntime = null;
        //初期コンテキストを設定
        try {
            Environment env = new Environment();
            env.setProviderUrl(url);
            env.setSecurityPrincipal(username);
            env.setSecurityCredentials(password);
            Context ctx = env.getInitialContext();
           //ローカル MBeanHome を取得
            home = (MBeanHome) ctx.lookup(MBeanHome.LOCAL_JNDI_NAME);
            System.out.println("Got the MBeanHome: " + home + " for server: " +
                                                                  serverName);
        } catch (Exception e) {
            System.out.println("Caught exception: " + e);
        }
        // ここで、getRuntimeMBean メソッドを使用してサーバ インスタンスの
       //ServerRuntimeMbean にアクセスする
        try { 
            serverRuntime =
                           (ServerRuntimeMBean)home.getRuntimeMBean
                           (serverName,"ServerRuntime");
            System.out.println("Got serverRuntimeMBean: " + serverRuntime);
            //ServerRuntimeMBean を使用して状態を取得および変更
            System.out.println("Current state: " + serverRuntime.getState() );
            serverRuntime.shutdown();
            System.out.println("Current state: " + serverRuntime.getState() );
        } catch (javax.management.InstanceNotFoundException e) {
            System.out.println("Caught exception: " + e);
        }
    }
}

管理 MBeanHome と getMBeansByType() の使用

コード リスト 5-1 の例と同様に、この節で扱うサンプル クラスでは管理 MBeanHome インタフェースを使用して ServerRuntime MBean を取得します。管理 MBeanHome はドメイン内のすべての MBean への単一のアクセス ポイントとなりますが、これを使用するには、取得する MBean の WebLogicObjectName を構築するか、フィルタ処理を行って特定の (現在の) サーバに当てはまる MBean を見つけ出す必要があります。

コード リスト 5-4 のクラス例は次のように機能します。

  1. 管理 MBeanHome インタフェースを取得します。
  2. MBeanHome.getMBeansByType メソッドを使用して、ドメイン内のすべての ServerRuntime MBean を取得します。
  3. MBean のリストを Set オブジェクトに割り当て、Set および Iterator インタフェースのメソッドを使用してリストを検索します。
  4. ServerRuntimeMBean.getName メソッドを使用して、MBean の WebLogicObjectNameName 要素を取得します。次に、Name 値を別の値と比較します。
  5. 特定のサーバ インスタンスの ServerRuntimeMBean が見つかったら、ServerRuntimeMBean.getState メソッドを使用して現在のサーバの状態を返します。
  6. 続いて ServerRuntimeMBean.shutdown() メソッドを呼び出します。このメソッドで、正常な停止が開始されます。

次の例で、weblogic はサーバの状態を変更するパーミッションを持つユーザのユーザ名とパスワードで、Server1 は状態を参照および変更する必要のある WebLogic Server インスタンスの名前です。MBean を変更するパーミッションの詳細については、『WebLogic リソースのセキュリティ』の「セキュリティ ロール」を参照してください。

コード リスト 5-4管理 MBeanHome と getMBeansByType() の使用

import java.util.Set;
import java.util.Iterator;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.ServerRuntimeMBean;
public class serverRuntimeInfo {
    public static void main(String[] args) {
        MBeanHome home = null;
        //ドメイン変数
        String url = "t3://localhost:7001";
        String serverName = "Server1";
        String username = "weblogic";
        String password = "weblogic";
        ServerRuntimeMBean serverRuntime = null;
        Set mbeanSet = null;
        Iterator mbeanIterator = null;
        //初期コンテキストを設定
        try {
            Environment env = new Environment();
            env.setProviderUrl(url);
            env.setSecurityPrincipal(username);
            env.setSecurityCredentials(password);
            Context ctx = env.getInitialContext();
            // 管理 MBeanHome インタフェースを取得
            home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
            System.out.println("Got the Admin MBeanHome: " + home );
        } catch (Exception e) {
            System.out.println("Exception caught: " + e);
        }
        // getMBeansByType メソッドを使用して ServerRuntime MBean
        //を取得する
        try {
            mbeanSet = home.getMBeansByType("ServerRuntime");
            mbeanIterator = mbeanSet.iterator();
            // 各 ServerRutime MBean のサーバの名前を serverName
            // で指定された値と比較する
            while(mbeanIterator.hasNext()) {
                serverRuntime = (ServerRuntimeMBean)mbeanIterator.next();
                if(serverRuntime.getName().equals(serverName)) {
                    System.out.println("Found the serverRuntimembean: " +
                                      serverRuntime + " for: " + serverName);
                    System.out.println("Current state: " +
                                                  serverRuntime.getState() );
                    System.out.println("Stopping the server ...");
                    serverRuntime.shutdown();
                    System.out.println("Current state: " +
                                       serverRuntime.getState() );
                }
            }
        } catch (Exception e) {
            System.out.println("Caught exception: " + e);
        }
    }
}

管理 MBeanHome と getMBean() の使用

すべての MBean のリストを取得し、そのリストをフィルタ処理して特定のサーバの ServerRuntimeMBean を見つけ出す代わりに、この例では MBean 命名規約を使用して、Server1 というサーバ インスタンス上の ServerRuntimeMBeanWebLogicObjectName を構築します。WebLogicObjectName の構築については、表 3-1 を参照してください。

正しいオブジェクト名を確実に指定するには、weblogic.Admin GET コマンドを使用します。たとえば次のコマンドでは、MyHost というホスト コンピュータで動作するサーバ インスタンスの ServerRuntimeMBean のオブジェクト名と属性のリストが返されます。

java weblogic.Admin -url http://MyHost:7001 -username weblogic
-password weblogic GET -pretty -type ServerRuntime

weblogic.Admin ユーティリティを使用した MBean 情報の検索方法については、『WebLogic Server コマンド リファレンス』の「WebLogic Server MBean を管理するためのコマンド」を参照してください。

コード リスト 5-5 において、weblogic は MBean の属性を参照および修正するパーミッションを持つユーザのユーザ名とパスワード、Server1 は状態を参照および変更する必要のある WebLogic Server インスタンスの名前、mihirDomain は WebLogic Server 管理ドメインの名前です。MBean を変更するパーミッションの詳細については、『WebLogic リソースのセキュリティ』の「セキュリティ ロール」を参照してください。

コード リスト 5-5管理 MBeanHome と getMBean() の使用

import java.util.Set;
import java.util.Iterator;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.ServerRuntimeMBean;
import weblogic.management.WebLogicObjectName;
public class serverRuntimeInfo2 {
    public static void main(String[] args) {
        MBeanHome home = null;
        //ドメイン変数
        String url = "t3://localhost:7001";
        String serverName = "Server1";
        String username = "weblogic";
        String password = "weblogic";
        String domain = "medrec";
        ServerRuntimeMBean serverRuntime = null;
        //初期コンテキストを設定
        try {
            Environment env = new Environment();
            env.setProviderUrl(url);
            env.setSecurityPrincipal(username);
            env.setSecurityCredentials(password);
            Context ctx = env.getInitialContext();
            home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
            System.out.println("Got Admin MBeanHome from the Admin server: "
                               + home);
        } catch (Exception e) {
            System.out.println("Exception caught: " + e);
        }
        try {
            WebLogicObjectName objName = new WebLogicObjectName(serverName,
                           "ServerRuntime",home.getDomainName(),serverName);
           System.out.println("Created WebLogicObjectName: " + objName);
            serverRuntime = (ServerRuntimeMBean)home.getMBean(objName);
            System.out.println("Got the serverRuntime using the adminHome: " +
                                serverRuntime );
            System.out.println("Current state: " + serverRuntime.getState() );
            System.out.println("Stopping the server ...");
            //状態を SHUTDOWN に変更
            serverRuntime.shutdown();
            System.out.println("Current state: " + serverRuntime.getState() );
        } catch(Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

MBeanServer インタフェースの使用

この節の例は、標準的な JMX の手法で MBean と対話します。管理 MBeanHome インタフェースを使用して javax.management.MBeanServer インタフェースを取得し、MBeanServer を使用して Server1 というサーバ インスタンスの ServerRuntimeMBeanListenPort 属性の値を取得します。

次の例で、weblogic は MBean の属性を参照および修正するパーミッションを持つユーザのユーザ名とパスワードで、mihirDomain は WebLogic Server 管理ドメインの名前です。MBean を変更するパーミッションの詳細については、『WebLogic リソースのセキュリティ』の「セキュリティ ロール」を参照してください。

コード リスト 5-6管理 MBeanHome と getMBean() の使用

import java.util.Set;
import java.util.Iterator;
import javax.naming.Context;
import javax.management.MBeanServer;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.WebLogicObjectName;
public class serverRuntimeInfo2 {
    public static void main(String[] args) {
        MBeanHome home = null;
        //ドメイン変数
        String url = "t3://localhost:7001";
        String serverName = "MedRecServer";
        String username = "weblogic";
        String password = "weblogic";
        Object attributeValue = null;
        MBeanServer homeServer = null;
        //初期コンテキストを設定
        try {
            Environment env = new Environment();
            env.setProviderUrl(url);
            env.setSecurityPrincipal(username);
            env.setSecurityCredentials(password);
            Context ctx = env.getInitialContext();
            // 管理 MBeanHome インタフェースを取得
            home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
            System.out.println("Got Admin MBeanHome from the Admin server: " +
                                                                      home);
        } catch (Exception e) {
            System.out.println("Exception caught: " + e);
        }
        try {
            // MBean オブジェクト名を作成
            WebLogicObjectName objName = new WebLogicObjectName(serverName,
                           "ServerRuntime",home.getDomainName(),serverName);
            System.out.println("Created WebLogicObjectName: " + objName);
            //MBeanServer インタフェースを取得
            homeServer = home.getMBeanServer();
            //ServerRuntimeMBean の ListenPort 属性を取得
            attributeValue = homeServer.getAttribute(objName, "ListenPort");
            System.out.println("ListenPort for " + serverName + " is:" +
                                                            attributeValue);
        } catch(Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

 


例 : クラスタに関する実行時の情報の参照

この節の例では、クラスタ内で現在実行されている WebLogic Server インスタンスの数と名前を取得します。この例では、weblogic.management.runtime.ClusterRuntimeMBean を使用します。この MBean は、1 つの管理対象サーバから見た WebLogic クラスタのメンバーの情報を提供します。

ClusterRuntimeMBean のインスタンスは管理対象サーバのみでホストされます。また、ClusterRuntimeMBean のインスタンスはクラスタに参加しているアクティブな管理対象サーバから取得する必要があります。

クラスタにあるアクティブな管理対象サーバから ClusterRuntimeMBean を取得するために、この例では次のことを行います。

  1. 管理 MBeanHome を取得します。このインタフェースは管理サーバ上で実行され、ドメイン内のすべての ClusterRuntimeMBean へのアクセスを提供します。
  2. すべての ClusterRuntimeMBean を取得し、それらが対象クラスタに属しているかどうかを調べます。
  3. 対象クラスタ内の管理対象サーバの ClusterRuntimeMBean を 1 つ見つけます。
  4. 管理対象サーバの ClusterRuntimeMBean の API を使用して、クラスタ内のアクティブ サーバの数と名前を判別します。

この例において、weblogic は MBean の属性を参照および変更するパーミッションを持つユーザのユーザ名とパスワードです。MBean を変更するパーミッションの詳細については、『WebLogic リソースのセキュリティ』の「セキュリティ ロール」を参照してください。

コード リスト 5-7クラスタ内で実行中のサーバのリストの取得

import java.util.Set;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import javax.management.ObjectName;
import weblogic.management.WebLogicMBean;
import weblogic.management.runtime.ClusterRuntimeMBean;
import weblogic.management.WebLogicObjectName;
import weblogic.management.MBeanHome;
public class getRunningServersInCluster {
    public static void main(String[] args) {
        MBeanHome home = null;
        //ドメイン変数
        String url = "t3://localhost:7001"; //管理サーバの url
        /* ドメインに複数のクラスタが存在する場合、クラスタ内の
         * すべてのサーバのリストを定義する。ドメインのサーバをこのリストと
         * 比較して、対象クラスタに属するサーバを特定する
         */
        String server1 = "cs1"; // クラスタ内のサーバの名前
        String server2 = "cs2"; // クラスタ内のサーバの名前
        String username = "weblogic";
        String password = "weblogic";
        ClusterRuntimeMBean clusterRuntime = null;
        Set mbeanSet = null;
        Iterator mbeanIterator = null;
        String name = "";
        String[] aliveServerArray = null;
        //初期コンテキストを設定
        try {
            Environment env = new Environment();
            env.setProviderUrl(url);
            env.setSecurityPrincipal(username);
            env.setSecurityCredentials(password);
            Context ctx = env.getInitialContext();
            // 管理 MBeanHome インタフェースを取得
            home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
            // ドメイン内の ClusterRuntime MBean のリストを取得
            mbeanSet = home.getMBeansByType("ClusterRuntime");
            mbeanIterator = mbeanSet.iterator();
            while(mbeanIterator.hasNext()) {
                // リストから 1 つの ClusterRuntime MBean を取得
                clusterRuntime = (ClusterRuntimeMBean)mbeanIterator.next();
                // ClusterRuntime MBean の名前を取得
                name = clusterRuntime.getName();
                // 現在の ClusterRuntimeMBean が対象クラスタのサーバに
                // 属しているかどうかを特定
                if(name.equals(server1) || name.equals(server2) ) {
                    // 現在の ClusterRuntimeMBean を使用してクラスタ内の
                    // サーバの数を取得
                    System.out.println("\nNumber of active servers in the
                         cluster: " + clusterRuntime.getAliveServerCount());
                    // クラスタ内のサーバの名前を取得
                    aliveServerArray = clusterRuntime.getServerNames();
                    break;
                }
            }
        } catch (Exception e) {
             System.out.println("Caught exception: " + e);
        }
        if(aliveServerArray == null) {
            System.out.println("\nThere are no running servers in the cluster");
            System.exit(1);
        }
        System.out.println("\nThe running servers in the cluster are: ");
        for (int i=0; i < aliveServerArray.length; i++) {
            System.out.println("server " + i + " : " + aliveServerArray[i]);
        }
    }
}

 


EJB の実行時情報の参照

サーバ インスタンスにデプロイされる各 EJB について、weblogic.management.runtime パッケージから MBean タイプがインスタンス化されます (表 5-1 を参照)。weblogic.management.runtime パッケージの MBean の詳細については、「WebLogic Server の Javadoc」を参照してください。

表 5-1 EJB の実行時情報を提供する MBean

MBean のタイプ

説明

EJBComponentRuntimeMBean

EJB モジュールについて収集されたすべての実行時情報の最上位インタフェース。

StatefulEJBRuntimeMBean

ステートフル セッション Bean でのみインスタンス化される。

ステートフル セッション Bean について収集された EJB 実行時情報にアクセスするためのメソッドが含まれる。

StatelessEJBRuntimeMBean

ステートレス セッション Bean でのみインスタンス化される。

ステートレス セッション Bean について収集された EJB 実行時情報にアクセスするためのメソッドが含まれる。

MessageDrivenEJBRuntimeMBean

メッセージ駆動型 Bean でのみインスタンス化される。

メッセージ駆動型 Bean について収集された EJB 実行時情報にアクセスするためのメソッドが含まれる。

EntityEJBRuntimeMBean

エンティティ Bean について収集された EJB 実行時情報にアクセスするためのメソッドが含まれる。

EJBCacheRuntimeMBean

EJB について収集されたキャッシュ実行時情報にアクセスするためのメソッドが含まれる。

EJBLockingRuntimeMBean

EJB について収集されたロック マネージャ実行時情報にアクセスするためのメソッドが含まれる。

EJBTransactionRuntimeMBean

EJB について収集されたトランザクション実行時情報にアクセスするためのメソッドが含まれる。

EJBPoolRuntimeMBean

ステートレス セッション Bean でのみインスタンス化される。

ステートレス セッション EJB について収集されたフリープール実行時情報にアクセスするためのメソッドが含まれる。

WebLogic Server は、フリー プールを使用してステートレス セッション EJB のパフォーマンスとスループットを高めます。フリー プールには、非バインド ステートレス セッション EJB が格納される。非バインド EJB インスタンスはステートレス セッション EJB クラスのインスタンスで、メソッド呼び出しを処理しない。


 

WebLogic Server には、抽象インタフェース EJBRuntimeMBean も用意されています。このインタフェースには、他の EJB 実行時 MBean が使用するメソッドが含まれます。

EJB 実行時 MBean は、階層内でインスタンス化されます。次に例を示します。

取得する実行時データの種類によっては、データのコンテキストを提供するために親 MBean の名前を取得することも必要となります。たとえば、EJBTransactionRuntimeMBean.TransactionsRolledBackTotalCount の値を取得する場合は、親 EJBEntityRuntimeMBean の名前も取得して値の取得元であるエンティティ Bean を判別します。

図 5-1 は、階層関係を示しています。

図 5-1 EJB 実行時 MBean の階層

EJB 実行時 MBean の階層


 

例 : すべてのステートフルおよびステートレス EJB の実行時情報の取得

ドメインにデプロイされたすべての EJB の実行時情報を取得するために、コード リスト 5-8 の例は次のように機能します。

  1. 管理サーバに接続し、管理 MBeanHome インタフェースを取得します。
  2. 特定のサーバ インスタンスにデプロイされた EJB のみの実行時情報を取得する場合は、その特定のサーバ インスタンスに接続してローカル MBeanHome インタフェースを取得します。詳細については、「例 : 内部クライアントからのローカル MBeanHome の取得」を参照してください。

  3. フリー プールのステートレス Bean インスタンスを取得しようとしてできなかった回数の割合を表示するために、この例は次のように機能します。
    1. MBeanHome.getMBeansByType を呼び出して、すべての StatelessEJBRuntime MBean を取得します。
    2. 各ステートレス EJB について、displayEJBInfo メソッド (このクラスに後で定義) を呼び出します。このメソッドは次のように機能します。
    1. StatelessEJBRuntime.getPoolRuntime メソッドを呼び出して、ステートレス EJB と関連付けられた EJBPoolRuntimeMBean を取得します。
    2. EJBPoolRuntimeMBean.getMissTotalCount メソッドを呼び出して、試行の失敗した回数を取得します。
  4. ドメインの各ステートフル EJB でロールバックされたトランザクションの割合を確認するために、この例は次のように機能します。
    1. MBeanHome.getMBeansByType を呼び出して、すべての StatefulEJBRuntime MBean を取得します。
    2. displayEJBInfo メソッド (このクラスに後で定義) を呼び出します。
    3. EJBRuntime.getTransactionRuntime メソッドを呼び出して、ステートフル EJB と関連付けられた EJBTransactionRuntimeMBean を取得します。
    4. EJBTransactionRuntimeMBean.getTransactionsRolledBackTotalCount メソッドおよび getTransactionsCommittedTotalCount メソッドを呼び出します。
    5. コミットされたトランザクションの数をロールバックされたトランザクションの数で割って、ロールバックされたトランザクションの割合を確認します。

コード リスト 5-8EJB の実行時情報の参照

import java.util.Iterator;
import java.util.Set;
import javax.management.InstanceNotFoundException;
import javax.naming.Context;
import javax.naming.InitialContext;
import weblogic.management.MBeanHome;
import weblogic.management.WebLogicObjectName;
import weblogic.management.configuration.ApplicationMBean;
import weblogic.management.configuration.EJBComponentMBean;
import weblogic.management.configuration.ServerMBean;
import weblogic.management.runtime.EJBComponentRuntimeMBean;
import weblogic.management.runtime.EJBPoolRuntimeMBean;
import weblogic.management.runtime.EJBRuntimeMBean;
import weblogic.management.runtime.EJBTransactionRuntimeMBean;
import weblogic.management.runtime.StatelessEJBRuntimeMBean;
import weblogic.jndi.Environment;
public final class EJBMonitor {
    private String url = "t3://localhost:7001";
    private String user = "weblogic";
    private String password = "weblogic";
    private MBeanHome   mBeanHome; // admin
    public EJBMonitor() throws Exception {
        Environment env = new Environment();
        env.setProviderUrl(url);
        env.setSecurityPrincipal(user);
        env.setSecurityCredentials(password);
        Context ctx = env.getInitialContext();
        mBeanHome = (MBeanHome)ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
    }
    public void displayStatelessEJBPoolMissPercentages()
    throws Exception
    {
        String type = "StatelessEJBRuntime";
        Set beans = mBeanHome.getMBeansByType(type);
        System.out.println("Printing Stateless Session pool miss
                            percentages:");
        for(Iterator it=beans.iterator();it.hasNext();) {
            StatelessEJBRuntimeMBean rt = (StatelessEJBRuntimeMBean)it.next();
            displayEJBInfo(rt);
            EJBPoolRuntimeMBean pool = rt.getPoolRuntime();
            String missPercentage = "0";
            long missCount = pool.getMissTotalCount();
            if(missCount > 0) {
                missPercentage =
                          ""+(float)missCount/pool.getAccessTotalCount()*100;
            }
            System.out.println("Pool Miss Percentage: "+ missPercentage +"\n");
        }
    }
    public void displayStatefulEJBTransactionRollbackPercentages()
    throws Exception
    {
        String type = "StatefulEJBRuntime";
        Set beans = mBeanHome.getMBeansByType(type);
        System.out.println("Printing Stateful transaction rollback
                          percentages:");
        for(Iterator it=beans.iterator();it.hasNext();) {
            EJBRuntimeMBean rt = (EJBRuntimeMBean)it.next();
            displayEJBInfo(rt);
            EJBTransactionRuntimeMBean trans = rt.getTransactionRuntime();
             String rollbackPercentage = "0";
            long rollbackCount = trans.getTransactionsRolledBackTotalCount();
            if(rollbackCount > 0) {
                long totalTransactions = rollbackCount +
                        trans.getTransactionsCommittedTotalCount();
                rollbackPercentage =
                        ""+(float)rollbackCount/totalTransactions*100;
            }
            System.out.println("Transaction rollback percentage: "+
                        rollbackPercentage +"\n");
        }
    }
    private void displayEJBInfo(EJBRuntimeMBean rt) throws Exception {
        System.out.println("EJB Name: "+rt.getEJBName());
        EJBComponentRuntimeMBean compRTMBean =
                        (EJBComponentRuntimeMBean)rt.getParent();
        EJBComponentMBean compMBean = compRTMBean.getEJBComponent();
        ApplicationMBean appMBean = (ApplicationMBean)compMBean.getParent();
        System.out.println("Application Name: "+appMBean.getName());
        System.out.println("Component Name: "+compMBean.getName());
        WebLogicObjectName objName = rt.getObjectName();
        System.out.println("Server Name: "+objName.getLocation());
    }
    public static void main(String[] argv) throws Exception {
        EJBMonitor m = new EJBMonitor();
        m.displayStatelessEJBPoolMissPercentages();
        m.displayStatefulEJBTransactionRollbackPercentages();
    }
}

 


サーブレットの実行時情報の参照

ServletRuntimeMBean のインスタンスは、個々のサーブレットの実行方法に関する情報へのアクセスを提供します。たとえば、ServletRuntime.InvocationTotalCount 属性は、サーブレット インスタンスが呼び出された回数を示します。

ServletRuntimeMBean のインスタンスの WebLogicObjectName はサーブレット タイプがインスタンス化されるたびに動的に生成されるため、各サーブレットに JMX リスナやモニタを登録することはできません。代わりに、ServletRuntime MBean をルックアップして ServletRuntime メソッドを呼び出すという手法を取れます。

ServletRuntime オブジェクト名の一般的な構造は次のとおりです。
domain:Location=dynamic-name,ServerRuntime=server,Type=ServletRuntime

dynamic-name 値には、サーブレットがデプロイされているサーバの名前、サーブレットが含まれるアプリケーションの名前、サーブレットのクラス名、および特定のサーブレット インスタンスに割り当てられている数が含まれます。

次に例を示します。
medrec:Location=MedRecServer,Name=MedRecServer_MedRecServer_MainWAR_org.
apache.struts.action.ActionServlet_549,ServerRuntime=MedRecServer,
Type=ServletRuntime

ServletRuntime MBean は MBean の階層と共にインスタンス化されます(図 5-2 を参照)。

MBean 階層をたどらなくても ServletRuntimeMBean のインスタンスを取得することはできますが、特定の Web アプリケーション内のサーブレットのみを取り出すためには、この階層的な機構を使用することをお勧めします (サーバ インスタンス上のサーブレットの数によっては、サーバ インスタンス上のすべての ServletRuntime MBean を取得するとパフォーマンスが低下するおそれがあります)。Web アプリケーション内のサーブレットを取得したら、リストを繰り返し処理してモニタするデータを取得できます。

例 : サーブレットの実行時情報の取得

サンプルPatient Web アプリケーションMedRec アプリケーションのコンポーネント) 内のアクション サーブレットのすべてのインスタンスにおけるServletRuntime.InvocationTotalCount 属性の値を取得するために、コード リスト 5-9 のサンプル クラスは次のように機能します。

  1. 管理サーバに接続し、管理 MBeanHome インタフェースを取得します。
  2. 管理 MBeanHome インタフェースは、ドメイン内にデプロイされているすべての Web アプリケーション (および Web アプリケーション内のすべてのサーブレット インスタンス) へのアクセスを提供します。

    特定のサーバ インスタンスにあるサーブレット インスタンスのみの実行時情報を取得する場合は、その特定のサーバ インスタンスに接続してローカル MBeanHome インタフェースを取得します。詳細については、「例 : 内部クライアントからのローカル MBeanHome の取得」を参照してください。

  3. MedRec アプリケーションの ApplicationRuntimeMBean を取得するために、このクラスは次のように機能します。
    1. MBeanHome.getMBeansByType を呼び出して、ドメイン内のすべての ApplicationRuntime MBean を取得します。
    2. ApplicationRuntimeMBean ごとに、ApplicationRuntimeMBean.getApplicationName を呼び出して、その戻り値を appName 変数と比較します。
    3. MedRec アプリケーションの ApplicationRuntimeMBean を見つけたら、その MBean を返します。
    4. 注意 : ドメイン内の Web アプリケーションの数によっては、この手順が不要な場合もあります。ドメインに少数の Web アプリケーションしか含まれていない場合は、単純にすべての WebAppComponentRuntime MBean を取得し、そのリストを繰り返し処理して特定の Web アプリケーションを見つけられます。

  4. Patient Web アプリケーションの WebAppComponentRuntimeMBean を取得するために、このクラスは次のように機能します。
    1. ApplicationRuntimeMBean.lookupComponents を呼び出して、すべてのアプリケーション コンポーネントを取得します。
    2. 取得したリストを繰り返し処理して、Web アプリケーション コンポーネント (WebAppComponentRuntime MBean で表されるもの) のみを取得します。
    3. WebAppComponentRuntimeMBean ごとに、WebAppComponentMBean.getContextRoot を呼び出して、その戻り値を ctxRoot 変数と比較します。
    4. コンテキスト ルートは、Web アプリケーションの便利でユニークな識別子です。Web アプリケーションをエンタープライズ アプリケーションの一部としてデプロイする場合は、アプリケーションの application.xml デプロイメント記述子でコンテキスト ルートを指定します。Web アプリケーションをスタンドアロン モジュールとしてデプロイする場合は、Web アプリケーションの weblogic.xml デプロイメント記述子でコンテキスト ルートを定義します。

      Patient Web アプリケーションの場合、コンテキスト ルートは WL_HOME\samples\server\medrec\src\medrecEar\
      META-INF\application.xml
      にある次の XML 要素で定義されます。

      <module>
        <web>
           <web-uri>patientWebApp</web-uri>
            <context-root>/patient</context-root>
        </web>
      </module
      >

    5. Patient Web アプリケーションの WebAppComponentRuntimeMBean を見つけたら、その MBean を返します。
  5. Patient Web アプリケーションにあるアクション サーブレットのすべてのインスタンスを見つけるために、コードは次のように機能します。
    1. WebAppComponentMBean.getServlets を呼び出して、ServletRuntimeMBean のすべてのインスタンスを取得します。
    2. ServletRuntimeMBean ごとに、ServletRuntimeMBean.getServletName を呼び出して、その戻り値を servletName 変数と比較します。
    3. アクション サーブレットのインスタンスを表す ServletRuntimeMBean を見つけたら、その MBean を返します。
  6. アクション サーブレットの各インスタンスの呼び出し回数を返すために、このコードでは ServletRuntimeMBean.getInvocationTotalCount を呼び出して、その戻り値を標準出力に出力します。

コード リスト 5-9Web アプリケーション内のサーブレットの呼び出し回数の取得

import java.util.Set;
import java.util.Iterator;
import java.util.regex.Pattern;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.runtime.ServletRuntimeMBean;
import weblogic.management.runtime.ApplicationRuntimeMBean;
import weblogic.management.runtime.WebAppComponentRuntimeMBean;
import weblogic.management.runtime.ComponentRuntimeMBean;
public class ServletRuntime {
   public static void main(String[] args) {
      //管理サーバの url
      String url = "t3://localhost:7001";
      String username = "weblogic";
      String password = "weblogic";
      String appName = "MedRecEAR";
      String ctxRoot = "/patient";
      String servletName = "action";
      try {
         MBeanHome home = getMBeanHome(url, username, password);
         ApplicationRuntimeMBean app = getApplicationRuntimeMBean
             (home, appName);
         WebAppComponentRuntimeMBean webapp =
             getWebAppComponentRuntimeMBean(app,ctxRoot);
         ServletRuntimeMBean servlet = getServletRuntimeMBean
             (webapp,servletName);
         System.out.println("Invocation count is " +
             servlet.getInvocationTotalCount());
      } catch (Exception e) {
         System.out.println("Exception caught: " + e);
      }
   }
   /**
   * 初期コンテキストを取得して管理 MBeanHome をルックアップ
   */
   private static MBeanHome getMBeanHome(String url,
      String username, String password) throws Exception
   {
      Environment env = new Environment();
      env.setProviderUrl(url);
      env.setSecurityPrincipal(username);
      env.setSecurityCredentials(password);
      Context ctx = env.getInitialContext();
      // 管理 MBeanHome を取得
      return (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
   }
   /**
   * アプリケーションの RuntimeMBean を見つける
   */
   private static ApplicationRuntimeMBean
      getApplicationRuntimeMBean(MBeanHome home, String appName)
      throws Exception
   {
      //
      // すべてのアプリケーションの RuntimeMBean を取得
      //
      Set appMBeans = home.getMBeansByType("ApplicationRuntime");
      Iterator appIterator = appMBeans.iterator();
      //
      // Set を繰り返して、目的のアプリケーションを見つける
      //
      while (appIterator.hasNext()) {
         ApplicationRuntimeMBean appRuntime = (ApplicationRuntimeMBean)
            appIterator.next();
         if (appName.equals(appRuntime.getApplicationName())) {
            return appRuntime;
         }
      }
      throw new Exception("Could not find RuntimeMBean for "+appName);
   }
   /**
   * 特定のアプリケーション内の Web アプリケーションの RuntimeMBean を見つける
   */
   private static WebAppComponentRuntimeMBean
      getWebAppComponentRuntimeMBean(ApplicationRuntimeMBean app,
      String ctxroot) throws Exception
   {
      ComponentRuntimeMBean[] compMBeans = app.lookupComponents();
      if (compMBeans == null) {
         throw new Exception("Application has no components");
      }
      for (int i=0; i<compMBeans.length; i++) {
         if (compMBeans[i] instanceof WebAppComponentRuntimeMBean) {
            WebAppComponentRuntimeMBean webMBean =
              (WebAppComponentRuntimeMBean)compMBeans[i];
            if (ctxroot.equals(webMBean.getContextRoot())) {
               return webMBean;
            }
         }
      }
      throw new Exception("Could not find web application with context
         root "+ctxroot);
   }
   /**
   * 特定の Web アプリケーションにあるサーブレットの RuntimeMBean を見つける
   */
   private static ServletRuntimeMBean
      getServletRuntimeMBean(WebAppComponentRuntimeMBean webapp,
      String servletName) throws Exception
   {
      ServletRuntimeMBean[] svltMBeans = webapp.getServlets();
      if (svltMBeans == null) {
         throw new Exception("No servlets in "+webapp.getComponentName());
      }
      for (int j=0; j<svltMBeans.length; j++) {
         if (servletName.equals(svltMBeans[j].getServletName())) {
            return svltMBeans[j];
         }
      }
      throw new Exception("Could not find servlet named "+servletName);
   }
}

 

フッタのナビゲーションのスキップ  ページの先頭 前 次