Class MessageBus
java.lang.Object
dev.ua.theroer.magicutils.messaging.MessageBus
- All Implemented Interfaces:
AutoCloseable
Typed cross-server publish/subscribe bus for MagicUtils plugins.
Wraps a pluggable MessageTransport (the default per-platform
plugin-messaging transport, or Redis) with a channel-oriented, typed API:
subscribe(String, Class, MessageHandler) and
publish(Target, String, Object). Payloads are serialized with a shared
MessageCodec. The bus applies Target addressing on receipt so
routing behaves identically regardless of which transport is in use.
The bus is AutoCloseable and is meant to be registered with a
MagicRuntime so it is torn down with the plugin.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidBroadcasts a payload on a channel to every subscriber on the network.static MessageBus.Builderbuilder(MessageSource self, MessageTransport transport) Creates a bus builder.voidclose()codec()Returns the codec used for payload serialization.voidhostsPlayer(@Nullable Predicate<UUID> hostsPlayer) Sets the predicate answering whether this member hosts a given player id.voidPublishes a payload on a channel to the given target.self()Returns this member's identity.subscribe(String channel, Class<T> type, MessageHandler<T> handler) Subscribes a typed handler to a channel.Returns the underlying transport.
-
Method Details
-
builder
Creates a bus builder.- Parameters:
self- this member's identitytransport- underlying transport- Returns:
- builder
-
self
-
transport
-
codec
Returns the codec used for payload serialization.- Returns:
- message codec
-
hostsPlayer
Sets the predicate answering whether this member hosts a given player id.Used to honour
Target.player(UUID)on backends. Ignored on the proxy. May be updated at any time (for example wired to the online-player registry).- Parameters:
hostsPlayer- predicate, or null to disable player-target delivery
-
publish
-
broadcast
-
subscribe
Subscribes a typed handler to a channel.- Type Parameters:
T- payload type- Parameters:
channel- logical channel nametype- payload typehandler- message handler- Returns:
- subscription handle; close it to unsubscribe
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-