Class OrderedConcurrentOutputBuffer<MessageT>
- java.lang.Object
-
- pl.morgwai.base.concurrent.OrderedConcurrentOutputBuffer<MessageT>
-
public class OrderedConcurrentOutputBuffer<MessageT> extends Object
Buffers messages until all of those that should be written before to the output are available, so that they all can be written in the correct order. Useful for processing input streams in several concurrent threads when order of response messages must reflect the order of request messages.A buffer consists of ordered buckets implementing
OrderedConcurrentOutputBuffer.OutputStream
just as the underlying output stream passed tothe constructor
. Each bucket gets flushed automatically to the output stream after all the previous buckets areclosed
. A user canadd a new bucket
at the end of the buffer,write messages
to it and finallyclose it
to indicate that no more messages will be written to it and trigger flushing of subsequent bucket(s).
Within each bucket, messages are written to the output in the order they were buffered.All bucket methods and
signalNoMoreBuckets()
are thread-safe.addBucket()
is not thread-safe and concurrent invocations must be synchronized (in case of websockets and gRPC, it is usually not a problem as endpoints and request observers are guaranteed to be called by only 1 thread at a time).Note: this class should only be used if the response messages order requirement cannot be dropped: if you control a given stream API, then it's more efficient to add some unique id to request messages, include it in response messages and send them as soon as they are produced, so nothing needs to be buffered.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
OrderedConcurrentOutputBuffer.OutputStream<MessageT>
-
Constructor Summary
Constructors Constructor Description OrderedConcurrentOutputBuffer(OrderedConcurrentOutputBuffer.OutputStream<MessageT> outputStream)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description OrderedConcurrentOutputBuffer.OutputStream<MessageT>
addBucket()
Adds a new empty bucket at the end of this buffer.void
signalNoMoreBuckets()
Indicates that no more new buckets will be added.
-
-
-
Constructor Detail
-
OrderedConcurrentOutputBuffer
public OrderedConcurrentOutputBuffer(OrderedConcurrentOutputBuffer.OutputStream<MessageT> outputStream)
-
-
Method Detail
-
addBucket
public OrderedConcurrentOutputBuffer.OutputStream<MessageT> addBucket()
Adds a new empty bucket at the end of this buffer. Not thread-safe.- Returns:
- bucket placed right after the one returned by a previous call to this method (or the first one if this is the first call). All methods of the returned bucket are thread-safe.
- Throws:
IllegalStateException
- ifsignalNoMoreBuckets()
have been already called.
-
signalNoMoreBuckets
public void signalNoMoreBuckets()
Indicates that no more new buckets will be added. If all buckets are already closed and flushed, then the underlying output stream will be closed. Thread-safe.
-
-