イベント・コンシューマーは、メッセージ駆動型 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]");
各イベント・グループは、単一の JMS トピックおよび任意の数の JMS キューに関連付けできます。 通知ヘルパーを照会すると、特定のイベント・グループに関連付けられている宛先を検出できます。
イベント・グループに関連付けられているトピックを検出するには、次のようにイベント・グループの名前を指定して、NotificationHelper の getJmsTopic(String) メソッドを使用します。MessagePort msgPort = notificationHelper.getJmsTopic("critical_events");イベント・グループに関連付けられているキューを検出するには、次のように getJmsQueues(String) メソッドを使用します。
MessagePort[] msgPorts = notificationHelper.getJmsQueues("critical_events");戻されるオブジェクトは、JMS トピックを表す単一の MessagePort オブジェクト、または JMS キューを表す MessagePort オブジェクトの配列のいずれかです。 MessagePort インスタンスは、宛先およびその接続ファクトリーの各 JNDI 名が収容されているラッパー・オブジェクトです。
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();
ご使用のリスナーの onMessage() メソッドにおいて、通知ヘルパーを使用し、各受信 JMS メッセージをイベント通知が収容された配列に変換します。 (イベントが通知ヘルパーで指定されたイベント・セレクターに一致しない場合、配列は空になります。) イベント通知は、EventNotification インターフェースを実装したクラスのインスタンスです。
public void onMessage(Message msg) { EventNotification[] notifications = notificationHelper.getEventNotifications(msg); // ...
通知タイプ | 説明 |
---|---|
CREATE_EVENT _NOTIFICATION_TYPE |
新規イベントが、宛先に関連付けられたイベント・グループに作成されています。 これは、新規イベントが送信されたか、または既存のイベントがイベント・グループ定義に一致するように変更されたかのいずれかを意味します。 その通知には、完全なイベント・データも含まれています。 |
REMOVE_EVENT _NOTIFICATION_TYPE |
イベント・データベースに保管されたイベントが、宛先に関連付けられたイベント・グループから削除されています。 これは、イベントがイベント・データベースから削除されたか、または既存のイベントがイベント・グループ定義に一致しないように変更されたかのいずれかを意味します。 その通知には、削除されたイベントのグローバル・インスタンス ID も含まれています。 |
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 // ... } }