![]() ![]() ![]() ![]() |
JMX には MBean をモニタする 2 つの方法があります。特定のイベント (属性値の変更など) が発生したときに通知を送信する方法、または、モニタ MBean という特別なタイプの MBean が別の MBean をポーリングして、属性値について記述した通知を定期的に送信する方法です。これらの通知をリスンしたり、適切に応答したりするリスナという Java クラスを作成します。たとえば、アプリケーションのデプロイ、アンデプロイ、再デプロイ時に通知を受信するリスナを管理ユーティリティに組み込むことができます。
すべての WebLogic Server コンフィグレーション MBean と一部の実行時 MBean は、属性値が変わるときに通知を送信します。
モニタする MBean が通知を送信する場合は、MBean における変更をリスンするリスナ オブジェクトを作成するか、MBean を定期的にポーリングして、属性値が特定の方法で変更された場合にのみ通知を送信するモニタ MBean を作成するかを選択することができます。どちらの方法を選ぶかは、主に通知を受信する状況の複雑さによって決まります。
要件が単純な場合は、MBean からリスナに通知が送信され、変更が直ちに通知されるので、リスナを直接 MBean に登録する方法が適しています。ただし、リスナと省略可能なフィルタ (javax.management.NotificationListener
および NotificationFilter
) を実装する基本クラスには、値をしきい値や他の値と比較する機能がほとんどありません (J2SE 5.0 API 仕様の javax.management
パッケージを参照)。
通知の要件が複雑な場合や、MBean 属性値の 1 つの変更に直接関連付けられない一連の変更をモニタする場合は、モニタ MBean を使用します (J2SE 5.0 API 仕様の javax.management.monitor
パッケージを参照)。モニタ MBean には、データを比較して、特定の状況でのみ通知を送信するツールが豊富に用意されています。ただし、モニタは観察対象 MBean で属性値の変更を定期的にポーリングし、変更の通知は指定したポーリング間隔でのみ受けられます。
WebLogic Server JMX エージェントと WebLogic Server MBean は、さまざまなタイプのイベントに応じてさまざまな型の通知オブジェクトを送信します。ほとんどのイベント タイプは複数の MBean をトリガして、イベント プロセスのさまざまな時点で通知を送信します。表 7-1 では、一般的なイベント タイプについて説明し、通知をリスンするために JMX モニタ アプリケーションが登録すべき推奨 MBean を示しています。
注意 : | 各 JMX 通知オブジェクトには、ドット区切りの文字列を格納する Type という属性があります。この Type 属性の説明と通知のオブジェクト型を混同しないようにしてください。 |
注意 : | Type 属性によって、通知の分類やフィルタ処理を行うことができます。たとえば、カスタム MBean が通知を送信する場合、JMX の規約では、通知オブジェクトの Type 属性を、mycompany.myapp.valueIncreased という企業名で始まる文字列に設定するように勧めています。 |
注意 : | すべての JMX 通知オブジェクトは javax.management.Notification オブジェクト型を拡張します。JMX と WebLogic Server では javax.management.AttributeChangeNotification などの追加の通知オブジェクト型を定義しています。追加のオブジェクト型には、さまざまなタイプのイベントに適した特殊な情報が含まれています。Notification のサブクラスのリストについては、J2SE 5.0 API 仕様の javax.management.Notification を参照してください。また、WebLogic Server API リファレンスの「weblogic.management.logging.WebLogicLogNotification 」も参照してください。 |
ServerLifeCycleRuntimeMBean に登録して、AttributeChangeNotificationFilter をコンフィグレーションする。
ServerLifeCycleRuntimeMBean があり、そのサーバがアクティブでない場合でも、ドメイン実行時 MBean サーバから利用できる。サーバ インスタンスを起動すると、そのサーバの ServerLifeCycleRuntimeMBean は State 属性の値を更新して、AttributeChangeNotification を送信する。
|
|||
javax.management.MBeanServerDelegate にリスナを登録する。javax.management.MBeanServerDelegate は MBean が登録または登録解除されると javax.management.MBeanServerNotification 型の通知を送信する。
|
|||
|
|||
AttributeChangeNotification 型の通知を送信する。この通知を受信するには、ドメイン実行時 MBean サーバ内の MBean にリスナを登録する。
|
|||
weblogic.management.runtime.LogBroadcasterRuntimeMBean は weblogic.management.logging.WebLogicLogNotification 型の通知を送信する。これは、標準の javax.management.Notification クラスとしてキャストできる。
LogBroadcasterRuntimeMBean にリスナを登録する。標準の JMX 通知をリスンできる。または、ログ メッセージの詳細情報を取得する場合は、詳細情報の取得に使用できるメソッドを備えた WebLogicLogNotification をリスンできる。WebLogicLogNotification をリスンするには、この WebLogic Server クラスをリスナ クラスにインポートする必要がある。
WebLogicLogNotification 」を参照。
|
WebLogic Server MBean は、サービスやリソースの実行時状態に関する詳細な統計を提供します。表 7-2 の統計は、WebLogic Server のパフォーマンスの概要を提供するものです。これらの統計の変化をリスンするには、リスナを作成して、属性が含まれる MBean にリスナを直接登録します。または、モニタ MBean をコンフィグレーションして、重要だと判断した統計のみを定期的にポーリングして報告させることもできます。「通知リスナとフィルタを登録する」および「モニタとリスナを登録する」を参照してください。
ServerLifeCycleRuntimeMBean
|
|
ServerMBean
|
|
ThreadPoolRuntimeMBean
|
|
ThreadPoolRuntimeMBean
|
|
JVMRuntimeMBean
|
|
JDBCDataSourceRuntimeMBean
|
|
JDBCDataSourceRuntimeMBean
|
|
JDBCDataSourceRuntimeMBean
|
|
JDBCDataSourceRuntimeMBean
|
|
JDBCDataSourceRuntimeMBean
|
MBean が送信する通知を直接リスンするには、次の手順に従います。
javax.management.NotificationListener
を実装するクラスを作成します。
J2SE 5.0 API 仕様の NotificationListener
を参照してください。
NotificationListener.handleNotification(Notification notification, java.lang.Object handback)
メソッドを追加します。注意 : | 通知ブロードキャスタをブロックしないように、このメソッドの実装はできるだけ早く復帰する必要があります。 |
Type
属性の値を知ることができます。この値は、通知の送信を引き起こしたイベントのタイプを分類するのに使用します。
通知オブジェクトから情報を取得するには、handleNotification
メソッドの内部で、そのオブジェクトのメソッドを呼び出します。すべての通知型は javax.management.Notification
を拡張しているので、Notification
の以下のメソッドはすべての通知で使用できます。
getMessage()
getSequenceNumber()
getTimeStamp()
getType()
getUserData()
J2SE 5.0 API 仕様の Notification
を参照してください。
ほとんどの通知型は、その通知に固有のデータを取得するための追加のメソッドを提供しています。たとえば、javax.management.AttributeChangeNotification
には、値がどのように変化したかを判断するのに使用できる getNewValue()
と getOldValue()
があります。
コード リスト 7-1 は、AttributeChangeNotification のメソッドを使用して、値が変化した属性の名前と古い値および新しい値を取得する簡単なリスナです。
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.AttributeChangeNotification;
public class MyListener implements NotificationListener {
public void handleNotification(Notification notification, Object obj) {
if(notification instanceof AttributeChangeNotification) {
AttributeChangeNotification attributeChange =
(AttributeChangeNotification) notification;
System.out.println("This notification is an
AttributeChangeNotification");
System.out.println("Observed Attribute: " +
attributeChange.getAttributeName() );
System.out.println("Old Value: " + attributeChange.getOldValue() );
System.out.println("New Value: " + attributeChange.getNewValue() );
}
}
}
JMX 1.2 では、リスンする MBean とは別の JVM で動作するリスナのプログラミングに関する特別な要件はありません。
(javax.management.MBeanServerConnection
を使用して) リモート JMX エージェントへの接続を確立すると、JMX によって JVM 間のデータの共有が処理されます。リモート JVM から接続を確立する手順については、「通知リスナとフィルタを登録する」を参照してください。
NotificationListener
クラスを作成するときに、以下の推奨事項を考慮してください。
MBean から送信されるすべての通知に 1 つのリスナを使用するのではなく、フィルタとリスナを組み合わせて使用するのが最適です。複数のリスナを使用すると JVM の初期化に時間がかかりますが、その代わりにコードの保守が容易になります。
たとえば、WebLogic Server ドメインに 3 つの JDBC データ ソースがある場合は、AttributeChangeNotification
をリスンする 1 つのリスナ クラスを作成できます。次に、3 つの登録クラスを作成します。各登録クラスは、リスナを JDBCDataSourceRuntimeMBean
の特定のインスタンスに登録します。
handleNotification
メソッド シグネチャには handback オブジェクトの引数が含まれているが、リスナでは handback オブジェクトからデータを取得したり、handback オブジェクトを操作したりする必要はない。handback オブジェクトは、リスナが MBean エミッタに関する情報を関連付けるのを助ける非透過的なオブジェクトです。handleNotification
メソッドの実装はできるだけ早く復帰する必要がある。if
文でラップして、リスナがすべての型の通知オブジェクトのメソッドを呼び出さないようにする。
JDK 1.5 では、指定する条件に一致した通知を転送するようにコンフィグレーションできる、簡単なフィルタ クラスが 2 つ用意されています。JDK のフィルタ クラスをコンフィグレーションするには、次の手順に従います。
javax.management.NotificationFilterSupport
または AttributeChangeNotificationFilter
のインスタンスを作成します。
J2SE 5.0 API 仕様の NotificationFilterSupport
または AttributeChangeNotificationFilter
を参照してください。
たとえば、以下のコード行では、State
という属性で変更があった場合にのみ属性変更通知を転送する AttributeChangeNotificationFilter
をコンフィグレーションします。
AttributeChangeNotificationFilter filter =
new AttributeChangeNotificationFilter();
filter.enableAttribute("State");
JDK のフィルタ クラスでは単純過ぎて要件に合わない場合は、より高度なカスタム フィルタ クラスを作成できます (J2SE 5.0 API 仕様の NotificationFilter
を参照)。ただし、カスタム フィルタを使用すると、リスナおよびフィルタのパッケージ化とデプロイメントが複雑になるため、できる限り JDK のフィルタ クラスを使用することをお勧めします。「リスナをパッケージ化し、WebLogic Server にデプロイする」を参照してください。
通知リスナ クラスを実装したら、MBean インスタンスにリスナを登録する (また、必要に応じてフィルタをコンフィグレーションして登録する) 追加のクラスを作成します。
通知リスナとフィルタを MBean に登録するには、次の手順に従います。
「MBean サーバへのリモート接続の作成」を参照してください。
カスタム MBean に登録するには、その MBean の JMX オブジェクト名を含む ObjectName
を作成します。J2SE 5.0 API 仕様の javax.management.ObjectName
を参照してください。
MBeanServerConnection.addNotificationListener (
ObjectName
name
, ObjectName
listener
, NotificationFilter
filter
,
Object
handback
)
メソッドに渡して、リスナとフィルタを登録します。
サンプル クラスでは、コード リスト 7-1 のリスナと JDK の AttributeChangeNotificationFilter
を、ドメインの ServerLifeCycleRuntimeMBean に登録します。このクラスでは handback オブジェクトは渡しません。
このサンプルで、weblogic
は MBean 属性を表示および変更するパーミッションを持つユーザです。MBean の表示や変更を行うパーミッションの詳細については、『ロールおよびポリシーによる WebLogic リソースの保護』の「ユーザ、グループ、セキュリティ ロール」を参照してください。
サンプル クラスには、RegisterListener
クラスをアクティブに保ち、メイン プログラムを終了しないコードも含まれています。リスナ クラスは、クラスの呼び出しとアクティブの維持を行う、より大きなアプリケーションのコンテキストで動作するため、このコードは通常は必要ありません。ここでは、簡単にコンパイルしてサンプルの機能を確認できるように含まれています。
import java.util.Hashtable;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.MalformedObjectNameException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import javax.management.AttributeChangeNotificationFilter;
public class RegisterListener {
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.mbeanserv
ers.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();
}
/*
* ServerLifeCycleRuntimeMBean の配列を取得する
*/
public static ObjectName[] getServerLCRuntimes() throws Exception {
ObjectName domainRT = (ObjectName) connection.getAttribute(service,
"DomainRuntime");
return (ObjectName[]) connection.getAttribute(domainRT,
"ServerLifecycleRuntimes");
}
public static void main(String[] args) throws Exception {
String hostname = args[0];
String portString = args[1];
String username = args[2];
String password = args[3];
try {
// リスナ クラスをインスタンス化する
MyListener listener = new MyListener();
AttributeChangeNotificationFilter filter =
new AttributeChangeNotificationFilter();
filter.enableAttribute("State");
initConnection(hostname, portString, username, password);
// MBean の名前とリスナ クラスを
// MBeanServer の addNotificationListener メソッドに渡す
ObjectName[] serverLCRT = getServerLCRuntimes();
int length= (int) serverLCRT.length;
for (int i=0; i < length; i++) {
connection.addNotificationListener(serverLCRT[i], listener,
filter, null);
System.out.println("\n[myListener]: Listener registered with"
+serverLCRT[i]);
}
// リモート クライアントをアクティブに保つ
System.out.println("pausing...........");
System.in.read();
} catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
JMX リスナは、リモート アプリケーションとして、WebLogic Server 起動クラスとして (サーバが起動するとすぐにリスナが使用可能になる)、または WebLogic Server にデプロイする他のアプリケーションの内部に、パッケージ化してデプロイできます。
JDK のフィルタを使用する場合は、フィルタ クラスをパッケージ化する必要はありません。常に JDK から使用できるようになっています。
表 7-3 では、リスナとカスタム フィルタをパッケージ化してデプロイする方法について説明します。
lib ディレクトリにコピーする。『ドメインのコンフィグレーションについて』の「ドメイン ディレクトリの内容」を参照。
|
||
lib ディレクトリにコピーする。『ドメインのコンフィグレーションについて』の「ドメイン ディレクトリの内容」を参照。
|
||
|
サーバや JDBC データ ソースなどの WebLogic Server リソースを作成すると、WebLogic Server はコンフィグレーション MBean を作成してドメイン実行時 MBean サーバに登録します。
これらのイベントをリスンするには、リスナを javax.management.MBeanServerDelegate
に登録します。javax.management.MBeanServerDelegate
は、MBean が登録または登録解除されるたびに、javax.management.MBeanServerNotification
型の通知を送信します。J2SE 5.0 API 仕様の MBeanServerDelegate
を参照してください。
コード リスト 7-3 のサンプル リスナに関する以下の点に注意してください。
ServerRuntimeMBean
のインスタンスには、オブジェクト名に Type=ServerRuntime
キー プロパティが含まれています。Type
属性が含まれており、その値によって通知の分類とフィルタ処理ができる。MBeanServerNotification
の Type
属性には、2 つの文字列「JMX.mbean.registered」または「JMX.mbean.unregistered」のうち 1 つだけ含まれています。また、JMX 通知には Type
属性の値を返す getType
メソッドも含まれます。
コード リスト 7-3 のリスナは、Type
属性の値に応じて異なるコード行を呼び出します。
JDBCDataSourceRuntimeMBean
が登録されると、リスナはこの MBean のオブジェクト名をカスタム メソッドに渡す。カスタム メソッドはリスナを登録し、JDBCDataSourceRuntimeMBean
用のフィルタをコンフィグレーションします。この MBean リスナは、MBean の Enabled
属性が変更されるとメッセージを送信します。
カスタム メソッドの実装クラスは (フィルタ クラスではなく) 登録クラス内にあるので、メソッドは登録クラスの MBean サーバへの接続を再利用できます。再利用によってリソースが有効に活用され、複数のクラスに資格と URL を格納する必要がなくなります。
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.MBeanServerNotification;
import javax.management.ObjectName;
public class DelegateListener implements NotificationListener {
public void handleNotification(Notification notification, Object obj) {
if (notification instanceof MBeanServerNotification) {
MBeanServerNotification msnotification =
(MBeanServerNotification) notification;
// MBeanServerNotification の Type 属性の値を取得する。
// 「JMX.mbean.registered」または「JMX.mbean.unregistered」が
// 含まれている
String nType = msnotification.getType();
// 登録または登録解除された MBean のオブジェクト名を
// 取得する
ObjectName mbn = msnotification.getMBeanName();
// WebLogic Server MBean のオブジェクト名には常に
// 「Type」キー プロパティが含まれている。これは、
// MBean のタイプ (ServerRuntime や Log など) を表す
String key = mbn.getKeyProperty("Type");
if (nType.equals("JMX.mbean.registered")) {
System.out.println("A " + key + " has been created.");
System.out.println("Full MBean name: " + mbn);
System.out.println("Time: " + msnotification.getTimeStamp());
if (key.equals("JDBCDataSourceRuntime")) {
// リスナを ServerRuntimeMBean に登録する。
// 「ListenToDelegate」クラスで
// 「registerwithServerRuntime」メソッドを定義すると、
// 「ListenToDelegate」が確立した接続を再利用できる。
// リソースを有効に利用する方法となるだけでなく、
// 複数のクラスに資格と URL を格納する必要がなくなる
//
ListenToDelegate.registerwithJDBCDataSourceRuntime(mbn);
}
}
if (nType.equals("JMX.mbean.unregistered")) {
System.out.println("An MBean has been unregistered");
System.out.println("Server name: " +
mbn.getKeyProperty("Name"));
System.out.println("Time: " + msnotification.getTimeStamp());
System.out.println("Full MBean name: "
+ msnotification.getMBeanName());
}
}
}
}
コード リスト 7-4 では、登録クラスのメソッドを示します。以下の点に注意してください。
MBeanServerDelegate
の JMX オブジェクト名は常に "JMImplementation:type=MBeanServerDelegate"
。main
メソッドでは、通知の Type 属性の値が「JMX.mbean.registered」または「JMX.mbean.unregistered」で始まる場合にのみ通知を転送するように、javax.management.NotificationFilterSupport
のインスタンスをコンフィグレーションする。registerwithJDBCDataSourceRuntime
メソッドは、指定された JDBCDataSourceRuntimeMBean
インスタンスに コード リスト 7-1 のリスナを登録する。このメソッドでは javax.management.AttributeChangeNotificationFilter
もコンフィグレーションします。これは、Enabled
という属性の変更について説明する AttributeChangeNotification
のみを転送します。
これらのメソッドをコンパイルして実行するには、コード リスト 7-2 の補助的なカスタム メソッドを使用し、生成されたクラスをリモート JMX クライアントとして実行します。
public static void main(String[] args) throws Exception {
String hostname = args[0];
String portString = args[1];
String username = args[2];
String password = args[3];
ObjectName delegate = new ObjectName(
"JMImplementation:type=MBeanServerDelegate");
try {
// リスナ クラスをインスタンス化する
StartStopListener slistener = new StartStopListener();
NotificationFilterSupport filter = new NotificationFilterSupport();
filter.enableType("JMX.mbean.registered");
filter.enableType("JMX.mbean.unregistered");
/* ドメイン実行時 MBean サーバへの接続を確立する
* カスタム メソッドを呼び出し、
* MBeanServerConnection のインスタンスを使用して接続を表す。
* カスタム メソッドは MBeanServerConnection を、
* クラス全体の静的変数「connection」に割り当てる
*/
initConnection(hostname, portString, username, password);
// MBean の名前とリスナ クラスを
// MBeanServer の addNotificationListener メソッドに渡す
connection.addNotificationListener(delegate, slistener, filter,
null);
System.out.println("\n[myListener]: Listener registered ...");
// リモート クライアントをアクティブに保つ
System.out.println("pausing...........");
System.in.read();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
// 登録される JDBCDataSourceRuntimeMBean の通知を受信した場合に
// リスナによって呼び出される
public static void registerwithJDBCDataSourceRuntime(ObjectName mbname) {
try {
MyListener mylistener = new MyListener();
AttributeChangeNotificationFilter filter =
new AttributeChangeNotificationFilter();
filter.enableAttribute("Enabled");
connection.addNotificationListener(mbname, mylistener,
filter, null);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
モニタ MBean をコンフィグレーションして使用するには、次の手順に従います。
JMX では、特定のタイプの変更を観察する特別なモニタ MBean を提供しています。
StringMonitorMBean
- 値が String
の属性を観察します。
このモニタを使用して、ServerLifeCycleRuntimeMBean
State
などの属性を定期的に観察します。
J2SE 5.0 API 仕様の javax.management.monitor.StringMonitor
を参照してください。このクラスは StringMonitorMBean
を実装しています。
GaugeMonitorMBean
- 値が Number
の属性を観察します。
このモニタを使用して、通常の処理の結果として値が変動する属性を観察します。属性の値が特定の範囲外まで変動した場合に通知を送信するように、ゲージ モニタをコンフィグレーションします。たとえば、ゲージ モニタを使用して ThreadPoolRuntimeMBean
StandbyThreadCount
属性をモニタし、サーバの未使用かつ使用可能なスレッドの数が許容範囲内にあることを確認できます。
J2SE 5.0 API 仕様の javax.management.monitor.GaugeMonitor
を参照してください。このクラスは GaugeMonitorMBean
を実装しています。
CounterMonitorMBean
- 値が Number
の属性を観察します。
このモニタを使用して、通常の処理の結果として値が増加する属性を観察します。属性の値が上限しきい値を超えた場合に通知を送信するように、カウンタ モニタをコンフィグレーションします。しきい値を増やして指定された時点でしきい値をリセットするように、カウンタ モニタをコンフィグレーションすることもできます。
たとえば、サーバでのヒット総数を追跡して、100 ヒット増えるたびに通知を受けるようにするには、ServerRuntimeMBean
SocketsOpenedTotalCount
属性を観察するカウンタ モニタを使用します。
J2SE 5.0 API 仕様の javax.management.monitor.CounterMonitor
を参照してください。このクラスは CounterMonitorMBean
を実装しています。
すべてのモニタ MBean は javax.management.monitor.MonitorNotification
型の通知を送信します。モニタ MBean は通知を生成するときに、特定の値を通知の Type
プロパティに書き込んで、通知を生成したイベントについて記述します。表 7-4 では、さまざまなタイプのモニタ MBean がエンコードする Type
プロパティの値について説明します。フィルタまたはリスナは通知の getType()
メソッドを使用して、Type
プロパティ内の String
を取得します。
エラーが発生した場合、すべてのモニタは通知の Type
プロパティに以下のいずれかの値をエンコードします。
jmx.monitor.error.mbean
- 観察対象 MBean が MBean サーバに登録されていないことを示します。通知では、観察対象のオブジェクト名が示されます。jmx.monitor.error.attribute
- 観察対象の属性が観察対象オブジェクトに存在しないことを示します。通知では、観察対象のオブジェクト名と属性名が示されます。jmx.monitor.error.type
- 観察対象の属性値のオブジェクト インスタンスが null
か、またはモニタに対応していない型であることを示します。通知では、観察対象のオブジェクト名と属性名が示されます。jmx.monitor.error.runtime
- 観察対象の属性値の取得中に (上記以外の理由で) 送出された例外が含まれます。
また、カウンタ モニタとゲージ モニタは、以下のような場合に Type
プロパティに jmx.monitor.error.threshold
をエンコードできます。
観察対象の属性が指定した条件に一致する場合、モニタ MBean は通知を送信します。MonitorNotification
用のリスナを作成するための特別な要件はありません。以下の点を除いて、手順は「通知リスナを作成する」で説明した手順と同じです。
コード リスト 7-5 を参照してください。
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.monitor.MonitorNotification;
public class MonitorListener implements NotificationListener {
public void handleNotification(Notification notification, Object obj) {
if(notification instanceof Notification) {
Notification notif = (Notification) notification;
System.out.println("Notification type" + notif.getType() );
System.out.println("Message: " + notif.getMessage() );
}
if (notification instanceof MonitorNotification) {
MonitorNotification mn = (MonitorNotification) notification;
System.out.println("Observed Attribute: " +
mn.getObservedAttribute());
System.out.println("Trigger: " + mn.getTrigger() );
}
}
}
モニタ MBean を使用するには、まず、MBean サーバ内でモニタ MBean のインスタンスを作成して登録する必要があります。次に、作成したモニタ MBean にリスナを登録します。この手順のすべてを 1 つのクラスの中で行うことができます。
モニタ MBean を登録し、リスナを登録して、モニタ MBean を起動するには、次の手順に従います。
「MBean サーバへのリモート接続の作成」を参照してください。
ObjectName
を作成します。
J2SE 5.0 API 仕様の javax.management.ObjectName
を参照してください。
オブジェクト名は組織の名前で始めて、モニタ MBean インスタンスの目的を明確に表すキー プロパティを含めることをお勧めします。
例 : mycompany:Name=SocketMonitor,Type=CounterMonitor
javax.management.MBeanServerConnection.createMBean(String
classname
ObjectName
name
)
メソッドを使用します。ここで、
設定する属性のガイドラインについては、J2SE 5.0 API 仕様の javax.management.monitoring
パッケージを参照してください。
addObservedObject(ObjectName
objectname
)
および addObservedAttribute(String
attributename
)
オペレーションを呼び出します。
モニタ MBean の addNotificationListener(NotificationListener
listener
, NotificationFilter
filter
, Object
handback
)
メソッドを呼び出します。
start()
オペレーションを呼び出して、モニタを起動します。
コード リスト 7-6 では、CounterMonitorMBean
を作成およびコンフィグレーションして、ドメインの各 ServerRuntimeMBean
インスタンスの SocketsOpenedTotalCount
属性を観察するクラスの main()
メソッドを示します (『WebLogic Server MBean リファレンス』の「SocketsOpenedTotalCount
」を参照)。
このサンプル コードでは、ServerRuntimeMBean
の複数のインスタンスをモニタできるように、ドメイン実行時 MBean サーバに接続します。以下の点に注意してください。
この main メソッドをコンパイルして実行するには、コード リスト 7-2 の補助的なカスタム メソッドを使用し、生成されたクラスをリモート JMX クライアントとして実行します。
public static void main(String[] args) throws Exception {
String hostname = args[0];
String portString = args[1];
String username = args[2];
String password = args[3];
try {
/* ドメイン実行時 MBean サーバへの接続を確立する
* カスタム メソッドを呼び出し、
* MBeanServerConnection のインスタンスを使用して接続を表す。
* カスタム メソッドは MBeanServerConnection を、
* クラス全体の静的変数「connection」に割り当てる
*/
initConnection(hostname, portString, username, password);
// モニタ MBean を作成して登録する
ObjectName monitorON =
new ObjectName("mycompany:Name=mySocketMonitor,Type=CounterMonitor");
String classname = "javax.management.monitor.CounterMonitor";
System.out.println("===> create mbean "+monitorON);
connection.createMBean(classname, monitorON);
// モニタ MBean をコンフィグレーションする
Number initThreshold = new Long(2);
Number offset = new Long(1);
connection.setAttribute(monitorON,
new Attribute("InitThreshold", initThreshold));
connection.setAttribute(monitorON, new Attribute("Offset", offset));
connection.setAttribute(monitorON,
new Attribute("Notify", new Boolean(true)));
// モニタする MBean のオブジェクト名を取得する
ObjectName[] serverRT = getServerRuntimes();
int length= (int) serverRT.length;
for (int i=0; i < length; i++) {
// ServerRuntime MBean の各インスタンスをモニタする MBean として設定する
System.out.println("===> add observed mbean "+serverRT[i]);
connection.invoke(monitorON, "addObservedObject",
new Object[] { serverRT[i] },
new String[] { "javax.management.ObjectName" });
Attribute attr = new Attribute("ObservedAttribute",
"SocketsOpenedTotalCount");
connection.setAttribute(monitorON, attr);
}
// リスナ クラスをインスタンス化し、カウンタ モニタ メッセージのみを
// 転送するようにフィルタをコンフィグレーションする
MonitorListener listener = new MonitorListener();
NotificationFilterSupport filter = new NotificationFilterSupport();
filter.enableType("jmx.monitor.counter");
filter.enableType("jmx.monitor.error");
// MBean サーバの addNotificationListener メソッドを使用して、
// モニタ MBean にリスナとフィルタを登録する
System.out.println("===> ADD NOTIFICATION LISTENER TO "+monitorON);
connection.addNotificationListener(monitorON, listener, filter, null);
System.out.println("\n[myListener]: Listener registered ...");
// モニタを起動する
connection.invoke(monitorON, "start", new Object[] { }, new String[] { });
// リモート クライアントをアクティブに保つ
System.out.println("pausing...........");
System.in.read();
} catch(Exception e) {
System.out.println("Exception: " + e);
e.printStackTrace();
}
}
![]() ![]() ![]() |