Sends a value to all current subscribers. @param value the value to broadcast @return an Awaitable that completes when all subscribers have accepted the value @throws ChannelClosedException if the broadcast channel is closed
(T value)
| 108 | * @throws ChannelClosedException if the broadcast channel is closed |
| 109 | */ |
| 110 | public Awaitable<Void> send(T value) { |
| 111 | Objects.requireNonNull(value, "value must not be null"); |
| 112 | if (closed) throw new ChannelClosedException("BroadcastChannel is closed"); |
| 113 | CompletableFuture<?>[] futures = subscribers.stream() |
| 114 | .map(sub -> { |
| 115 | try { |
| 116 | return sub.send(value).toCompletableFuture(); |
| 117 | } catch (ChannelClosedException e) { |
| 118 | return CompletableFuture.completedFuture(null); |
| 119 | } |
| 120 | }) |
| 121 | .toArray(CompletableFuture[]::new); |
| 122 | return GroovyPromise.of(CompletableFuture.allOf(futures)); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Closes this broadcast channel and all subscriber channels. |