JMX 通知を使用したアプリケーション・ロギングのモニター

Java 開発者は、JMX 通知を使用してアプリケーション・サーバー・ログをモニターするプログラムを作成できます。

このタスクについて

最も一般的なログ・メッセージ・リスナーは Java で作成され、SOAP を使用してデプロイメント・マネージャーまたはアプリケーション・サーバーに接続します。 このトピックを使用して、ログ・イベントを listen する Java クライアントを作成します。
注: このトピックでは、 1 つ以上のアプリケーション・サーバー・ログ・ファイルを参照します。推奨される代替案として、分散システムや IBM® i システムの SystemOut.logSystemErr.logtrace.logactivity.log ファイルではなく、High Performance Extensible Logging (HPEL) ログおよびトレース・インフラストラクチャーを使用するようにサーバーを構成できます。また HPEL は、ネイティブ z/OS® ロギング機能と連携させて使用することができます。HPEL を使用する場合、LogViewer コマンド・ライン・ツールを サーバー・プロファイルの bin ディレクトリーから使用して、すべてのログ・ファイルにアクセスし、 情報をトレースできます。HPEL の使用について詳しくは、HPEL を使用してのアプリケーションの トラブルシューティングに関する情報を参照してください。
トラブルの回避 (Avoid trouble) トラブルの回避 (Avoid trouble): JMX 通知はサーバーをスローダウンさせる可能性があるため、大量のロギングがあるサーバーにリスナーを追加する場合は注意してください。gotcha

手順

  1. 必要なパッケージをインポートします。 通常は、Java プログラムの開始部分に次のようなインポート・ステートメントが必要になります。
    import javax.management.Notification;
    import javax.management.NotificationListener;
    import javax.management.ObjectName;
    import javax.management.InstanceNotFoundException;
    import javax.management.MalformedObjectNameException;
    
    import com.ibm.websphere.management.AdminClient;
    import com.ibm.websphere.management.AdminClientFactory;
    import com.ibm.websphere.management.exception.ConnectorException;
    さらに、メッセージ、および後続のステップでの呼び出しから返されるタイプを処理するために、次のようなインポート・ステートメントが必要になります。
    import java.util.Iterator;
    import java.util.Properties;
    import java.util.Set;
    import com.ibm.websphere.ras.RasMessage;
  2. NotificationListener インターフェースを実装する Java クラスを作成します。
  3. handleNotification メソッドを実装します。 以下の例は、Java コンソールにメッセージ・テキストを書き込むサンプルです。
    public void handleNotification(Notification notification, Object handback) {
            RasMessage rasMessage = (RasMessage)notification.getUserData() ;
            System.out.println("Localized message: " + rasMessage.getLocalizedMessage(null));
    }
  4. JMX MBeans のモニター対象となるサーバーの SOAP ポートに接続します。 以下のコードは、指定されたホストと指定されたポートを使用して SOAP 接続される AdminClient オブジェクトを作成します。
            AdminClient adminClient = null ;
            String hostName = "someHostName";
            String soapPort = "8880";
    
            Properties connectProps = new Properties();
            connectProps.setProperty(AdminClient.CONNECTOR_TYPE, "SOAP");
            connectProps.setProperty(AdminClient.CONNECTOR_HOST, hostName);
            connectProps.setProperty(AdminClient.CONNECTOR_PORT, soapPort);
    
            try {
                adminClient = AdminClientFactory.createAdminClient(connectProps);
            } catch (ConnectorException e) {
                // error handling code
            } 
  5. RasLoggingService MBean に関する MBean オブジェクト名を取得します。 以下のコードは RasLoggingService MBean オブジェクト名を取得します。
            String queryString = "WebSphere:cell="+cellName+",node="+nodeName+",process="+serverName+",
    type=RasLoggingService,*" ;
            Set<ObjectName> objectMBeans = null;
            try {
                ObjectName queryName = new ObjectName(queryString);
                objectMBeans = (Set<ObjectName>)adminClient.queryNames(queryName, null);
                if (objectMBeans.size() > 1) {
                    // error handling code to deal with the case where we get more than one name returned.
                }
            } catch (MalformedObjectNameException e) {
                // error handling code
            } catch (ConnectorException e) {
                // error handling code
            }
    
            if (objectMBeans.isEmpty()) {
                // error handling code to deal with the case where the MBean is not found
            }
    
            Iterator<ObjectName> objectNames = objectMBeans.iterator() ;
            ObjectName objectName = objectNames.next() ;
  6. 通知リスナーを追加します。 以下のサンプル・コードは、通知リスナーを追加し、通知が処理されるのを 60 秒待機した後、その通知リスナーを除去します。 リスナーは必要な間だけ接続しておくことができます。
            try {
                adminClient.addNotificationListener(objectName, this, null, null);
                Thread.sleep(60 * 1000) ;
                adminClient.removeNotificationListener(objectName, this) ;
            } catch (InstanceNotFoundException e) {
                // error handling code
            } catch (ConnectorException e) {
                // error handling code
            } catch (Exception e) {
                // error handling code
            }
  7. 必要な jar をクラスパスに追加します。 コードのコンパイルと実行を可能にするため、管理クライアント jar ファイルをクラスパスに追加します。 管理クライアント jar ファイルの場所は <install_root>/runtimes ディレクトリー内です。

タスクの結果

これで、アプリケーション・サーバーからのログ・イベント通知を listen し、通知の結果として処置を実行できる Java プログラムが作成されました。

トピックのタイプを示すアイコン タスク・トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=ttrb_HPELNotification
ファイル名:ttrb_HPELNotification.html