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

java.lang.Object
  extended by com.ibm.websphere.samples.asynchbeans.wstrader.tickstream.StreamListener
All Implemented Interfaces:
com.ibm.websphere.asynchbeans.AsynchScopeEvents, com.ibm.websphere.asynchbeans.EventSourceEvents, com.ibm.websphere.asynchbeans.SubsystemMonitorEvents, StreamHeartBeatEvents, TickerUpdateEvents

public class StreamListener
extends java.lang.Object
implements com.ibm.websphere.asynchbeans.AsynchScopeEvents, com.ibm.websphere.asynchbeans.SubsystemMonitorEvents, TickerUpdateEvents, StreamHeartBeatEvents

This is created the by stream servlet. It's job is to listen for events that need to be pushed over HTTP to the applet and push them. Each applet client will have a StreamListener associated with it. When the applet disconnects, the listener will remain, but the PrintWriter (associated with the response object of the Servlet) will become invalid. The client can then reconnect, look-up this listener instance and retrieve various settings, such at the Ticker symbols that were previously configured. If the client manually logs off, then this listener should be explicitly destroyed.


Constructor Summary
StreamListener(StreamClientState cstate, java.io.PrintWriter pw, java.lang.String name)
          Create a new Servlet Listener
 
Method Summary
 void destroyed(com.ibm.websphere.asynchbeans.AsynchScope scope)
          /** This is only called when either the client logs out or the SSM associated with the clienyt detected that the client is not responsive.
 void destroyed(com.ibm.websphere.asynchbeans.SubsystemMonitor ssm)
           
 java.lang.String getName()
          This returns the name of this streamer.
 java.lang.String getSubscribedTickers()
          Returns the subscribedTickers.
 void handleHeartBeat(ClientInstruction clientInstruction)
          Handle pings from the Stream HeartBeat Alarm
 void listenerCountChanged(com.ibm.websphere.asynchbeans.EventSource es, int oldCount, int newCount)
           
 void listenerExceptionThrown(com.ibm.websphere.asynchbeans.EventSource es, java.lang.Object listener, java.lang.String methodName, java.lang.Throwable exception)
           
 void sendMessages()
          Send all ticker update messages from our buffer to the associated servlet's PrintWriter.
 void subsystemIsDead(com.ibm.websphere.asynchbeans.SubsystemMonitor ssm)
          This gets called when the SSM associated with the price generator detects the price generator has failed or been stopped.
 void subsystemIsFresh(com.ibm.websphere.asynchbeans.SubsystemMonitor ssm)
          This is called when the SSM associated with the price generator detects the price generator has done a lazerus and recovered from a failure.
 void subsystemIsStale(com.ibm.websphere.asynchbeans.SubsystemMonitor ssm)
          This is called when the SSM associated with the price generator detects that the price generator may fail.
 void tickerStopped(TickerSubscriber subscriber)
          The ticker listener has stopped.
 void tickerUpdated(TickerSubscriber subscriber, javax.jms.Message msg)
          This is called whenever a new message is received for a ticker that we're interested in.
 void unexpectedException(com.ibm.websphere.asynchbeans.EventSource es, java.lang.Object runnable, java.lang.Throwable exception)
           
 void updateTickers(java.lang.String tickers, boolean duringDestroy)
          This takes a new list of tickers to subscribe to.
 void wakeServlet()
          Wakes all all routines waking on the PrintWriter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StreamListener

public StreamListener(StreamClientState cstate,
                      java.io.PrintWriter pw,
                      java.lang.String name)
Create a new Servlet Listener

Parameters:
cstate - the client state that this stream should be registered with. A client may have more than one stream instance.
pw - the servlet's response object's PrintWriter.
name - the identity of this stream. The client can lookup this stream by name any time.
Method Detail

getName

public java.lang.String getName()
This returns the name of this streamer.


wakeServlet

public void wakeServlet()
Wakes all all routines waking on the PrintWriter. This should only be the servlet on which the streaming request was received.


destroyed

public void destroyed(com.ibm.websphere.asynchbeans.AsynchScope scope)
/** This is only called when either the client logs out or the SSM associated with the clienyt detected that the client is not responsive. When these events happen then this gets called and we clean up the state for the client and wake up the stream servlet to allow it to be reused.

Specified by:
destroyed in interface com.ibm.websphere.asynchbeans.AsynchScopeEvents
See Also:
AsynchScopeEvents.destroyed(AsynchScope)

destroyed

public void destroyed(com.ibm.websphere.asynchbeans.SubsystemMonitor ssm)
Specified by:
destroyed in interface com.ibm.websphere.asynchbeans.SubsystemMonitorEvents
See Also:
SubsystemMonitorEvents.destroyed(SubsystemMonitor)

subsystemIsDead

public void subsystemIsDead(com.ibm.websphere.asynchbeans.SubsystemMonitor ssm)
This gets called when the SSM associated with the price generator detects the price generator has failed or been stopped.

Specified by:
subsystemIsDead in interface com.ibm.websphere.asynchbeans.SubsystemMonitorEvents
See Also:
SubsystemMonitorEvents.subsystemIsDead(SubsystemMonitor)

subsystemIsFresh

public void subsystemIsFresh(com.ibm.websphere.asynchbeans.SubsystemMonitor ssm)
This is called when the SSM associated with the price generator detects the price generator has done a lazerus and recovered from a failure.

Specified by:
subsystemIsFresh in interface com.ibm.websphere.asynchbeans.SubsystemMonitorEvents
See Also:
SubsystemMonitorEvents.subsystemIsFresh(SubsystemMonitor)

subsystemIsStale

public void subsystemIsStale(com.ibm.websphere.asynchbeans.SubsystemMonitor ssm)
This is called when the SSM associated with the price generator detects that the price generator may fail. For example, it is late delivering heart beats to this server.

Specified by:
subsystemIsStale in interface com.ibm.websphere.asynchbeans.SubsystemMonitorEvents
See Also:
SubsystemMonitorEvents.subsystemIsStale(SubsystemMonitor)

listenerCountChanged

public void listenerCountChanged(com.ibm.websphere.asynchbeans.EventSource es,
                                 int oldCount,
                                 int newCount)
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)

tickerStopped

public void tickerStopped(TickerSubscriber subscriber)
Description copied from interface: TickerUpdateEvents
The ticker listener has stopped.

Specified by:
tickerStopped in interface TickerUpdateEvents
See Also:
TickerUpdateEvents.tickerStopped(TickerSubscriber)

tickerUpdated

public void tickerUpdated(TickerSubscriber subscriber,
                          javax.jms.Message msg)
This is called whenever a new message is received for a ticker that we're interested in. We verify that we want the message and then queue it in the FIFO associated with this client.

The message is then sent by the sendMessages method on another thread. We return quickly here to avoid holding up the message receiver.

Specified by:
tickerUpdated in interface TickerUpdateEvents
Parameters:
subscriber - the information about the ticker.
msg - the received message
See Also:
TickerUpdateEvents.tickerUpdated(TickerSubscriber, Message)

handleHeartBeat

public void handleHeartBeat(ClientInstruction clientInstruction)
Handle pings from the Stream HeartBeat Alarm

Specified by:
handleHeartBeat in interface StreamHeartBeatEvents
Parameters:
clientInstruction - the instruction to send to the client listener
See Also:
StreamHeartBeatEvents.handleHeartBeat(ClientInstruction)

sendMessages

public void sendMessages()
Send all ticker update messages from our buffer to the associated servlet's PrintWriter. Continue to process messages as they become available until getSendingEvents() becomes false or there is an error writing to the PrintWriter.


updateTickers

public void updateTickers(java.lang.String tickers,
                          boolean duringDestroy)
                   throws javax.naming.NamingException,
                          com.ibm.websphere.asynchbeans.WorkException,
                          javax.jms.JMSException
This takes a new list of tickers to subscribe to. We remove any listeners from before which are not specified now and add new listeners for tickers we were not subscribed to before.

Parameters:
tickers - The new list of tickers for this client, can be empty meaning none.
duringDestroy - If true then the client has logged out or timed out and the AsynchScope for the client is being destroyed.
Throws:
javax.naming.NamingException
com.ibm.websphere.asynchbeans.WorkException
javax.jms.JMSException

getSubscribedTickers

public java.lang.String getSubscribedTickers()
Returns the subscribedTickers.

Returns:
String