The event source example is a sample code for a WS-Notification 1.0 notification producer. It relies on libraries from the Apache Axis and the Eclipse TPTP open source projects.
It demonstrates the following steps that an event source must follow:
The source code for EventSourceWsn10.java is available in the
<sdk_install_dir>/samples/wsnt-axis/src/com/wtci/samples/axis/source
directory.
This example uses several Apache Axis classes, Eclipse Hyades logging classes as well as classes generated from the wsdl files. Use the following import statements to access the required packages:
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;
The main
method of EventSourceWsn10.java creates a new
instance of EventSourceWsn10 using the default constructor. It then
calls the private sendEvent() method, which contains the main program
logic. After the sendEvent() method returns, the main() method prints
the message "Notification sent" to standard output.
/** * 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(); } }
The createEvents
method of EventSourceWsn10.java is a
helper method used to create an event and populate it with a minimal set
of property data. This method is called by the sendEvent
method to create the event that will be sent to the 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; }
The createEvents
method performs the following steps:
Note:
Like situation, sourceComponentId is also a complex property. However, its child properties are all simple attributes represented by strings, so you can use the setSourceComponentId() helper method rather than separately instantiating a specialized Java object.The other properties set by the example (version, severity, and msg) are all simple properties represented by strings or integers.
createEvents
method returns the event,
which is now populated with property data.The next method of the event source example, createNotificationMessage(event), is a helper method used to create notification message encapsulating the event passed in as parameter to the method. This method is called by the sendEvent method to create the notification message that will be sent to the 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; }
The createNotificationMessage(event) performs the following steps
The last method of the event source example, sendEvent(), contains the main program logic. This method handles all interaction with the NotificationConsumer client, including sending the event.
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); }
The sendEvent() method performs the following steps:
Related topics
Building the samples
Running the samples
Related concepts
Converting an event in the Common
Base Event format to Enterprise Console event format