WS-Notification サンプルのウォークスルー

イベント・ソース例 (EventSource.java) は、ハードコーディングされた単一のイベントを Web サービスに送信する単純なイベント・ソースです。イベント・ソースが実行する必要のある以下のステップを例示します。

  1. イベント・ファクトリーの取得
  2. イベントの作成と必須データの取り込み
  3. 生成された Web サービスのクライアント・サイド・スタブを使用したイベントの送信

EventSource.java のソース・コードは、SDK の <sdk_install_dir>/samples/wsnt-was/src/com/wtci/samples/was/source ディレクトリーにあります。

この例では、Hyades ロギング・クラス、および wsdl ファイルから生成されるクラスを使用します。以下のインポート・ステートメントを使用して、必要なパッケージにアクセスします。

import com.ibm.wtci.samples.waswsn10.emitter.NotificationConsumerService;
import com.ibm.wtci.samples.waswsn10.emitter.NotificationConsumerServiceLocator;
import com.ibm.wtci.samples.waswsn10.wsn.NotificationConsumer;
import com.ibm.wtci.samples.waswsn10.wsn.NotificationMessageHolderType;

import com.ibm.ws.webservices.engine.xmlsoap.SOAPElement;
import com.ibm.ws.webservices.engine.xmlsoap.SOAPFactory;

import org.eclipse.hyades.logging.events.cbe.CommonBaseEvent;
import org.eclipse.hyades.logging.events.cbe.EventFactory;
import org.eclipse.hyades.logging.events.cbe.EventFactoryFactory;
import org.eclipse.hyades.logging.events.cbe.Situation;
import org.eclipse.hyades.logging.events.cbe.util.EventFormatter;

import java.net.URL;

