이벤트 소스 예제는 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의 기본
메소드는 기본
생성자를 사용하여 EventSourceWsn10 인스턴스를 작성합니다. 그런 다음, 기본 프로그램 로직이 포함된 개인용 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(); } }
EventSourceWsn10.java의 createEvents
메소드는 이벤트를
작성하고 이벤트를 최소의 특성 데이터로 채우는 데 사용되는 도움말
메소드입니다. sendEvent
메소드에서 이 메소드를 호출하여
Web Services로 전송될 이벤트를 작성합니다.
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; }
createEvents
메소드는 다음 단계를 수행합니다.
주:
situation와 같이, sourceComponentId도 집합 특성입니다. 그러나 하위 특성은 모두 문자열로 표시된 단순 속성으로 특수 Java 오브젝트를 개별적으로 인스턴스화하는 대신 setSourceComponentId() 도움말 메소드를 사용할 수 있습니다.예제(version, severity, msg)에서 설정된 기타 특성은 모두 문자열 또는 정수로 표시되는 단순 특성입니다.
createEvents
메소드는 특성 데이터로
현재 채워진 이벤트를 리턴합니다. 이벤트 소스 예제의 다음 메소드 createNotificationMessage(event)는 메소드에 매개변수로서 전달된 이벤트를 캡슐화하는 통지 메시지를 작성하는 데 사용되는 도움말 메소드입니다. sendEvent 메소드에서 이 메소드를 호출하여 Web Services로 전송될 통지 메시지를 작성합니다.
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 name 'Topic' and value of //dialect attribute as 'tec' 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); //Setting the Topic of the notification notificationArray[i].setTopic(topicExpType); //Setting the event to be the message of the notification notificationArray[i].setMessage(events[i]); //Setting information about the producer of the event in //the notification 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 { //The first step is creating an event CommonBaseEvent[] events = createEvents(); //Creating the Notification message encapsulating the event NotificationMessageHolderType[] notificationArray = createNotificationMessage(events); // Obtaining the address of the NotificationConsumer10Service // Web Services 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 8080 for axis endpoint = "http://localhost:8080/axis/services/NotificationConsumer10Soap"; } //Creating an URL object for the address obtained URL serviceURL = new URL(endpoint); //Creating an instance of NotificationConsumerServiceLocator NotificationConsumerService notifierSvc = new NotificationConsumerServiceLocator(); //Using the NotificationConsumerService to obtain // NotificationConsumer object for the service NotificationConsumer notifier = notifierSvc.getNotificationConsumer10Soap(serviceURL); //Registering an instance of CbeSerializer object for serializing // objects of //type CommonBaseEvent to be sent over the wire 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); //Sending the notification notifier.notify(notificationArray); }
sendEvent() 메소드는 다음 단계를 수행합니다.
관련 개념
Common Base Event 형식의
이벤트를 Enterprise Console 이벤트 형식으로 변환