+

Search Tips   |   Advanced Search

 

Develop event listeners

 

Application components that listen for events can use the EventSource.addListener() method to register an event listener object (a type of asynchronous bean) with the event source to which the events will be published. An event source also can fire events in a type-safe manner using any interface.

 

Overview

Notifications between components within a single EAR file are handled by a special event source. See the topic, Using the application notification service.

 

Procedure

  1. Create an event listener object, which can be any type. For example, see the following interface code:

    interface SampleEventGroup
    {
      void finished(String message);
    }
    
    class myListener implements SampleEventGroup
    {
      public void finished(String message)
      {
      // This will be called when we 'finish'.
      }
    }
    
    

  2. Register the event listener object with the event source. For example, see the following code:

    InitialContext ic = ...;
    EventSource es = (EventSource)ic.lookup("java:comp/websphere/ApplicationNotificationService");
    myListener l = new myListener();
    es.addListener(l);
    
    
    This enables the myListener.finished() method to be called whenever the event is fired. The following code example shows how this event might be fired:

    InitialContext ic = ...;
    EventSource es = (EventSource)ic.lookup("java:comp/websphere/ApplicationNotificationService");
    myListener proxy = es.getEventTrigger(myListener.class);
    // fire the 'event' by calling the method 
    // representing the event on the proxy
    proxy.finished("done");
    
    



Use the application notification service

Example: Firing a listenerCountChanged event

 

Related tasks


Use asynchronous beans