public final class BroadcastChannel<T>
extends Object
A one-to-many broadcast channel where each value sent is delivered to all subscribers.
Unlike AsyncChannel (point-to-point, each value consumed by
one receiver), a BroadcastChannel delivers every value to
every subscriber that has called subscribe().
def broadcast = BroadcastChannel.create()
def sub1 = broadcast.subscribe()
def sub2 = broadcast.subscribe()
async {
broadcast.send('hello')
broadcast.send('world')
broadcast.close()
}
// Both subscribers receive both values
for await (msg in sub1) { println "Sub1: $msg" }
for await (msg in sub2) { println "Sub2: $msg" }
Inspired by GPars' DataflowBroadcast.
T - the value type| Type Params | Return Type | Name and description |
|---|---|---|
|
public Flow.Publisher<T> |
asPublisher()Returns a Flow.Publisher view of this broadcast channel. |
|
public void |
close()Closes this broadcast channel and all subscriber channels. |
|
public boolean |
close(Throwable cause)Closes this broadcast channel because cause failed the producer. |
<T> |
public static BroadcastChannel<T> |
create()Creates a new broadcast channel. |
|
public Throwable |
getCloseCause()Returns the failure passed to close(Throwable), or null
when this broadcast is open or was closed normally. |
|
public int |
getSubscriberCount()Returns the number of current subscribers. |
|
public boolean |
isClosed()Returns true if this broadcast channel has been closed. |
|
public Awaitable<Void> |
send(T value)Sends a value to all current subscribers. |
|
public AsyncChannel<T> |
subscribe()Creates a new subscriber channel. |
|
public AsyncChannel<T> |
subscribe(int bufferSize)Creates a new subscriber channel with the specified buffer capacity. |
Returns a Flow.Publisher view of this broadcast channel. Each call to Flow.Publisher#subscribe(Flow.Subscriber) on the returned publisher creates a new AsyncChannel subscriber under the hood, draining values to the downstream subscriber according to its requested demand.
Semantics:
request(n); the worker blocks the
broadcast send when no demand exists (sender-side backpressure).onComplete when the subscriber channel
is closed normally and drained. close(Throwable) signals
onError with that failure instead. A subscriber that
arrives after the broadcast has closed gets the same signal
straight after onSubscribe, so for await over it
ends at once or throws the failure.
Backpressure policy (important). This bridge uses lossless,
sender-gated backpressure: send(Object) awaits delivery to
every live subscriber, and each per-subscriber channel has a bounded
buffer (default 16). A subscriber that never calls request(n),
or that requests slowly, will fill its buffer; once full, the
subscriber's channel suspends its backing send, which in turn
stalls BroadcastChannel.send(...) for all
subscribers. In other words, the slowest subscriber controls producer
throughput.
This is intentional and matches the point-to-point semantics of
subscribe(): values are neither dropped nor reordered. If
you need decoupled per-subscriber policies (drop-newest, drop-oldest,
latest-only, or unbounded buffering), wrap the publisher with a
Reactive Streams operator of your choice, or use a subscriber that
drains promptly with request(Long.MAX_VALUE).
Flow.Publisher backed by per-subscriber channelsCloses this broadcast channel and all subscriber channels. Idempotent. A later close(Throwable) does not add a failure.
Closes this broadcast channel because cause failed the producer.
Values already accepted by a subscriber remain receivable. Iteration
on each subscriber throws cause. null is a normal close,
the same as close(). Returns true when this call closes
the broadcast. The first close wins, so a later call returns false
and leaves the retained failure unchanged. A later subscribe()
fails at once with ChannelClosedException carrying this cause.
A later subscriber of asPublisher() receives onError
with this cause, or onComplete after a normal close.
cause - the failure each subscriber should surface, or nulltrue if this call closed the broadcastCreates a new broadcast channel.
T - the value type Returns the failure passed to close(Throwable), or null
when this broadcast is open or was closed normally.
nullReturns the number of current subscribers.
Returns true if this broadcast channel has been closed.
Sends a value to all current subscribers.
value - the value to broadcastCreates a new subscriber channel. The returned AsyncChannel will receive all values sent to this broadcast from this point forward. Each subscriber is independent — values are buffered per subscriber.
Creates a new subscriber channel with the specified buffer capacity.
bufferSize - the buffer capacity for this subscriber