イベント・ソース例 (EventSource.java) は、ハードコーディングされた単一のイベントを 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
メソッドは、以下のステップを実行します。
この例で設定される他のプロパティー (version、severity、および msg) は、すべてストリングまたは整数で表記される単純プロパティーです。
イベント・ソースの例の次のメソッドは 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) は、以下のステップを実行します。
イベント・ソースの例の最後のメソッド 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() メソッドは、以下のステップを実行します。
関連概念
Common Base Event フォーマットのイベントの Enterprise Console イベント・フォーマットへの変換