Class Message

java.lang.Object
io.fluxzero.sdk.common.Message
All Implemented Interfaces:
HasMetadata, HasMessage
Direct Known Subclasses:
Schedule, WebRequest, WebResponse

public class Message extends Object implements HasMessage
Represents a unit of communication within the Fluxzero Runtime, wrapping a payload and its metadata.

A Message serves as the base class for all messages in Fluxzero (e.g., commands, events, queries, web requests). It contains a strongly typed payload, immutable metadata, a globally unique message ID, and a timestamp.

Messages can be enriched with metadata, transformed with a new payload, or serialized for transmission. Logical redispatch retains the message ID unless explicitly replaced; it does not forward an incoming transport envelope. Source, target, request ID and log index belong to SerializedMessage, not this value. Request transport sets source to the sending client for response correlation. Local in-process handling has no transport source. Blocking gateway methods do not determine whether delivery is local or remote.

Key Features

  • Immutable, with withX and addMetadata methods for safe transformation
  • Supports auto-generation of message ID and timestamp if not provided
  • Lazy routing key computation via getRoutingKey()
See Also:
  • Constructor Details

    • Message

      public Message(Object payload)
      Constructs a message with the given payload and empty metadata.
      Parameters:
      payload - the message payload
    • Message

      public Message(Object payload, Metadata metadata)
      Constructs a message with the given payload and metadata.
      Parameters:
      payload - the message payload
      metadata - the associated metadata
    • Message

      @ConstructorProperties({"payload","metadata","messageId","timestamp"}) public Message(Object payload, Metadata metadata, String messageId, Instant timestamp)
      Full constructor for internal use and deserialization.
      Parameters:
      payload - the payload
      metadata - metadata for the message
      messageId - optional unique ID; auto-generated if null
      timestamp - optional creation time; defaults to current time if null
  • Method Details

    • asMessage

      public static Message asMessage(Object object)
      Converts any object into a Message. If the object already implements HasMessage, the existing message is returned.
      Parameters:
      object - the input object to wrap
      Returns:
      a Message instance
    • getPayload

      public <R> R getPayload()
      Returns the strongly typed payload of the message.
      Specified by:
      getPayload in interface HasMessage
      Type Parameters:
      R - the expected type of the payload
      Returns:
      the payload
    • toMessage

      public Message toMessage()
      Description copied from interface: HasMessage
      Returns the underlying Message representation of this object.
      Specified by:
      toMessage in interface HasMessage
      Returns:
      the Message backing this instance
    • getRoutingKey

      public Optional<String> getRoutingKey()
      Returns the lazily computed routing key. The value, including an invalid null returned by a custom HasMessage.computeRoutingKey() implementation, is computed at most once per message.
    • withPayload

      public Message withPayload(Object payload)
      Returns a new message instance with the provided payload and existing metadata, ID, and timestamp.
    • withMetadata

      public Message withMetadata(Metadata metadata)
      Returns a new message with the supplied metadata.
    • withMessageId

      public Message withMessageId(String messageId)
      Returns a new message with the supplied technical identifier.
    • withTimestamp

      public Message withTimestamp(Instant timestamp)
      Returns a new message with the supplied timestamp.
    • addMetadata

      public Message addMetadata(Metadata metadata)
      Returns a new message with the combined metadata.
    • addMetadata

      public Message addMetadata(String key, Object value)
      Adds a single metadata entry.
    • addMetadata

      public Message addMetadata(Object... keyValues)
      Adds multiple metadata entries.
    • addMetadata

      public Message addMetadata(Map<String,?> values)
      Adds metadata from a given map.
    • addUser

      public Message addUser(User user)
      Attaches a user object to the metadata using the configured UserProvider.
    • serialize

      public SerializedMessage serialize(Serializer serializer)
      Serializes this message to a SerializedMessage using the provided Serializer.