Interface MessageChannel<P extends ChannelPayload>
- Type Parameters:
P- the type of payload handled by this channel; must extendChannelPayload
public interface MessageChannel<P extends ChannelPayload>
A typed communication pipe for sending and receiving messages across the
CommandBridge network.
A MessageChannel is bound to a specific payload type P,
providing type-safe message routing. Target one or more servers with
to(java.util.Collection), broadcast to all connected servers with
toAll(), or subscribe to incoming messages with
listen(dev.objz.commandbridge.api.message.MessageListener).
All send operations return CompletableFuture
and are non-blocking.
To send a payload to a specific server:
channel.to(List.of(Platform.backend("survival-1")))
.send(payload);
To broadcast to every connected server:
channel.toAll().send(payload);
To subscribe to incoming messages and cancel the listener later:
Subscription sub = channel.listen((ctx, payload) ->
System.out.println("From " + ctx.from().id()));
sub.cancel();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceMessageChannel.Sender<P extends ChannelPayload>A target-bound sender created byto(java.util.Collection)ortoAll(), used to dispatch payloads to the targeted servers. -
Method Summary
Modifier and TypeMethodDescriptionlisten(MessageListener<P> listener) Subscribes to messages received on this channel.to(Collection<Platform.ServerTarget> targets) Returns aMessageChannel.Sendertargeting the specified servers.toAll()Returns aMessageChannel.Senderthat broadcasts to all servers currently connected to the bridge network.
-
Method Details
-
to
Returns aMessageChannel.Sendertargeting the specified servers.- Parameters:
targets- the destination servers; must not benullor empty. The collection is captured at call time.- Returns:
- a
Senderscoped to the given targets - See Also:
-
toAll
MessageChannel.Sender<P> toAll()Returns aMessageChannel.Senderthat broadcasts to all servers currently connected to the bridge network.- Returns:
- a
Sendertargeting every currently connected server. The set of servers is determined at send time, not at the time this method is called. - See Also:
-
listen
Subscribes to messages received on this channel.- Parameters:
listener- the callback to invoke for each incoming message; must not benull- Returns:
- a
Subscriptionthat can be cancelled viaSubscription.cancel()to stop receiving messages. The listener remains active until cancelled. - See Also:
-