Examples of use
This section contains sections of code that illustrate the EventService API. The complete sample applications are located in the Apama installation’s samples\engine_client\javaEventService directory. The directory contains a README.txt file that describes how to build and run the sample applications including how to inject the necessary monitors.
The following piece of code creates an EventService object and then sends a simple event to the correlator (the correlator would need a corresponding monitor to listen for the SimpleEvent event type):
IEventService eventService = EventServiceFactory.createEventService();
public EventServiceSample() {
try {
// create the simpleEvent object
Event simpleEvent = new Event("SimpleEvent(\"hi there\")");
// send it
eventService.sendEvent(simpleEvent);
System.out.println("SimpleEvent(hi there) sent...");
System.out.print("\t Please check Correlator console for the string ");
System.out.print("\t *** SimpleEvent(\"hi there\") received ***");
} catch (EngineException ee) {
ee.printStackTrace(System.err);
}
}
The next code sample illustrates how to create a named channel. Then it adds a listener to the channel. To illustrate how the listener works, the code sample sends an event to the correlator, which (assuming the corresponding monitor has been injected) sends a notification that the event was received. Finally, the sample shows the listener’s callback method that is invoked when the notification is received.
public ESChannelSample() {
// Add the channel to those monitored by the event service
IEventServiceChannel ourChannel =
eventService.addChannel("eventService.sample.channel", null);
// Create an EventType to describe the event we are expecting to receive
EventType channelEventType = new EventType(
"ChannelledEvent", new Field[] {new Field("s", StringFieldType.TYPE)});
// Add a listener that will be notified whenever an event of the
// specified type is received on the channel
ourChannel.addEventListener(new MyEventListener(), channelEventType);
// Send an event to the correlator
try {
Event cEvent = new Event("ChannelledEvent(\"hi there\")");
eventService.sendEvent(cEvent);
} catch (EngineException ee) {
ee.printStackTrace(System.err);
}
}
// Inner class to implement our callback method that is notified when
// events are received.
private class MyEventListener extends EventListenerAdapter {
public void handleEvent(Event event) {
System.out.println("MyEventListener.handleEvent() called, event = " +
event);
}
}
The following code sample defines a request event type and a response event type and registers the request event type with the channel. An IResponseWrapper object is used to issue the request. Note that the requestResponse() method is a blocking call.
// Define the RequestEvent and ResponseEvent event type
// NOTE: in order to use the requestResponse() method, the Request
// and Response event must have a member "messageId"
// (IEventServiceChannel.DEFAULT_MESSAGEID_FIELD_NAME) of
// MonitorScript type integer, Java type Long.
EventType requestEventType = new EventType("RequestEvent",
new Field[] {
new Field(IEventServiceChannel.DEFAULT_MESSAGEID_FIELD_NAME,
IntegerFieldType.TYPE), // field required for using
// requestResponse() call
new Field("msg", StringFieldType.TYPE)});
EventType responseEventType = new EventType("ResponseEvent",
new Field[] {
new Field(IEventServiceChannel.DEFAULT_MESSAGEID_FIELD_NAME,
IntegerFieldType.TYPE), // field required for using
// requestResponse() call
new Field("responseMsg", StringFieldType.TYPE)});
try {
// Register the RequestEventType to the channel
ourChannel.registerEventType(requestEventType);
// Create the requestEvent
Event requestEvent = new Event("RequestEvent(0, \"Hi There\")");
// Send two events in a for loop. User can experiment
// what will happen if responseWrapper.releaseLock() is NOT called
// by commenting out the line :
// responseWrapper.releaseLock()
// inside the finally block.
for (int i = 0; i < 2; i++) {
IResponseWrapper responseWrapper = null;
try {
// Now, make the requestReponse.
// NOTE: requestResponse() is a blocking call and won't return until
// a response is received (or other failure condition ocurred)
System.out.println("Sending RequestEvent() ...");
responseWrapper =
ourChannel.requestResponse(requestEvent, responseEventType);
// print the response event
System.out.println("Response event received: " +
responseWrapper.getEvent());
} finally {
if (responseWrapper != null) {
// NOTE: we must release the lock after a response is received
responseWrapper.releaseLock();
}
}
}
} catch (Exception ee) {
ee.printStackTrace(System.err);
}
}
The next piece of sample code registers a ResponseListener with the channel. The code defines an asyncRequest event type and an asyncResponse event type and a callback method that is used to handle the response. Note, the asynchRequestResponse() method is non-blocking.
// Define the AsyncRequestEvent and AsyncResponseEvent event type
// NOTE: in order to use the asyncRequestResponse() method, the Request
// and Response event must have a member "messageId"
// (IEventServiceChannel.DEFAULT_MESSAGEID_FIELD_NAME) of
// MonitorScript type integer, Java type Long
EventType asyncRequestEventType = new EventType("AsyncRequestEvent",
new Field[] {
new Field(IEventServiceChannel.DEFAULT_MESSAGEID_FIELD_NAME,
IntegerFieldType.TYPE), // field required for using
// asyncRequestResponse() call
new Field("msg", StringFieldType.TYPE)});
EventType asyncResponseEventType = new EventType("AsyncResponseEvent",
new Field[] {
new Field(IEventServiceChannel.DEFAULT_MESSAGEID_FIELD_NAME,
IntegerFieldType.TYPE), // field required for using
// asyncRequestResponse() call
new Field("responseMsg", StringFieldType.TYPE)});
try {
// Register the RequestEventType to the channel
ourChannel.registerEventType(asyncRequestEventType);
// Create the requestEvent
Event requestEvent = new Event("AsyncRequestEvent(0, \"Hi There\")");
// Now, make the asyncRequestReponse. MyReponseListener's
// handleResponse() is the callback method when a response is available
ourChannel.asyncRequestResponse(new MyResponseListener(),
requestEvent,
asyncResponseEventType);
System.out.println("AsyncRequestEvent() sent...");
} catch (Exception ee) {
ee.printStackTrace(System.err);
}
}
/** Inner class to implement our callback method that is notified when events
are recieved. */
private class MyResponseListener implements IResponseListener {
/** Callback method that is called when a matching response Event is
* received for an asynchronous request-response call made on an
* EventServiceChannel.
*
* @param requestEvent the original request Event.
* @param responseEvent the corresponding response Event.
*/
public void handleResponse(Event requestEvent, Event responseEvent) {
System.out.println("MyResponseListener.handleResponse() called - ");
System.out.println("\t requestEvent = " + requestEvent);
System.out.println("\t responseEvent = " + responseEvent);
}
/** Callback method that is called when an exception is thrown within an
* asynchronous request-response call made on an EventServiceChannel.
*
* @param exception The exception that was thrown in the asynchronous call.
*/
public void handleException(Exception exception) {
exception.printStackTrace(System.err);
}
}