Class 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 to the constructor. Each bucket gets flushed automatically to the output stream after all the previous buckets are closed. A user can add a new bucket at the end of the buffer, write messages to it and finally close 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.

    • Method Detail

      • 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.