EventSource.java の main メソッドは、デフォルトのコンストラクターを使用して EventSource の新規インスタンスを作成します。次に、専用の sendEvent メソッド (メインプログラム・ロジックを含む) を呼び出します。sendEvent メソッドが戻ると、main() メソッドが標準出力に「Notification sent」というメッセージを出力します。

    /**
     * Main method for the event source.
     *
     * @param args
     *            arguments passed from the command line
     */
    public static void main(String args[]) {
        EventSourceWsn10 source = new EventSourceWsn10();
        try {
            source.sendEvent();
            System.out.println("Notification sent");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

イベントの作成および取り込み

EventSource.java の createEvent() メソッドは、イベントを作成して最小のプロパティー・データ・セットを取り込むために使用されるヘルパー・メソッドです。 このメソッドは、Web サービスに送信されるイベントを作成するために sendEvent メソッドによって呼び出されます。

    public static CommonBaseEvent[] createEvents() throws Exception {
        CommonBaseEvent[] events = null;

        String cbeFile = System.getProperty("cbe.file");
        if (cbeFile != null) {
            events = EventFormatter.eventsFromCanonicalXMLDoc(cbeFile);
        } else {
            // 最初のステップでイベント・ファクトリーにアクセスする
            EventFactory eventFactory = EventFactoryFactory.createEventFactory();

            // 拡張名を持つイベントを作成する
            CommonBaseEvent event = eventFactory.createCommonBaseEvent("EVENT");
            event.setCreationTimeAsLong(System.currentTimeMillis());

            // イベントの必須の状態説明を設定する
            Situation situation = eventFactory.createSituation();
            situation.setCategoryName(Situation.REPORT_SITUATION_CATEGORY);
            situation.setReportSituation("INTERNAL", "Succeeded");
            event.setSituation(situation);

            // イベント・ソース用に必須コンポーネント ID を
            // 設定する
            event.setSourceComponentId("Event Source",
                "source.EventSource",
                "createEvent()",
                "http://www.ibm.com/namespaces/autonomic/Tivoli/Samples",
                "Sample",
                "unknown",
                "hostname");
            // Common Base Event のバージョンを設定する
            event.setVersion("1.0.1");
            // オプションのフィールドを設定する
            event.setSeverity((short) 10);
            event.setMsg("Common Event Infrastructure Tutorial");

            events = new CommonBaseEvent[] { event };
        }

        return events;
    }

createEvent メソッドは、以下のステップを実行します。

  1. EventFactoryFactory の createEventFactory() メソッドを使用して、 新規 EventFactory オブジェクトを作成します。
  2. イベント・ファクトリーの createCommonBaseEvent(String) メソッドを使用して、 新規イベント・オブジェクト (CommonBaseEvent のインスタンス) を作成します。指定されたストリング ("EVENT") はイベントの extensionName プロパティーを設定するために使用され、現行システム時刻は creationTime プロパティーを設定するために使用されます。
  3. イベントの situation プロパティーの値を設定します。situation は複雑なプロパティーであるため、 特殊な Java クラスによって表記されます。したがって、situation プロパティーを設定するには、以下のような個別のステップを実行する必要があります。
    1. 新規の状態オブジェクトを作成する
    2. setter メソッドを使用して、必要なプロパティー・データを状態オブジェクトに取り込む
    3. データが取り込まれた状態オブジェクトを指定して Event.setSituation() メソッドを呼び出すことにより、イベントの situation プロパティーを設定する
  4. 他のいくつかのイベント・プロパティーの値 (必須の場合もあれば、オプションの場合もある) を設定します。これらのプロパティーには、severitymsgversion、および sourceComponentId が含まれます。
    注: situation と同様、sourceComponentId も複雑なプロパティーです。ただし、子プロパティーはすべてストリングで表記される単純属性であるため、特殊な Java オブジェクトを別々にインスタンス化するのではなく、 setSourceComponentId() ヘルパー・メソッドを使用できます。

    この例で設定される他のプロパティー (versionseverity、および msg) は、すべてストリングまたは整数で表記される単純プロパティーです。

  5. 最後に、createEvent() メソッドが、プロパティー・データが取り込まれたイベントを戻します。

通知メッセージの作成

イベント・ソースの例の次のメソッドは createNotificationMessage(event) です。これは、通知メッセージを作成し、メソッドにパラメーターとして渡されるイベントをカプセル化するのに使用されるヘルパー・メソッドです。このメソッドは、Web サービスに送信される通知メッセージを作成するために sendEvent メソッドによって呼び出されます。

    public static NotificationMessageHolderType[] createNotificationMessage(
            CommonBaseEvent events[]) throws Exception {
        NotificationMessageHolderType[] notificationArray = 
            new NotificationMessageHolderType[events.length];
        for (int i = 0; i < events.length; i++) {

            //NotificationMessageHolderType のインスタンスを作成する
            notificationArray[i] = new NotificationMessageHolderType();

            //「Topic」という名前を持つ Topic エレメントを作成する
            SOAPFactory soapFactory = new SOAPFactory();
            SOAPElement topicSE = (SOAPElement)
                soapFactory.createElement("Topic");
            SOAPElement topicSEChild = (SOAPElement)
                topicSE.addChildElement("Topic");
            topicSEChild.setAttribute("dialect","none");
            notificationArray[i].setTopic(topicSE);

            //イベントが通知のメッセージとなるように設定する
            SOAPElement messageSE = (SOAPElement)
                soapFactory.createElement("Message");
            messageSE.addNamespaceDeclaration("ns2",
                "http://www.ibm.com/AC/commonbaseevent1_0_1");
            messageSE.addAttribute("http://www.w3.org/2001/XMLSchema-instance",
                "type",
                "ns2:CommonBaseEvent");
            String cbeStr = EventFormatter.toCanonicalXMLString(events[i]);
            SOAPElement cbeSE = (SOAPElement)
                soapFactory.createElementFromXMLString(cbeStr);
            messageSE.addChildElement(cbeSE);
            notificationArray[i].setMessage(messageSE);

            //通知にイベントの作成元についての情報を
            //設定する
            SOAPElement producerSE = (SOAPElement)
                soapFactory.createElement("ProducerReference");
            SOAPElement producerSEChild = (SOAPElement)
                soapFactory.createElement("Address",
                "ns1",
                "http://schemas.xmlsoap.org/ws/2003/03/addressing");
            producerSEChild.addTextNode("protocol://your.event.source.address");
            producerSE.addChildElement(producerSEChild);
            notificationArray[i].setProducerReference(producerSE);
        }
        return notificationArray;
    }

createNotificationMessage(event) は、以下のステップを実行します。

  1. 送信される通知の情報を保持する NotificationMessageHolderType オブジェクトを作成します。
  2. 「Topic」という名前の通知の Topic エレメントを作成します。通知の受信時に Topic エレメント内のすべてが無視されることを示すために、ネストされた子エレメントが追加されます。
  3. 通知のメッセージがメソッドに渡されるイベントとなるように設定します。
  4. 通知にイベントの作成元についての情報を設定します。
  5. このように作成し、情報を取り込んだ通知メッセージを呼び出し元に戻します。

イベントの送信

イベント・ソースの例の最後のメソッド sendEvent() には、メインプログラム・ロジックが含まれます。このメソッドは、イベントの送信など、NotificationConsumer クライアントとのすべての対話を処理します。

    private void sendEvent() throws Exception {
        //最初のステップでイベントを作成する
        CommonBaseEvent[] events = createEvents();

        //イベントをカプセル化する通知メッセージを作成する
        NotificationMessageHolderType[] notification =
            createNotificationMessage(events);

        //NotificationConsumerService Web サービスのアドレスを取得する
        String endpoint = System.getProperty("service.address");
        if (endpoint == null) {
            // アドレスが指定されていない場合、Web サービスは was 用のポート 9080 の
            // ローカル・ホストで稼働していると想定される
            endpoint = "http://localhost:9080/wsnt-was/services/NotificationConsumer10Soap";
        }

        //取得したアドレスの URL オブジェクトを作成する
        java.net.URL serviceURL = new URL(endpoint);

        //NotificationConsumerServiceLocator のインスタンスを作成する
        NotificationConsumerService notifierSvc =
            new NotificationConsumerServiceLocator();
        NotificationConsumer notifier =
            notifierSvc.getNotificationConsumer10Soap(serviceURL);

        //通知を送信する
        notifier.notify(notification);

    }

sendEvent() メソッドは、以下のステップを実行します。

  1. createEvent() ヘルパー・メソッドを呼び出して、新規イベントを作成し、データを取り込みます。
  2. createNotificationMessage(event) ヘルパー・メソッドを呼び出して、 作成されたイベントをカプセル化する通知メッセージを作成します。
  3. システム・プロパティーから、NotificationConsumer Web サービスのアドレスを取得して、通知要求にサービスを提供します。
  4. NotificationConsumerServiceLocator をインスタンス化して、NotificationConsumer オブジェクトを取得するために使用します。
  5. NotificationConsumer の notify() メソッドを呼び出して、イベントを送信します。これにより、イベントをカプセル化する SOAP メッセージが生成されます。


関連トピック
サンプルのビルド
サンプルの実行


関連概念
Common Base Event フォーマットのイベントの Enterprise Console イベント・フォーマットへの変換