Class Message

java.lang.Object
io.fluxcapacitor.javaclient.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 Flux platform, wrapping a payload and its metadata.

A Message serves as the base class for all messages in Flux (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.

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
    • withPayload

      public Message withPayload(Object payload)
      Returns a new message instance with the provided payload and existing metadata, ID, and 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.