T - the type of elementspublic interface StreamMessageDuplicator<T> extends SafeCloseable
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.
| Modifier and Type | Method and Description |
|---|---|
void |
abort()
Closes this duplicator and aborts all stream messages returned by
duplicate(). |
void |
abort(Throwable cause)
Closes this duplicator and aborts all stream messages returned by
duplicate()
with the specified Throwable. |
void |
close()
Closes this duplicator and prevents it from further duplication.
|
StreamMessage<T> |
duplicate()
Returns a new
StreamMessage that publishes the same elements as the StreamMessage
that this duplicator is created from. |
StreamMessage<T> duplicate()
StreamMessage that publishes the same elements as the StreamMessage
that this duplicator is created from.void close()
duplicate() will raise
an IllegalStateException 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 original StreamMessage are cleaned up when
all duplicated streams are complete. If you want to stop publishing and clean
up the resources immediately, call abort().
close in interface AutoCloseableclose in interface SafeCloseablevoid abort()
duplicate().
This will also clean up the data published from the original StreamMessage.void abort(Throwable cause)
duplicate()
with the specified Throwable.
This will also clean up the data published from the original StreamMessage.Copyright © 2020 LeanCloud. All rights reserved.