Interface StreamMessageDuplicator<T>

Type Parameters:
T - the type of elements
All Superinterfaces:
AutoCloseable, SafeCloseable
All Known Subinterfaces:
HttpRequestDuplicator, HttpResponseDuplicator
All Known Implementing Classes:
DefaultStreamMessageDuplicator

public interface StreamMessageDuplicator<T> extends SafeCloseable
A duplicator that duplicates a StreamMessage into one or more StreamMessages, which publish the same elements.

Only one subscriber can subscribe to a StreamMessage. If you want to subscribe to it multiple times, use StreamMessageDuplicator which is returned by calling StreamMessage.toDuplicator().


 StreamMessage<String> streamMessage = ...
 try (StreamMessageDuplicator<String> duplicator = streamMessage.toDuplicator()) {
     // streamMessage.subscribe(...) will throw an exception. You cannot subscribe to streamMessage anymore.

     // Duplicate the stream as many as you want to subscribe.
     StreamMessage<String> duplicatedStreamMessage1 = duplicator.duplicate();
     StreamMessage<String> duplicatedStreamMessage2 = duplicator.duplicate();

     duplicatedStreamMessage1.subscribe(...);
     duplicatedStreamMessage2.subscribe(...);
 }
 

Use the try-with-resources block or call close() manually to clean up the resources after all subscriptions are done. If you want to stop publishing and clean up the resources immediately, call abort(). If you do none of these, memory leak might happen.

If you subscribe to the duplicated stream message with the SubscriptionOption.WITH_POOLED_OBJECTS, the published elements can be shared across Subscribers. So do not manipulate the data unless you copy them.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this duplicator and aborts all stream messages returned by duplicate().
    void
    Closes this duplicator and aborts all stream messages returned by duplicate() with the specified Throwable.
    void
    Closes this duplicator and prevents it from further duplication.
    Returns a new StreamMessage that publishes the same elements as the StreamMessage that this duplicator is created from.