JMX によるカスタム管理ユーティリティの開発
![]() |
![]() |
![]() |
![]() |
以下の節では、JMX クライアントから WebLogic Server MBean にアクセスする方法について説明します。
JMX クライアントが独自の JVM (つまり、WebLogic Server インスタンスでない JVM) で実行される場合は、クライアントのクラスパスに次の 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 クラスのみを使用したリモート接続」を参照してください。
各 WebLogic Server ドメインには 3 種類の MBean サーバが含まれており、それぞれの MBean サーバが別々の MBean 階層へのアクセスを提供します。「MBean サーバ」を参照してください。
WebLogic MBean サーバに接続するには、次の手順に従います。
以下のパラメータ値をコンストラクタに渡します (J2SE 5.0 API 仕様の「JMXServiceURL
」を参照してください)。
t3
、t3s
、http
、https
、iiop
、iiops
/jndi/
を指定し、その後には表 4-1 で説明する JNDI 名のいずれかを続ける必要があります。javax.management.remote.JMXConnector
オブジェクトを作成します。このオブジェクトには、JMX クライアントが MBean サーバへの接続に使用するメソッドがあります。JMXConnector
のコンストラクタ メソッドは、javax.management.remote.JMXConnectorFactory.
connector(JMXServiceURL serviceURL
, Map<String,?>
environment
)
です。
以下のパラメータ値をコンストラクタに渡します (J2SE 5.0 API 仕様の「JMXConnectorFactory
」を参照してください)。
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 クライアントは、クラスパスにこのパッケージのクラスを含める必要があります。「リモート クライアントのクラスパスの設定」を参照してください。
このメソッドは、javax.management.MBeanServerConnection
型のオブジェクトを返します。
MBeanServerConnection
オブジェクトは、WebLogic MBean サーバへの接続であり、ローカル接続にもリモート接続にも使用できます。J2SE 5.0 API 仕様の「MBeanServerConnection
」を参照してください。
コード リスト 4-1 のコードに関する以下の点に留意してください。
connection
および connector
を使用して、MBean サーバへの接続を表す。connection
変数および connector
変数に値を割り当てる initConnection()
メソッドは、クラス内で再利用できる 1 つの接続を確立するためにクラス インスタンスごとに一度だけ呼び出される必要があります。 initConnection()
メソッドは、クラスがインスタンス化されるときに渡される引数として、(サーバのリスン アドレスとリスン ポートとともに) ユーザ名とパスワードを取る。暗号化されていないユーザ資格がコード内に含まれないようにするため、この方法をお勧めします。これらの引数を保持する String
オブジェクトは破棄され、JVM のガベージ コレクション ルーチンによってメモリから削除されます。JMXConnector.close()
メソッドを呼び出して MBean サーバとの接続を閉じる (J2SE 5.0 API 仕様の「JMXConnector
」を参照してください)。コード リスト 4-1 ドメイン実行時 MBean サーバへの接続
public class MyConnection {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;
/*
* ドメイン実行時 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();
}
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();
}
}
WebLogic Server ドメインは 3 種類の MBean サーバを保持しており、それぞれの MBean サーバが特定の機能を実行します。以下のように、実行しようとしているタスクをサポートしている MBean サーバを介して MBean にアクセスします。
実行時 MBean サーバを使用したモニタのほうが、ドメイン実行時 MBean サーバを使用したモニタより、使用するメモリとネットワーク トラフィックの量が小さくなります (WebLogic Server は、クライアントが接続を要求するまでドメイン実行時 MBean サーバを初期化しません)。
ほとんどの場合、ドメイン内のすべてのサーバ インスタンスは同じコンフィグレーション データのセットを保持しているため、管理サーバまたは管理対象サーバどちらの実行時 MBean サーバをモニタするかは重要ではありません。ただし、サーバを再起動しないと消費できない変更を行うと、サーバは変更を受け入れなくなり、そのサーバのコンフィグレーション データが古くなるおそれがあります。この場合、このサーバの実行時 MBean サーバをモニタすることは、その特定のサーバ インスタンスのコンフィグレーションのみをモニタすることになります。WebLogic Server ドメインの変更および変更のアクティブ化のプロセスを理解するには、『ドメインのコンフィグレーションについて』の「コンフィグレーションの変更の管理」を参照してください。
ドメイン実行時 MBean サーバの MBean に JMX リスナとフィルタを登録する場合は、JMX フィルタを、それがモニタする MBean と同じ JVM で実行します。たとえば、ある管理対象サーバ上の MBean にフィルタを登録する場合は、フィルタをその管理対象サーバで実行し、フィルタ条件を満たすメッセージのみをリスナに転送します。
一般的に、ドメイン実行時 MBean サーバを使用するコードのほうが、各サーバ インスタンス上の実行時 MBean サーバを使用するコードより管理がしやすく安全性も高くなっています。その理由は以下のとおりです。
ドメイン実行時 MBean サーバを使用してすべての JMX リクエストを転送することによるトレードオフとして、ネットワーク レイテンシとメモリ使用量の増加のためにパフォーマンスが若干低下します。MBean の値を読み込むために各管理対象サーバの実行時 MBean サーバに直接接続すると、ドメイン実行時 MBean サーバが管理対象サーバから値を取得するために作成するネットワーク ホップはなくなりますが、ドメイン実行時 MBean サーバを使用することで実現する簡素化されたコードの保守と強化されたセキュリティのほうが、ほとんどのネットワーク トポロジとパフォーマンス要件に適しています。
図 4-1 ドメイン実行時 MBean サーバと実行時 MBean サーバ
リモート JMX クライアントからの接続では WebLogic Server クラスを使用することをお勧めします。ただし、リモート JMX クライアントで JDK のクラスのみを使用して WebLogic Server JMX エージェントに接続することもできます。そのためには、次の手順に従います。
Administration Console オンライン ヘルプの「IIOP の有効化とコンフィグレーション」を参照してください。
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-host
と WLS-port
は WebLogic Server インスタンスのリスン アドレスとリスン ポートであり、MBean-server-JNDI-name
は表 4-1 に示した値のいずれかです。
作成するハッシュ テーブルには、プロトコル パッケージの名前が含まれていません。この値を null のままにしておくことで、JMX クライアントは JDK の com.sun.jmx.remote.protocol
パッケージからプロトコル定義を使用します。
ローカル クライアントは、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/env/jmx/runtime
JMX クライアントのクラスが、EJB または Web アプリケーションなどの J2EE モジュールにある場合、実行時 MBean サーバの JNDI 名は次のようになります。java:comp/jmx/runtime
次に例を示します。InitialContext ctx = new InitialContext();
server = (MBeanServer)ctx.lookup("java:comp/jmx/runtime");
WebLogic Server は、MBean を階層データ モデルにまとめます (「WebLogic Server MBean のデータ モデル」を参照してください)。このモデルでは、すべての親 MBean に自身の子のオブジェクト名を保持する属性が含まれています。標準の JMX API で子のオブジェクト名を使用して、子 MBean の属性の値を取得または設定したり、そのメソッドを呼び出したりします。
WebLogic Server MBean 階層を移動するには、次の手順に従います。
上記の節「MBean サーバへのリモート接続の作成」を参照してください。
接続を開始すると、javax.management.MBeanServerConnection
型のオブジェクトが返されます。
MBeanServerConnection
.getAttribute(ObjectName
object-name
,
String
attribute
)
メソッドを呼び出して、MBean 階層のルートにある MBean のオブジェクト名を取得します。各値の説明は次のとおりです。object-name
は、MBean サーバに登録されているサービス MBean のオブジェクト名 (「サービス MBean」を参照してください)。表 2-3 には、各種の MBean サーバで使用可能なサービス MBean のタイプが示されています。
attribute
は、ルート MBean を保持するサービス MBean 属性の名前。MBean 階層での MBean の場所を判別するには、『WebLogic Server MBean リファレンス』でその MBean の説明を参照してください。『WebLogic Server MBean リファレンス』では、各 MBean について、現在の MBean のファクトリ メソッドを保持する親 MBean が記載されています。ファクトリ メソッドがパブリックでない MBean については、『WebLogic Server MBean リファレンス』では、現在の MBean にアクセスできる他の MBean が記載されています。
コード リスト 4-2 のコード例では、ドメイン実行時 MBean サーバに接続し、DomainRuntimeServiceMBean
を使用してドメイン内の各 ServerRuntimeMBean
のオブジェクト名を取得します。その後、各サーバの ServerRuntimeMBean
の Name
属性および State
属性の値を取得し、出力します。
コード リスト 4-2 のコードに関する以下の点に留意してください。
connection
グローバル変数と connector
グローバル変数に加えて、WebLogic Server サービス MBean のオブジェクト名をグローバル変数に割り当てる。クラス内のメソッドはこのオブジェクト名を頻繁に使用します。このオブジェクト名は一度定義したら、変更する必要はありません。printServerRuntimes()
メソッドは、ドメイン内のすべての ServerRuntimeMBean
インスタンスの配列を保持する DomainRuntimeServiceMBean
の ServerRuntimes
属性の値を取得する (『WebLogic Server MBean リファレンス』の「DomainRuntimeServiceMBean
」を参照してください)。
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;
// クラス内で使用できるように
// DomainRuntimeServiceMBean のオブジェクト名を初期化する
static {
try {
service = new ObjectName(
"com.bea:Name=DomainRuntimeService,Type=weblogic.management.
mbeanservers.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 の配列を出力する。
* この MBean は実行時 MBean 階層のルートであり、
* ドメイン内の各サーバは独自のインスタンスをホストする
*/
public static ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection.getAttribute(service,
"ServerRuntimes");
}
/*
* ServerRuntimeMBean を反復処理して、名前と状態を取得する
*/
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
のインスタンスを提供します (『WebLogic Server MBean リファレンス』の「ServletRuntimeMBean
」を参照してください)。
WebLogic Server のデータ モデルでは、ServletRuntimeMBean
へのパスは次のようになります。
コード リスト 4-3 のコードでは、上記の節で説明したように階層を移動し、ServletRuntimeMBean
の値を取得します。
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();
}
}
![]() ![]() |
![]() |
![]() |