WebSphere WebSphere Enterprise Service Bus, Version 6.0.1 Operating Systems: AIX, HP-UX, Linux, Solaris, Windows

Developing a non-MDB event consumer

An event consumer can also be created using a non-message driven bean.

Why and when to perform this task

To write an event consumer that is not a message-driven bean, follow these steps:

Steps for this task

  1. Obtain a notification helper. A JMS event consumer uses a notification helper to identify JMS destinations associated with an event group, to convert received JMS messages into event notifications, and to perform filtering of received events. To obtain a notification helper, use a notification helper factory, which is an instance of NotificationHelperFactory that has been bound into a JNDI namespace. The following code fragment uses a notification helper factory to obtain a notification helper.
    // 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();
                                          
  2. Optional: Specify the event selector. If you want to filter received events, you can use the setEventSelector() method to set an event selector on the notification helper. Your event consumer can then use the notification helper to check received events against the event selector. The following code fragment sets an event selector specifying events with severity greater than 30 (warning).
    notificationHelper.setEventSelector("CommonBaseEvent[@severity > 30]");
  3. Use the notification helper to find the JMS destination to subscribe to.

    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.
  4. Connect to the destination. Use the getter methods of MessagePort to retrieve the JNDI names of the destination and connection factory. You can then use the standard JMS interfaces to connect to the destination. The following code fragment subscribes to a JMS topic:
    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();
  5. Convert received messages into event notifications.

    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);
      // ...
  6. Check the notification type and retrieve the event data as appropriate. Each event notification has a field representing the notification type (an integer whose value is one of the notification type constants defined by the NotificationHelper interface). Three notification types are currently supported:
    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.
    Use the getNotificationType() method of EventNotification to check the notification type of each received notification. Based on the notification type, you can determine whether your event consumer should process the notification further, and what kind of event data the notification contains:
    • If the notification type is CREATE_EVENT_NOTIFICATION_TYPE or UPDATE_EVENT_NOTIFICATION_TYPE, your consumer can use EventNotification.getEvent() to attempt to retrieve the new or updated event. This method is valid only for notifications of new or updated events.
    • If the notification is REMOVE_EVENT_NOTIFICATION_TYPE, your consumer can use EventNotification.getGlobalInstanceId() to retrieve the global instance identifier of the deleted event. This method is valid only for notifications of deleted events.
    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
        // ...
      }
    }

Task topic

Terms of Use | Rate this page

Timestamp iconLast updated: 13 Dec 2005
http://publib.boulder.ibm.com/infocenter/dmndhelp/v6rxmx/index.jsp?topic=/com.ibm.websphere.wesb.doc\doc\tcei_admin_nonMDBEventConsumer.html

(C) Copyright IBM Corporation 2005. All Rights Reserved.
This information center is powered by Eclipse technology. (http://www.eclipse.org)