com.ibm.websphere.samples.asynchbeans.wstrader.tickstream
Class TickerSubscriber

java.lang.Object
  extended by com.ibm.websphere.samples.asynchbeans.wstrader.tickstream.TickerSubscriber
All Implemented Interfaces:
com.ibm.websphere.asynchbeans.EventSourceEvents, com.ibm.websphere.asynchbeans.Work, java.lang.Runnable

public class TickerSubscriber
extends java.lang.Object
implements com.ibm.websphere.asynchbeans.Work, com.ibm.websphere.asynchbeans.EventSourceEvents

This is a Work that is used to dynamically subscribe to a Topic and notify listeners that are colocated in this JVM whenever a message is received on that topic.

It is created whenever a client that is attached needs to subscribe to a particular stock ticker. A Map of these TickerSubscribers is kept in StreamApplicationState. If a Tickersubscriber for the required ticker doesn't exist then we create a new one for that ticker and add it to the Map. The client then adds a listener to the EventSource associated with the TickerSubscriber object. The tickerUpdated method on the client listener will be called whenever a message arrives on that topic.

The TickerSubscriber should only exist as long as their are interested clients attached to its EventSource. Once the number of listeners falls to 1 then the TickerSubscriber will terminate and release the JMS and thread resources.


Constructor Summary
TickerSubscriber(StreamApplicationState appState, com.ibm.websphere.asynchbeans.WorkManager wm, javax.jms.TopicConnectionFactory tcf, javax.jms.Topic topic, java.lang.String selector)
          This creates a new TickerSubscriber.
 
Method Summary
 com.ibm.websphere.asynchbeans.EventSource getEventSource()
          This returns the EventSource associated with this listener.
 java.lang.String getTicker()
           
 void listenerCountChanged(com.ibm.websphere.asynchbeans.EventSource es, int oldCount, int newCount)
          If the listener counter falls to one listener, then that means we're the only listener and therefore we will stop the Work by calling release and this will stop us listening needlessly when no one is listening to the received messages.
 void listenerExceptionThrown(com.ibm.websphere.asynchbeans.EventSource es, java.lang.Object listener, java.lang.String methodName, java.lang.Throwable exception)
           
 void release()
          This when called will cause the run method to exit.
 void run()
          This is the actual 'thread' which listens for prices.
 void unexpectedException(com.ibm.websphere.asynchbeans.EventSource es, java.lang.Object runnable, java.lang.Throwable exception)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TickerSubscriber

public TickerSubscriber(StreamApplicationState appState,
                        com.ibm.websphere.asynchbeans.WorkManager wm,
                        javax.jms.TopicConnectionFactory tcf,
                        javax.jms.Topic topic,
                        java.lang.String selector)
                 throws javax.jms.JMSException,
                        com.ibm.websphere.asynchbeans.WorkException,
                        javax.naming.NamingException
This creates a new TickerSubscriber. It receives the owner of this object, appState and the ticker that needs to be subscribed to. It creates an EventSource for this ticker and adds itself as a listener so that it can detect when the listener count falls to 1, that it, itself. It then starts a demon Work to subscribe to the topic and when a message is received then it will fire an event to any listeners on EventSource that implement TickerUpdateEvents.

Parameters:
wm - the main WorkManager for the application.
selector - the ticker that this TickerSubscriber is subscribed to.
Throws:
javax.jms.JMSException
com.ibm.websphere.asynchbeans.WorkException
javax.naming.NamingException
Method Detail

getTicker

public java.lang.String getTicker()

release

public void release()
This when called will cause the run method to exit. This is normally only called when the JMS listener is no longer required.

Specified by:
release in interface com.ibm.websphere.asynchbeans.Work

run

public void run()
This is the actual 'thread' which listens for prices. It allows the streaming servlet for an applet to register a listener that implements TickerUpdateEvents. We fire an event for each message received. All subscribers on the EventSource get this event. In this way, we share a single JMS listener for all subscribers within this JVM.

Specified by:
run in interface java.lang.Runnable

getEventSource

public com.ibm.websphere.asynchbeans.EventSource getEventSource()
This returns the EventSource associated with this listener. We fire an event when we receive a message. Normally, the only listeners are ones which store the message in a modified FIFO queue where another thread picks it up for processing at a later point.


listenerCountChanged

public void listenerCountChanged(com.ibm.websphere.asynchbeans.EventSource es,
                                 int oldCount,
                                 int newCount)
If the listener counter falls to one listener, then that means we're the only listener and therefore we will stop the Work by calling release and this will stop us listening needlessly when no one is listening to the received messages.

Specified by:
listenerCountChanged in interface com.ibm.websphere.asynchbeans.EventSourceEvents
See Also:
EventSourceEvents.listenerCountChanged(EventSource, int, int)

listenerExceptionThrown

public void listenerExceptionThrown(com.ibm.websphere.asynchbeans.EventSource es,
                                    java.lang.Object listener,
                                    java.lang.String methodName,
                                    java.lang.Throwable exception)
Specified by:
listenerExceptionThrown in interface com.ibm.websphere.asynchbeans.EventSourceEvents
See Also:
EventSourceEvents.listenerExceptionThrown(EventSource, Object, String, Throwable)

unexpectedException

public void unexpectedException(com.ibm.websphere.asynchbeans.EventSource es,
                                java.lang.Object runnable,
                                java.lang.Throwable exception)
Specified by:
unexpectedException in interface com.ibm.websphere.asynchbeans.EventSourceEvents
See Also:
EventSourceEvents.unexpectedException(EventSource, Object, Throwable)