Class AbstractBufferedStream<T,R>
- java.lang.Object
-
- io.axoniq.axonserver.connector.impl.FlowControlledStream<T,R>
-
- io.axoniq.axonserver.connector.impl.FlowControlledBuffer<T,R>
-
- io.axoniq.axonserver.connector.impl.AbstractBufferedStream<T,R>
-
- Type Parameters:
T- the type of results this stream implementation buffersR- the type of message used for flow control on this stream
- All Implemented Interfaces:
ResultStream<T>,io.grpc.stub.ClientResponseObserver<R,T>,io.grpc.stub.StreamObserver<T>,AutoCloseable
- Direct Known Subclasses:
BufferedEventStream,BufferedPersistentStreamSegment,SubscriptionQueryUpdateBuffer
public abstract class AbstractBufferedStream<T,R> extends FlowControlledBuffer<T,R> implements ResultStream<T>
An abstractFlowControlledBufferandResultStreamimplementation, used to provide a buffered stream of results ofTwhen receiving a request of typeR.
-
-
Constructor Summary
Constructors Constructor Description AbstractBufferedStream(String clientId, int bufferSize, int refillBatch)Constructs anAbstractBufferedStream
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Requests the current stream to be closed.Optional<Throwable>getError()Returns an optional containing the exception reported by the Server, if any.booleanisClosed()Check whether this buffer has been closed offTnext()Consumes the next available element in the stream, waiting for an element to become available, or for the buffer to be closed.TnextIfAvailable()Consumes the next available element in the stream, if available.TnextIfAvailable(long timeout, TimeUnit unit)Consumes the next available element in the stream, waiting for at mosttimeout(in givenunit) for an element to become available.voidonAvailable(Runnable callback)Sets the givencallbackto execute when data is available for reading, or the stream has been closed.voidonCloseRequested(Runnable handler)Adds the givenhandlerto a collection ofRunnables to be invoked when the stream is closed by the server.voidonCompleted()voidonError(Throwable t)voidonNext(T value)Tpeek()Retrieves, but does not remove, the first entry of this buffer, or returnsnullif the buffer is empty.-
Methods inherited from class io.axoniq.axonserver.connector.impl.FlowControlledBuffer
beforeStart, getErrorResult, take, terminalMessage, tryTake, tryTake, tryTake, tryTakeNow
-
Methods inherited from class io.axoniq.axonserver.connector.impl.FlowControlledStream
buildFlowControlMessage, buildInitialFlowControlMessage, enableFlowControl, markConsumed, outboundStream
-
-
-
-
Constructor Detail
-
AbstractBufferedStream
public AbstractBufferedStream(String clientId, int bufferSize, int refillBatch)
Constructs anAbstractBufferedStream- Parameters:
clientId- the identifier of the client initiation this streambufferSize- the size of this bufferrefillBatch- the number of entries to be consumed prior to refilling this buffer
-
-
Method Detail
-
next
public T next() throws InterruptedException
Description copied from interface:ResultStreamConsumes the next available element in the stream, waiting for an element to become available, or for the buffer to be closed.- Specified by:
nextin interfaceResultStream<T>- Returns:
- the next available element in the stream, or
nullif none available - Throws:
InterruptedException- when the Thread is interrupted while waiting for an element to become available- See Also:
ResultStream.isClosed()
-
nextIfAvailable
public T nextIfAvailable()
Description copied from interface:ResultStreamConsumes the next available element in the stream, if available. Will returnnullif no element is available immediately.- Specified by:
nextIfAvailablein interfaceResultStream<T>- Returns:
- the next available element in the stream, or
nullif none is available, or if the stream is closed
-
nextIfAvailable
public T nextIfAvailable(long timeout, TimeUnit unit) throws InterruptedException
Description copied from interface:ResultStreamConsumes the next available element in the stream, waiting for at mosttimeout(in givenunit) for an element to become available. If no element is available with the given timeout, or if the stream has been closed, it returnsnull.- Specified by:
nextIfAvailablein interfaceResultStream<T>- Parameters:
timeout- the amount of time to wait for an element to become availableunit- the unit of time in which the timeout is expressed- Returns:
- the next available element in the stream, or
nullif none is available - Throws:
InterruptedException- when the Thread is interrupted while waiting for an element to become available- See Also:
ResultStream.isClosed()
-
onNext
public void onNext(T value)
- Specified by:
onNextin interfaceio.grpc.stub.StreamObserver<T>- Overrides:
onNextin classFlowControlledBuffer<T,R>
-
onError
public void onError(Throwable t)
- Specified by:
onErrorin interfaceio.grpc.stub.StreamObserver<T>- Overrides:
onErrorin classFlowControlledBuffer<T,R>
-
getError
public Optional<Throwable> getError()
Description copied from interface:ResultStreamReturns an optional containing the exception reported by the Server, if any.Note that this method may return a non-empty response, even when
ResultStream.isClosed()returnsfalse, or if there are messages available for processing.- Specified by:
getErrorin interfaceResultStream<T>- Returns:
- an optional containing the exception reported by the Server, if any.
-
onCompleted
public void onCompleted()
- Specified by:
onCompletedin interfaceio.grpc.stub.StreamObserver<T>- Overrides:
onCompletedin classFlowControlledBuffer<T,R>
-
peek
public T peek()
Description copied from class:FlowControlledBufferRetrieves, but does not remove, the first entry of this buffer, or returnsnullif the buffer is empty.- Specified by:
peekin interfaceResultStream<T>- Overrides:
peekin classFlowControlledBuffer<T,R>- Returns:
- the first entry of this buffer without removing it, or
nullif it is empty
-
onAvailable
public void onAvailable(Runnable callback)
Description copied from interface:ResultStreamSets the givencallbackto execute when data is available for reading, or the stream has been closed. Note that any registration will replace the previous one.The callback is invoked in the publisher's thread. Invocations should be as short as possible, preferably delegating to a reader thead, instead of accessing the entries directly.
- Specified by:
onAvailablein interfaceResultStream<T>- Parameters:
callback- a {Runnable} toRunnable.run()when the next entry becomes available ornullto disable callbacks
-
isClosed
public boolean isClosed()
Description copied from class:FlowControlledBufferCheck whether this buffer has been closed off- Specified by:
isClosedin interfaceResultStream<T>- Overrides:
isClosedin classFlowControlledBuffer<T,R>- Returns:
trueif this buffer is closed,false otherwise- See Also:
ResultStream.getError()
-
close
public void close()
Description copied from interface:ResultStreamRequests the current stream to be closed. It is up to the publishing end of the stream to honor the request.Any elements received before closing are still available for reading.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceResultStream<T>- Overrides:
closein classFlowControlledBuffer<T,R>
-
onCloseRequested
public void onCloseRequested(Runnable handler)
Adds the givenhandlerto a collection ofRunnables to be invoked when the stream is closed by the server. Note that data may still be buffered locally for processing.- Parameters:
handler-Runnableto invoke when the stream is closed by the server
-
-