com.crankuptheamps.client
Class MessageStream

java.lang.Object
  extended by com.crankuptheamps.client.MessageStream
All Implemented Interfaces:
ConnectionStateListener, MessageHandler, Closeable, Iterable<Message>, Iterator<Message>

public class MessageStream
extends Object
implements Iterator<Message>, Iterable<Message>, MessageHandler, ConnectionStateListener, Closeable

MessageStream provides an iteration abstraction over the results of an AMPS command such as a subscribe, a SOW query, or SOW delete. MessageStream is produced when calling Client.execute() and continues iterating over the results until the connection is closed, or the iterator is explicitly closed, or when the SOW query is ended. You can use a MessageStream as you would other Iterators, for example, using a for loop (Java 1.7):


 MessageStream stream = client.execute(new Command("sow").setTopic("/orders"));
 for(Message message : stream)
 {
   ...
 }
 


Field Summary
 
Fields inherited from interface com.crankuptheamps.client.ConnectionStateListener
Connected, Disconnected
 
Constructor Summary
protected MessageStream(Client client_)
           
 
Method Summary
 void close()
          Closes this MessageStream, unsubscribing from AMPS if applicable.
 void connectionStateChanged(int newState_)
          Called on an implementation of this interface when the connection state changes.
 boolean hasNext()
           
 void invoke(Message message)
          Method for the AMPS client to call when a message is received.
 boolean isConnected()
           
 Iterator<Message> iterator()
           
 Message next()
           
 void remove()
           
protected  void setRunning()
           
protected  void setSOWOnly()
           
protected  void setStatsOnly()
           
protected  void setSubscription(CommandId commandId_)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageStream

protected MessageStream(Client client_)
Method Detail

setSubscription

protected void setSubscription(CommandId commandId_)

setSOWOnly

protected void setSOWOnly()

setStatsOnly

protected void setStatsOnly()

setRunning

protected void setRunning()

connectionStateChanged

public void connectionStateChanged(int newState_)
Description copied from interface: ConnectionStateListener
Called on an implementation of this interface when the connection state changes.

Specified by:
connectionStateChanged in interface ConnectionStateListener

hasNext

public boolean hasNext()
Specified by:
hasNext in interface Iterator<Message>

next

public Message next()
Specified by:
next in interface Iterator<Message>

invoke

public void invoke(Message message)
Description copied from interface: MessageHandler
Method for the AMPS client to call when a message is received.

The AMPS client reuses the same message object in successive calls to the message handler, resetting the contents of the object for each call. Should you need to use the data in the message outside of the call to the message handler, you must copy the message object or copy data out of the message object.

Specified by:
invoke in interface MessageHandler

remove

public void remove()
Specified by:
remove in interface Iterator<Message>

iterator

public Iterator<Message> iterator()
Specified by:
iterator in interface Iterable<Message>

isConnected

public boolean isConnected()
Returns:
true if the connection to AMPS is still active, or false if a disconnect is detected.

close

public void close()
Closes this MessageStream, unsubscribing from AMPS if applicable.

Specified by:
close in interface Closeable