BEA ホーム | 製品 | dev2dev | support | askBEA |
![]() |
![]() |
|
![]() |
e-docs > WebLogic Server > WebLogic Server ロギング サービスの使い方 > WebLogic Server ログからのメッセージのリスン |
WebLogic Server ロギング サービスの使い方
|
WebLogic Server ログからのメッセージのリスン
各 WebLogic Server は、そのログ メッセージを JMX 通知フォームでブロードキャストします。ブロードキャストには、WebLogic Server インスタンス、そのサブシステム、およびアプリケーションが WebLogic Server ログに書き込むすべてのメッセージ(DEBUG 重大度のメッセージを除く)が含まれます。管理サーバは、これらの通知をリスンし、それらのサブセットをドメイン全体のログ ファイルに書き込みます(1-6 ページの図1-1 「WebLogic Server ロギング サービス」を参照)。
アプリケーションは、WebLogic Server インスタンスからブロードキャストされるログ メッセージをリスンできます。たとえば、特定のサブシステムのエラーを知らせるログ メッセージをリスンできます。この場合、アプリケーションは以下のようなアクションを実行できます。
これらの通知をリスンするには、通知リスナを作成し、そのリスナを WebLogic Server ブロードキャスト MBean の LogBroadcasterRuntimeMBean に登録します。通知リスナは、JMX NotificationListener インタフェースの実装です。LogBroadcasterRuntimeMBean は、通知を発するときに登録済みリスナの handleNotification メソッドを使用して WebLogicLogNotification オブジェクトを受け渡します。詳細については、図 4-1 を参照してください。
WebLogicLogNotification オブジェクトの詳細については、WebLogicLogNotification オブジェクトを参照してください。
アプリケーションが WebLogic Server ログからの通知をリスンできるようにするには、以下のタスクを実行します。
注意: アプリケーションが WebLogic Server JVM の外部で実行される場合、そのアプリケーションは WebLogic Server ログ通知をリスンできますが、WebLogic ロギング サービスを利用してメッセージをブロードキャストすることはできません。
通知リスナを作成するための手順は、アプリケーションが WebLogic Server JVM 内で実行されているかどうかによって異なります。
WebLogic Server JVM 内で実行されるアプリケーション用の通知リスナの作成
アプリケーションが WebLogic Server JVM 内で実行される場合、次の手順に従います。
NotificationListener の詳細については、javax.management.Notification Javadoc(http://jcp.org/aboutJava/communityprocess/final/jsr003/index.html)を参照してください。
図 4-2 に、JSP が WebLogic Server JVM 内で実行されているシステムを示します。JSP は、LogBroadcasterRuntimeMBean からの通知をリスンします。
リスト4-1 に、ローカル クライアントの通知リスナの例を示します。このリスナは、WebLogicLogNotification ゲッター メソッドを使用して、受信したすべてのメッセージを出力します。詳細については、WebLogicLogNotification オブジェクトを参照してください。
コード リスト 4-1 ローカル クライアント用の通知リスナの例
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.Notification.*;
...
public class MyNotificationListener implements
NotificationListener {
...
public void handleNotification(Notification notification, Object obj) {
WebLogicLogNotification wln = (WebLogicLogNotification)notification;
System.out.println("WebLogicLogNotification");
System.out.println(" type = " + wln.getType());
System.out.println(" message id = " + wln.getMessageId());
System.out.println(" server name = " + wln.getServername());
System.out.println(" timestamp = " + wln.getTimeStamp());
System.out.println(" message = " + wln.getMessage() + "¥n");
}
アプリケーションが WebLogic Server JVM の外部で実行される場合、次の手順に従います。
実装には RemoteNotificationListener.handleNotification() メソッドを含める必要があります。詳細については、weblogic.management.RemoteNotificationListener Javadoc を参照してください。
図 4-3 に、JSP が WebLogic Server JVM 内で実行され、アプリケーションがリモート JVM で実行されるシステムを示します。メッセージをリスンするために、JSP は NotificationListener を実装し、リモート アプリケーションは RemoteNotificationListener を実装します。
リスト4-2 に、リモート クライアントの通知リスナの例を示します。
コード リスト 4-2 リモート クライアント用の通知リスナの例
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.Notification.*;
import weblogic.management.RemoteNotificationListener;
import weblogic.management.logging.WebLogicLogNotification;
...
public class MyRemoteNotificationListener implements
RemoteNotificationListener {
...
public void handleNotification(Notification notification, Object obj) {
WebLogicLogNotification wln = (WebLogicLogNotification)notification;
}
通知リスナを実装したら、そのリスナを WebLogic Server インスタンスの LogBroadcasterRuntimeMBean に登録する必要があります。各 WebLogic Server は独自のメッセージをブロードキャストするため、各 WebLogic Server で通知リスナを登録する必要があります。
この節では、リスナを登録するために使用するコードについて説明します。ここに示すコード部分は、クライアント アプリケーションの起動時、WebLogic Server の起動時、またはアプリケーションで通知を受け取る場合に実行するクラスに追加できます。
WebLogic Server インスタンスの LogBroadcasterRuntimeMBean に登録するには、コードで次の処理を行う必要があります。
addNotificationListener API の使用
addNotificationListener API の構文は次のとおりです。
MBeanServer.addNotificationListener(ObjectName name,
NotificationListener listener,
NotificationFilter filter,
java.lang.Object handback)
addNotificationListener API の詳細については、javax.managment.MBeanServer の Javadoc(http://jcp.org/aboutJava/communityprocess/final/jsr003/index.html)を参照してください。
以下に、「手順 1 : 通知リスナの作成」で定義したリスナの登録例を示します。リスト4-3 とリスト4-4 の例は、以下のことを行います。
リスト4-3 では、WebLogicObjectName を使用して LogBroadcasterRuntimeMBean オブジェクト名を指定しています。
コード リスト 4-3 WebLogicObjectName の使用
public void find(String host,
int port,
String username
String password){
String url = "t3://" + host +
":" + port;
//サーバの MBeanHome インタフェースを取得
try {
serverSpecificHome = (MBeanHome)Helper.getMBeanHome(username,
password,
url,
peach);
} catch (IllegalArgumentException iae) {
System.out.println("Illegal Argument Exception: " + iae);
}
//MBeanHome を使用してサーバの MBeanServer インタフェースを取得
MBeanServer mServer = serverSpecificHome.getMBeanServer();
//サーバの LogBroadcasterRuntimeMBean の WebLogicObjectName を作成
WebLogicObjectName logBCOname = new WebLogicObjectName("WebLogicLogBroadcaster",
"LogBroadcasterRuntime",
myDomain,
myServer);
//リスナ オブジェクトをインスタンス化
MyRemoteNotificationListener myListener = new MyRemoteNotificationListener();
//リスナを登録
mServer.addNotificationListener( logBCOname,
myListener,
null,
null);
}
リスト4-4 では、MBeanHome.getMBeanByClass を使用して LogBroadcasterRuntimeMBean オブジェクト名を検索しています。
コード リスト 4-4 getObjectName() の使用
public void find(String host,
int port,
String username
String password){
String url = "t3://" + host +
":" + port;
//サーバの MBeanHome インタフェースを取得
try {
serverSpecificHome = (MBeanHome)Helper.getMBeanHome(username,
password,
url,
peach);
} catch (IllegalArgumentException iae) {
System.out.println("Illegal Argument Exception: " + iae);
}
//MBeanHome を使用してサーバの MBeanServer インタフェースを取得
MBeanServer mServer = serverSpecificHome.getMBeanServer();
//getMBeanByClass を使用してオブジェクトを検索
LogBroadcasterRuntimeMBean logBCOname = (LogBroadcasterRuntimeMBean)
home.getMBeanByClass(Class.forName
("weblogic.management.runtime.LogBroadcasterRuntimeMBean")
);
//リスナ オブジェクトをインスタンス化
MyRemoteNotificationListener myListener = new MyRemoteNotificationListener();
//リスナを登録
mServer.addNotificationListener( logBCOname,
myListener,
null,
null);
}
リスト4-5 は、weblogic.Admin GET を使用して LogBroadcasterRuntimeMBean オブジェクト名を検索したことを前提としています。また、この例には weblogic.Admin GET が返すオブジェクト名のフォーマットも示されています。
コード リスト 4-5 weblogic.Admin GET の使用
MyRemoteNotificationListener myListener = new MyRemoteNotificationListener();
MBeanServer mServer = home.getMBeanServer();
ObjectName logBCOname = new ObjectName("mydomain:Location=myserver,Name=TheLogBroadcaster,Type=LogBroadcast
erRuntime");
mServer.addNotificationListener( logBCOname,
myListener,
null,
null);
デフォルトでは、前節で登録した通知フィルタは LogBroadcasterRuntimeMBean からのすべての通知をリスンしてアプリケーションに送信します。登録フィルタを作成すると、アプリケーションに関連する通知だけを送信するよう LogBroadcasterRuntimeMBean をコンフィグレーションできます。フィルタでは、ユーザが作成した条件と通知が一致するかどうかがチェックされます。true と評価された場合にのみ、LogBroadcasterRuntimeMBean は通知を送信します。
WebLogicLogNotification オブジェクトとしてキャストされた通知を検索するために、WebLogicLogNotification ゲッター メソッドを使用できます。たとえば、ゲッター メソッドを使用すると、メッセージ タイムスタンプ、重大度、ユーザ ID、メッセージを生成したサブシステムの名前、メッセージ テキストなどを取得できます。詳細については、「WebLogicLogNotification オブジェクト」を参照してください。
WebLogicLogNotification オブジェクト
WebLogic Server が生成するすべてのメッセージは、weblogic.management.logging.WebLogicLogNotification オブジェクトとして機能します。WebLogicLogNotification オブジェクトには以下のフィールドで構成されます。
WebLogicLogNotification は、javax.management.Notification からゲッター メソッド継承し、ログ メッセージ内のフィールドごとに 1 つのゲッター メソッドを提供します(図 4-4を参照)。
これらのゲッター メソッドを使用すると、WebLogicLogNotification 内の情報を検索または出力できます。詳細については、weblogic.management.logging.WebLogicLogNotification Javadoc を参照してください。
図4-4 WebLogicLogNotification ゲッター メソッド
リスト4-6 に、WebLogicLogNotification.getType メソッドを使用する NotificationFilter の例を示します。
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.Notification.*;
import weblogic.management.logging.WebLogicLogNotification;
....
public class MyLogNotificationFilter implements NotificationFilter,
java.io.Serializable {
public MyLogNotificationFilter() {
subsystem = "";
}
public boolean isNotificationEnabled(Notification notification) {
if (!(notification instanceof WebLogicLogNotification)) {
return false;
}
WebLogicLogNotification wln = (WebLogicLogNotification)notification;
if (subsystem == null ||
subsystem.equals("")) {
return true;
}
StringTokenizer tokens = new StringTokenizer(wln.getType(), ".");
tokens.nextToken();
tokens.nextToken();
return (tokens.nextToken().equals(subsystem));
}
public void setSubsystemFilter(String newSubsystem) {
subsystem = newSubsystem;
}
}
![]() |
![]() |
![]() |
![]() |
||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |