An event consumer can also be created using a non-message driven bean.
// Get notification helper factory from JNDI InitialContext context = new InitialContext(); Object notificationHelperFactoryObject = context.lookup("com/ibm/events/NotificationHelperFactory"); NotificationHelperFactory nhFactory = (NotificationHelperFactory) PortableRemoteObject.narrow(notificationHelperFactoryObject, NotificationHelperFactory.class); // Create notification helper NotificationHelper notificationHelper = nhFactory.getNotificationHelper();
notificationHelper.setEventSelector("CommonBaseEvent[@severity > 30]");
Each event group can be associated with a single JMS topic and any number of JMS queues. You can query the notification helper to find out what destinations are associated with a particular event group.
To find the topic associated with an event group, use the getJmsTopic(String) method of NotificationHelper, specifying the name of the event group:MessagePort msgPort = notificationHelper.getJmsTopic("critical_events");To find the queues associated with an event group, use the getJmsQueues(String) method:
MessagePort[] msgPorts = notificationHelper.getJmsQueues("critical_events");The returned object is either a single MessagePort object representing a JMS topic or an array of MessagePort objects representing JMS queues. A MessagePort instance is a wrapper object containing the JNDI names of the destination and its connection factory.
String connectionFactoryName = msgPort.getConnectionFactoryJndiName(); String destinationName = msgPort.getDestinationJndiName(); // create connection and session ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryName); Connection connection = connectionFactory.createConnection(); Session session = connection.createSesion(false, Session.CLIENT_ACKNOWLEDGE); // Create consumer and register listener Topic topic = (Topic) context.lookup(destinationName); MessageConsumer consumer = session.createConsumer(topic); consumer.setMessageListener(this); connection.start();
In the onMessage() method of your listener, use the notification helper to convert each received JMS message into an array containing an event notification. (If the event does not match the event selector specified on the Notification Helper, the array is empty.) An event notification is an instance of a class implementing the EventNotification interface.
public void onMessage(Message msg) { EventNotification[] notifications = notificationHelper.getEventNotifications(msg); // ...
Notification type | Description |
---|---|
CREATE_EVENT _NOTIFICATION_TYPE |
A new event has been created in the event group associated with the destination. This means either that a new event has been sent, or that an existing event has changed so that it now matches the event group definition. The notification also contains the complete event data. |
REMOVE_EVENT _NOTIFICATION_TYPE |
An event stored in the event database has been removed from the event group associated with the destination. This means either that an event has been deleted from the event database, or that an existing event has changed so that it no longer matches the event group definition. The notification also contains the global instance identifier of the deleted event. |
UPDATE_EVENT _NOTIFICATION_TYPE |
An event stored in the event database has been updated in a way that does not change its membership in the event group associated with the destination. The notification also contains the complete event data. |
for (int i = 0; i < notifications.length; i++) { int notifType = notifications[i].getNotificationType(); if(notifType == NotificationHelper.CREATE_EVENT_NOTIFICATION_TYPE) { CommonBaseEvent event = notifications[i].getEvent(); if (event != null) { // process the new event // ... } } else if(notifType == NotificationHelper.UPDATE_EVENT_NOTIFICATION_TYPE) { CommonBaseEvent event = notifications[i].getEvent(); if (event != null) { // process the updated event // ... } } else if(notifType == NotificationHelper.REMOVE_EVENT_NOTIFICATION_TYPE) { String eventId = notifications.[i].getGlobalInstanceId(); // process the event deletion // ... } }