事件來源範例 (EventSource.java) 是簡式事件來源,它會將單一且寫在程式中的事件傳送給「Web 服務」。它會示範事件來源必須遵循的下列步驟:
可在 SDK 的 <sdk_install_dir>/samples/wsnt-was/src/com/wtci/samples/was/source
目錄中取得 EventSource.java 的程式碼。
此範例使用 Hyades 記載類別以及從 wsdl 檔案產生的類別。請使用下列 import 陳述式來存取必要的套件:
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()
方法會將「已送出通知」訊息列印至標準輸出。
/** * 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() 方法是 helper 方法,可使用它來建立事件,
並將最小一組的內容資料移入該事件。sendEvent
方法會呼叫此方法,
以建立將傳送給「Web 服務」的事件。
public static CommonBaseEvent[] createEvents() throws Exception { CommonBaseEvent[] events = null; String cbeFile = System.getProperty("cbe.file"); if (cbeFile != null) { events = EventFormatter.eventsFromCanonicalXMLDoc(cbeFile); } else { // The first step is accessing the event factory EventFactory eventFactory = EventFactoryFactory.createEventFactory(); // Creating an event with an extension name. CommonBaseEvent event = eventFactory.createCommonBaseEvent("EVENT"); event.setCreationTimeAsLong(System.currentTimeMillis()); // Setting the mandatory situation description for the event. Situation situation = eventFactory.createSituation(); situation.setCategoryName(Situation.REPORT_SITUATION_CATEGORY); situation.setReportSituation("INTERNAL", "Succeeded"); event.setSituation(situation); // Setting the mandatory component identification for the // event source event.setSourceComponentId("Event Source", "source.EventSource", "createEvent()", "http://www.ibm.com/namespaces/autonomic/Tivoli/Samples", "Sample", "unknown", "hostname"); // Setting Common Base Event version event.setVersion("1.0.1"); // Setting optional fields event.setSeverity((short) 10); event.setMsg("Common Event Infrastructure Tutorial"); events = new CommonBaseEvent[] { event }; } return events; }
createEvent
方法會執行下列步驟:
範例所設定的其他屬性內容 (version、severity 及 msg) 全部都是以字串或整數來呈現的簡式內容屬性。
事件來源範例的下一個方法 createNotificationMessage(event) 是 helper 方法, 可使用它來建立會將傳入的事件封裝為方法之參數的通知訊息。sendEvent 方法會呼叫此方法,以建立將傳送給「Web 服務」的通知訊息。
public static NotificationMessageHolderType[] createNotificationMessage( CommonBaseEvent events[]) throws Exception { NotificationMessageHolderType[] notificationArray = new NotificationMessageHolderType[events.length]; for (int i = 0; i < events.length; i++) { //Creating an instance of NotificationMessageHolderType notificationArray[i] = new NotificationMessageHolderType(); //Creating a Topic element with the name '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); //Setting the event to be the message of the notification 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); //Setting information about the producer of the event in //the notification 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 { //The first step is creating an event CommonBaseEvent[] events = createEvents(); //Creating the Notification message encapsulating the event NotificationMessageHolderType[] notification = createNotificationMessage(events); //Obtaining the address of the NotificationConsumerService webservice String endpoint = System.getProperty("service.address"); if (endpoint == null) { // If no address was specified, the webservice is assumed to be // runnning in the localhost at port 9080 for was endpoint = "http://localhost:9080/wsnt-was/services/NotificationConsumer10Soap"; } //Creating an URL object for the address obtained java.net.URL serviceURL = new URL(endpoint); //Creating an instance of NotificationConsumerServiceLocator NotificationConsumerService notifierSvc = new NotificationConsumerServiceLocator(); NotificationConsumer notifier = notifierSvc.getNotificationConsumer10Soap(serviceURL); //Sending the notification notifier.notify(notification); }
sendEvent() 方法會執行下列步驟: