Class MessagingService

java.lang.Object
dev.ua.theroer.magicutils.messaging.MessagingService
All Implemented Interfaces:
AutoCloseable

public final class MessagingService extends Object implements AutoCloseable
High-level entry point for MagicUtils cross-server messaging.

Owns a MessageBus over a chosen MessageTransport and is the component registered with a MagicRuntime. The transport is selected at build time: when Redis is enabled in RedisConfig.Redis a RedisMessageTransport is used; otherwise the platform's default plugin-messaging transport is used, so the network runs without Redis.

  • Method Details

    • builder

      public static MessagingService.Builder builder(MessageSource self)
      Creates a service builder.
      Parameters:
      self - this member's identity
      Returns:
      builder
    • bus

      public MessageBus bus()
      Returns the underlying bus.
      Returns:
      message bus
    • transportName

      public String transportName()
      Returns the name of the active transport ("redis" or "plugin-messaging" etc.).
      Returns:
      transport name
    • isConnected

      public boolean isConnected()
      Returns whether the active transport is currently connected.
      Returns:
      true when connected
    • publish

      public void publish(Target target, String channel, @Nullable @Nullable Object payload)
      Publishes a payload on a channel to a target. Delegates to the bus.
      Parameters:
      target - addressing hint
      channel - channel name
      payload - payload object
    • broadcast

      public void broadcast(String channel, @Nullable @Nullable Object payload)
      Broadcasts a payload on a channel. Delegates to the bus.
      Parameters:
      channel - channel name
      payload - payload object
    • subscribe

      public <T> ListenerSubscription subscribe(String channel, Class<T> type, MessageHandler<T> handler)
      Subscribes a typed handler to a channel. Delegates to the bus.
      Type Parameters:
      T - payload type
      Parameters:
      channel - channel name
      type - payload type
      handler - handler
      Returns:
      subscription handle
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable