Interface StreamMessageDuplicator<T>
- Type Parameters:
T
- the type of elements
- All Superinterfaces:
AutoCloseable
,SafeCloseable
- All Known Subinterfaces:
HttpRequestDuplicator
,HttpResponseDuplicator
- All Known Implementing Classes:
DefaultStreamMessageDuplicator
StreamMessage
into one or more StreamMessage
s,
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
Subscriber
s. So do not manipulate the data unless you copy them.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
abort()
Closes this duplicator and aborts all stream messages returned byduplicate()
.void
Closes this duplicator and aborts all stream messages returned byduplicate()
with the specifiedThrowable
.void
close()
Closes this duplicator and prevents it from further duplication.Returns a newStreamMessage
that publishes the same elements as theStreamMessage
that this duplicator is created from.
-
Method Details
-
duplicate
Returns a newStreamMessage
that publishes the same elements as theStreamMessage
that this duplicator is created from. -
close
void close()Closes this duplicator and prevents it from further duplication.duplicate()
will raise anIllegalStateException
after this method is invoked.Note that the previously duplicated streams will not be closed but will continue publishing data until the original
StreamMessage
is closed. All the data published from the originalStreamMessage
are cleaned up when all duplicated streams are complete. If you want to stop publishing and clean up the resources immediately, callabort()
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceSafeCloseable
-
abort
void abort()Closes this duplicator and aborts all stream messages returned byduplicate()
. This will also clean up the data published from the originalStreamMessage
. -
abort
Closes this duplicator and aborts all stream messages returned byduplicate()
with the specifiedThrowable
. This will also clean up the data published from the originalStreamMessage
.
-