Class AsyncChangeStream<DocumentT,ChangeEventT extends BaseChangeEvent<DocumentT>>
- java.lang.Object
-
- com.mongodb.stitch.android.services.mongodb.remote.AsyncChangeStream<DocumentT,ChangeEventT>
-
- Type Parameters:
DocumentT
- The type of the full document on the underlying change event to be returned asynchronously.ChangeEventT
- The type of MongoDB change event that this stream internally returns.
public class AsyncChangeStream<DocumentT,ChangeEventT extends BaseChangeEvent<DocumentT>> extends Object
An implementation ofChangeStream
that returns each event as aTask
.
-
-
Constructor Summary
Constructors Constructor Description AsyncChangeStream(ChangeStream<ChangeEventT> stream, TaskDispatcher dispatcher)
Initializes a passthrough change stream with the provided underlying event stream.
-
Method Summary
Modifier and Type Method Description void
addChangeEventListener(BaseChangeEventListener<DocumentT,ChangeEventT> listener)
Adds a ChangeEventListener to the ChangeStream that will run on every event on the stream.void
close()
Closes the underlying stream and removes all ChangeEventListeners.boolean
isOpen()
Indicates whether or not the change stream is currently open.Task<ChangeEventT>
nextEvent()
Returns aTask
whose resolution gives the next event from the underlying stream.void
removeChangeEventListener(BaseChangeEventListener<DocumentT,ChangeEventT> listener)
Remove a ChangeEventListener from the ChangeStream.void
setExceptionListener(ExceptionListener exceptionListener)
Optionally adds a listener that is notified when an attempt to retrieve the next event.
-
-
-
Constructor Detail
-
AsyncChangeStream
public AsyncChangeStream(ChangeStream<ChangeEventT> stream, TaskDispatcher dispatcher)
Initializes a passthrough change stream with the provided underlying event stream.- Parameters:
stream
- The event stream.dispatcher
- The event dispatcher.
-
-
Method Detail
-
addChangeEventListener
public void addChangeEventListener(BaseChangeEventListener<DocumentT,ChangeEventT> listener)
Adds a ChangeEventListener to the ChangeStream that will run on every event on the stream. Multiple ChangeEventListeners can be added to any given stream and they will be removed when the stream is closed or when the listener is removed. Calls to nextEvent() will fail while there exists any ChangeEventListener's on this stream. Additionally, any events that occur after the ChangeEventListener is opened will not be caught by the listener.- Parameters:
listener
- theBaseChangeEventListener
-
removeChangeEventListener
public void removeChangeEventListener(BaseChangeEventListener<DocumentT,ChangeEventT> listener)
Remove a ChangeEventListener from the ChangeStream.- Parameters:
listener
- theBaseChangeEventListener
-
close
public void close() throws IOException
Closes the underlying stream and removes all ChangeEventListeners.- Throws:
IOException
- If the underlying stream throws anIOException
when it is closed.
-
setExceptionListener
public void setExceptionListener(ExceptionListener exceptionListener)
Optionally adds a listener that is notified when an attempt to retrieve the next event. fails.- Parameters:
exceptionListener
- TheExceptionListener
.
-
isOpen
public boolean isOpen()
Indicates whether or not the change stream is currently open.- Returns:
- True if the underlying change stream is open.
-
nextEvent
public Task<ChangeEventT> nextEvent() throws IOException, IllegalStateException
Returns aTask
whose resolution gives the next event from the underlying stream.- Returns:
- task providing the next event
- Throws:
IOException
- if the underlying stream throws anIOException
IllegalStateException
- if any ChangeEventListeners are running
-
-