このイベント・ソースの例は、WS-Notification 1.0 通知作成機能のサンプル・コードです。これは、Apache Axis のライブラリーおよび Eclipse TPTP オープン・ソースのプロジェクトを利用します。
イベント・ソースが実行する必要のある以下のステップを例示します。
EventSourceWsn10.java のソース・コードは、<sdk_install_dir>/samples/wsnt-axis/src/com/wtci/samples/axis/source
ディレクトリーにあります。
この例では、Apache Axis クラス、Eclipse Hyades ロギング・クラス、および wsdl ファイルから生成されるいくつかのクラスを使用します。以下のインポート・ステートメントを使用して、必要なパッケージにアクセスします。
import com.ibm.wtci.samples.axiswsn10.wsa.AttributedURI; import com.ibm.wtci.samples.axiswsn10.wsa.EndpointReferenceType; import com.ibm.wtci.samples.axiswsn10.wsn.NotificationConsumer; import com.ibm.wtci.samples.axiswsn10.wsn.NotificationConsumerService; import com.ibm.wtci.samples.axiswsn10.wsn.NotificationConsumerServiceLocator; import com.ibm.wtci.samples.axiswsn10.wsn.NotificationMessageHolderType; import com.ibm.wtci.samples.axiswsn10.wsn.TopicExpressionType; import org.apache.axis.encoding.TypeMapping; import org.apache.axis.encoding.TypeMappingRegistry; import org.apache.axis.message.MessageElement; import org.apache.axis.types.URI; 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; import javax.xml.namespace.QName;
EventSourceWsn10.java の main
メソッドでは、デフォルトのコンストラクターを使用して、EventSourceWsn10 の新規インスタンスを作成します。次に、専用の 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(); } }
EventSourceWsn10.java の createEvents
メソッドは、イベントを作成して最小のプロパティー・データ・セットを取り込むために使用されるヘルパー・メソッドです。このメソッドは、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; }
createEvents
メソッドは、以下のステップを実行します。
注:
situation と同様、sourceComponentId も複雑なプロパティーです。ただし、子プロパティーはすべてストリングで表記される単純属性であるため、特殊な Java オブジェクトを別々にインスタンス化するのではなく、 setSourceComponentId() ヘルパー・メソッドを使用できます。この例で設定される他のプロパティー (version、severity、および msg) は、すべてストリングまたは整数で表記される単純プロパティーです。
createEvents
メソッドが、プロパティー・データが取り込まれたイベントを戻します。イベント・ソースの例の次のメソッドは 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」という名前と「tec」という dialect 属性の値を持つ //Topic エレメントを作成する MessageElement[] msgTopic = new MessageElement[1]; msgTopic[0] = new MessageElement(); msgTopic[0].setName("Topic"); msgTopic[0].setAttribute("dialect", "none"); TopicExpressionType topicExpType = new TopicExpressionType(); topicExpType.set_any(msgTopic); //通知の Topic を設定する notificationArray[i].setTopic(topicExpType); //イベントが通知のメッセージとなるように設定する notificationArray[i].setMessage(events[i]); //通知にイベントの作成元についての情報を //設定する EndpointReferenceType endPointRef = new EndpointReferenceType(); AttributedURI au = new AttributedURI(); URI value = new URI("protocol", "your.event.source.address"); au.set_value(value); endPointRef.setAddress(au); notificationArray[i].setProducerReference(endPointRef); } return notificationArray; }
createNotificationMessage(event) は、以下のステップを実行します。
イベント・ソースの例の最後のメソッド sendEvent() には、メインプログラム・ロジックが含まれます。このメソッドは、イベントの送信など、NotificationConsumer クライアントとのすべての対話を処理します。
private void sendEvent() throws Exception { //最初のステップでイベントを作成する CommonBaseEvent[] events = createEvents(); //イベントをカプセル化する通知メッセージを作成する NotificationMessageHolderType[] notificationArray = createNotificationMessage(events); // NotificationConsumer10Service Web サービスのアドレスを // 取得する String endpoint = System.getProperty("service.address"); if (endpoint == null) { //アドレスが指定されていない場合、Web サービスは axis 用のポート 8080 の // ローカル・ホストで稼働していると想定される endpoint = "http://localhost:8080/axis/services/NotificationConsumer10Soap"; } //取得したアドレスの URL オブジェクトを作成する URL serviceURL = new URL(endpoint); //NotificationConsumerServiceLocator のインスタンスを作成する NotificationConsumerService notifierSvc = new NotificationConsumerServiceLocator(); //NotificationConsumerService を使用して、サービスの // NotificationConsumer オブジェクトを取得する NotificationConsumer notifier = notifierSvc.getNotificationConsumer10Soap(serviceURL); //CbeSerializer オブジェクトのインスタンスを登録して //CommonBaseEvent タイプのオブジェクトをシリアライズし、 //ワイヤー経由で送信されるようにする TypeMappingRegistry reg = (TypeMappingRegistry) notifierSvc.getTypeMappingRegistry(); TypeMapping tm = (TypeMapping) reg.getTypeMapping(endpoint); QName qn = new QName("http://www.ibm.com/AC/commonbaseevent1_0_1", "CommonBaseEvent"); tm.register(CommonBaseEvent.class, qn, new CbeSerializerFactory(), null); //通知を送信する notifier.notify(notificationArray); }
sendEvent() メソッドは、以下のステップを実行します。
関連概念
Common
Base Event フォーマットのイベントの Enterprise Console イベント・フォーマットへの変換