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 に、現在のドメインの名前とドメイン内のすべてのアクティブなサーバの名前を取得するサンプル クラスを示します。
注意 : 現在のサーバ インスタンスの名前のみを取得するには、管理 MBeanHome
ではなくローカル MBeanHome
インタフェースを使用します。「現在のサーバ インスタンスの名前を取得する」を参照してください。
getState
メソッドが返す値を weblogic.management.runtime.ServerStates
クラスのフィールド名と比較します。このクラスには、サーバのライフ サイクルの各状態に対するフィールドが 1 つずつあります。weblogic.management.runtime.ServerStates
の詳細については、「WebLogic Server の Javadoc」を参照してください。次の例で、weblogic
は MBean の属性を参照および変更するパーミッションを持つユーザのユーザ名とパスワードです。MBean を変更するパーミッションの詳細については、『WebLogic リソースのセキュリティ』の「セキュリティ ロール」を参照してください。
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 クライアントでは以下のことを実施します。
コード リスト 5-2 は、WebLogic Server JVM 内で動作する JMX クライアント用に使用できるコード セグメントです。
// 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;
コード リスト 5-1 のサンプル コードをコンパイルして実行すると、アクティブなドメインおよびサーバを判別できますが、weblogic.Admin
ユーティリティを使用すると Java クラスをコンパイルせずに同様のタスクを実行できます。
次のコマンドでは、現在アクティブなドメインの名前が返されます。AdminServer
はドメインの管理サーバ、MyHost
は管理サーバのホスト コンピュータ、weblogic
は MBean の属性を参照するパーミッションを持つユーザの名前とパスワードです。
java weblogic.Admin -url MyHost:8001 -username weblogic -password weblogic GET -type DomainRuntime -property Name
コマンドの出力には、DomainRuntimeMBean
の WebLogicObjectName
およびその 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.management.runtime.ServerRuntimeMBean
インタフェースは、WebLogic Server インスタンスの実行時の情報を提供します。たとえば、サーバが使用しているリスン ポートとアドレスを示します。また、正常にまたは強制的にサーバを停止できる操作も組み込まれています。
この節では、ServerRuntimeMBean
を検索し、その MBean を使用してサーバ インスタンスを正常に停止する例を示します。正常な停止とその期間の制御については、『WebLogic Server のコンフィグレーションと管理』の「正常な停止」を参照してください。
各例に、ServerRuntimeMBean
を取得するためのさまざまな方法を示します。
weblogic.Admin
ユーティリティでは、実行時 MBean の属性値を変更できません。
各 WebLogic Server インスタンスには、独自の MBeanHome
インタフェースがホストされます。このインタフェースは、そのサーバ インスタンス上のローカル コンフィグレーション MBean と実行時 MBean へのアクセスを提供します。管理 MBeanHome
インタフェースを使用する場合とは異なり、ローカル MBeanHome
を使用すると、フィルタ処理によって現在のサーバに適用される実行時 MBean を見つけ出す手間が省けます。また、管理サーバを経由して要求をルーティングするのではなく、直接ローカル サーバに接続しているので、より少ないネットワーク ホップで MBean にアクセスできます。
MBeanHome
インタフェースには、現在の WebLogic Server に存在する実行時 MBean のみを返す getRuntimeMBean()
メソッドがあります。MBeanHome
.getRuntimeMBean()
メソッドを管理サーバ上で呼び出した場合、管理サーバを管理およびモニタする実行時 MBean のみが返されます。
コード リスト 5-3 のクラス例は次のように機能します。
次の例において、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);
}
}
}
コード リスト 5-1 の例と同様に、この節で扱うサンプル クラスでは管理 MBeanHome
インタフェースを使用して ServerRuntime
MBean を取得します。管理 MBeanHome
はドメイン内のすべての MBean への単一のアクセス ポイントとなりますが、これを使用するには、取得する MBean の WebLogicObjectName
を構築するか、フィルタ処理を行って特定の (現在の) サーバに当てはまる MBean を見つけ出す必要があります。
コード リスト 5-4 のクラス例は次のように機能します。
次の例で、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);
}
}
}
すべての MBean のリストを取得し、そのリストをフィルタ処理して特定のサーバの ServerRuntimeMBean
を見つけ出す代わりに、この例では MBean 命名規約を使用して、Server1
というサーバ インスタンス上の ServerRuntimeMBean
の WebLogicObjectName
を構築します。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);
}
}
}
この節の例は、標準的な JMX の手法で MBean と対話します。管理 MBeanHome
インタフェースを使用して javax.management.MBeanServer
インタフェースを取得し、MBeanServer
を使用して Server1
というサーバ インスタンスの ServerRuntimeMBean
の ListenPort
属性の値を取得します。
次の例で、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
を取得するために、この例では次のことを行います。
この例において、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 について、weblogic.management.runtime
パッケージから MBean タイプがインスタンス化されます (表 5-1 を参照)。weblogic.management.runtime
パッケージの MBean の詳細については、「WebLogic Server の Javadoc」を参照してください。
WebLogic Server には、抽象インタフェース EJBRuntimeMBean
も用意されています。このインタフェースには、他の EJB 実行時 MBean が使用するメソッドが含まれます。
EJB 実行時 MBean は、階層内でインスタンス化されます。次に例を示します。
EJBComponentRuntimeMBean
のインスタンスを通じてその実行時データをエクスポーズする。 EntityEJBRuntimeMBean
のインスタンスを通じてその実行時データをエクスポーズする。 EntityEJBRuntimeMBean
は、最高で 4 つの MBean の親となる。 取得する実行時データの種類によっては、データのコンテキストを提供するために親 MBean の名前を取得することも必要となります。たとえば、EJBTransactionRuntimeMBean.
TransactionsRolledBackTotalCount の値を取得する場合は、親 EJBEntityRuntimeMBean
の名前も取得して値の取得元であるエンティティ Bean を判別します。
図 5-1 は、階層関係を示しています。
ドメインにデプロイされたすべての EJB の実行時情報を取得するために、コード リスト 5-8 の例は次のように機能します。
特定のサーバ インスタンスにデプロイされた EJB のみの実行時情報を取得する場合は、その特定のサーバ インスタンスに接続してローカル MBeanHome
インタフェースを取得します。詳細については、「例 : 内部クライアントからのローカル MBeanHome の取得」を参照してください。
StatelessEJBRuntimeMBean.getEJBName
メソッド (すべての EJB 実行時 MBean が EJBRuntimeMBean
から継承) を呼び出して、MBean の名前を取得する。
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 を参照)。
ApplicationRuntimeMBean
のインスタンスを通じてその実行時データをエクスポーズする。 J2EE モジュール (EJB または Web アプリケーションなど) をエンタープライズ アプリケーションのコンポーネントとして宣言せずにデプロイする場合、WebLogic Server は実行時のラッパー エンタープライズ アプリケーションを作成して、各モジュールをそのラッパー アプリケーション内にデプロイします。ラッパー アプリケーションの名前は、含まれるモジュールの名前と同じです。たとえば、myApp.WAR
をデプロイする場合、WebLogic Server はこの Web アプリケーションを myApp
というエンタープライズ アプリケーションの中にラップします。このラッパー アプリケーションと対話する必要はありません。このアプリケーションは WebLogic Server デプロイメントの実装のアーティファクトです。
WebAppComponentRuntimeMBean
のインスタンスを通じてその実行時データをエクスポーズする。ServletRuntimeMBean
のインスタンスを通じてその実行時データをエクスポーズする。図 5-2 アプリケーション、Web アプリケーション、およびサーブレット実行時 MBean の階層
MBean 階層をたどらなくても ServletRuntimeMBean
のインスタンスを取得することはできますが、特定の Web アプリケーション内のサーブレットのみを取り出すためには、この階層的な機構を使用することをお勧めします (サーバ インスタンス上のサーブレットの数によっては、サーバ インスタンス上のすべての ServletRuntime
MBean を取得するとパフォーマンスが低下するおそれがあります)。Web アプリケーション内のサーブレットを取得したら、リストを繰り返し処理してモニタするデータを取得できます。
サンプルPatient
Web アプリケーションMedRec
アプリケーションのコンポーネント) 内のアクション サーブレットのすべてのインスタンスにおけるServletRuntime.InvocationTotalCount
属性の値を取得するために、コード リスト 5-9 のサンプル クラスは次のように機能します。
管理 MBeanHome
インタフェースは、ドメイン内にデプロイされているすべての Web アプリケーション (および Web アプリケーション内のすべてのサーブレット インスタンス) へのアクセスを提供します。
特定のサーバ インスタンスにあるサーブレット インスタンスのみの実行時情報を取得する場合は、その特定のサーバ インスタンスに接続してローカル MBeanHome
インタフェースを取得します。詳細については、「例 : 内部クライアントからのローカル MBeanHome の取得」を参照してください。
ApplicationRuntimeMBean
ごとに、ApplicationRuntimeMBean.getApplicationName
を呼び出して、その戻り値を appName
変数と比較します。注意 : ドメイン内の Web アプリケーションの数によっては、この手順が不要な場合もあります。ドメインに少数の Web アプリケーションしか含まれていない場合は、単純にすべての WebAppComponentRuntime
MBean を取得し、そのリストを繰り返し処理して特定の Web アプリケーションを見つけられます。
コンテキスト ルートは、Web アプリケーションの便利でユニークな識別子です。Web アプリケーションをエンタープライズ アプリケーションの一部としてデプロイする場合は、アプリケーションの application.xml
デプロイメント記述子でコンテキスト ルートを指定します。Web アプリケーションをスタンドアロン モジュールとしてデプロイする場合は、Web アプリケーションの weblogic.xml
デプロイメント記述子でコンテキスト ルートを定義します。
Patient Web アプリケーションの場合、コンテキスト ルートは WL_HOME
\samples\server\medrec\src\medrecEar\
にある次の XML 要素で定義されます。
META-INF\application.xml
<module>
>
<web>
<web-uri>patientWebApp</web-uri>
<context-root>/patient</context-root>
</web>
</module
コード リスト 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);
}
}
![]() ![]() |
![]() |
![]() |