Class MessageEnvelope

java.lang.Object
dev.ua.theroer.magicutils.messaging.MessageEnvelope

public final class MessageEnvelope extends Object
Wire representation of a single message on the bus.

The envelope is transport-neutral: both the default plugin-messaging transport and the Redis transport serialize it to bytes (via MessageCodec) and reconstruct it on the receiving side. It carries the logical channel, the addressing hint, the originating MessageSource, and the already-serialized payload bytes so the transport never needs to understand the user payload type.

  • Constructor Details

    • MessageEnvelope

      public MessageEnvelope(UUID id, String channel, Target target, MessageSource source, byte[] payload, long timestamp)
      Creates an envelope.
      Parameters:
      id - unique message id (for de-duplication / correlation)
      channel - logical channel name
      target - addressing hint
      source - originating member
      payload - serialized payload bytes
      timestamp - epoch millis when the message was created
  • Method Details

    • create

      public static MessageEnvelope create(String channel, Target target, MessageSource source, byte[] payload)
      Creates an envelope with a fresh id and the current timestamp.
      Parameters:
      channel - logical channel name
      target - addressing hint
      source - originating member
      payload - serialized payload bytes
      Returns:
      new envelope
    • id

      public UUID id()
      Returns the unique message id.
      Returns:
      message id
    • channel

      public String channel()
      Returns the logical channel name.
      Returns:
      channel name
    • target

      public Target target()
      Returns the addressing hint.
      Returns:
      target
    • source

      public MessageSource source()
      Returns the originating member.
      Returns:
      source
    • payload

      public byte[] payload()
      Returns the serialized payload bytes.
      Returns:
      payload bytes (do not mutate)
    • timestamp

      public long timestamp()
      Returns the creation timestamp (epoch millis).
      Returns:
      timestamp
    • acceptedBy

      public boolean acceptedBy(MessageSource self, @Nullable Predicate<UUID> hostsPlayer)
      Decides whether a local member should accept this envelope given the target.

      Transports that cannot pre-filter (for example Redis pub/sub, which broadcasts to every subscriber) call this on receipt so addressing is honoured uniformly regardless of transport.

      Parameters:
      self - the local member
      hostsPlayer - predicate answering whether the local member hosts a player id; may be null on the proxy or when player hosting is unknown
      Returns:
      true when the local member is an intended recipient