J2EE 事件使用者将实现为消息驱动的 bean,它在部署时与 JMS 目标及连接工厂相关联。要接收事件,按这些步骤进行操作:
// 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]");
在侦听器的 onMessage() 方法中,使用通知辅助控件将每个已接收到的 JMS 消息转换为包含事件通知的数组。(如果该事件与通知辅助控件中指定的事件选择器不匹配,则该数组是空的。)事件通知是实现 EventNotification 接口的类的实例。
public void onMessage(Message msg) { EventNotification[] notifications = notificationHelper.getEventNotifications(msg); // ...
通知类型 | 描述 |
---|---|
CREATE_EVENT _NOTIFICATION_TYPE |
已在与目标相关联的事件组中创建了新事件。这表示新事件已发送或者现有事件已发生变化,所以它现在与事件组定义相匹配。该通知还包含完整事件数据。 |
REMOVE_EVENT _NOTIFICATION_TYPE |
已从与目标相关联的事件组中除去存储在事件数据库中的事。这表示 已从事件数据库中删除新事件或者现有事件已发生变化,所以它不再与事件组定义相匹配。该通知还包 含已删除事件的全局实例标识。 |
UPDATE_EVENT _NOTIFICATION_TYPE |
已经按某种方式对存储在事件数据库中的事件进行了更新,这种更新方式不会改变它在与目标相关联的事件组中的成员资格。该通知还包含完整事件数据。 |
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 // ... } }