非メッセージ駆動 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 // ... } }
(c) Copyright IBM Corporation 2005, 2006.
本製品では Eclipse テクノロジーが採用されています。(http://www.eclipse.org)