Class Message.MessageBuilder

java.lang.Object
dev.agenor.core.Message.MessageBuilder
Enclosing class:
Message

public static class Message.MessageBuilder extends Object
Fluent builder for constructing Message instances.

The builder provides a readable, type-safe way to construct messages with only the fields you need. It handles default value generation and ensures the immutability of the final message.

Builder Features:

  • Fluent method chaining
  • Optional fields with sensible defaults
  • Header accumulation (multiple calls add headers)
  • Immutable result

Thread Safety: This builder is NOT thread-safe. Each thread should use its own builder instance.

  • Constructor Details

    • MessageBuilder

      public MessageBuilder()
  • Method Details

    • id

      Sets the message ID.

      If not set, a random UUID will be generated automatically.

      Parameters:
      id - the message identifier
      Returns:
      this builder for method chaining
    • topic

      public Message.MessageBuilder topic(String topic)
      Sets the topic for publish-subscribe routing.

      Topics enable one-to-many communication where multiple agents can subscribe to receive messages.

      Parameters:
      topic - the topic name (e.g., "orders.created", "system.alerts")
      Returns:
      this builder for method chaining
    • senderId

      public Message.MessageBuilder senderId(String senderId)
      Sets the sender agent identifier.

      Always set this field for traceability and to enable reply functionality.

      Parameters:
      senderId - the sending agent's ID
      Returns:
      this builder for method chaining
    • receiverId

      public Message.MessageBuilder receiverId(String receiverId)
      Sets the receiver agent identifier for point-to-point messaging.

      When set, the message will be delivered directly to the specified agent, in addition to any topic-based routing.

      Parameters:
      receiverId - the receiving agent's ID
      Returns:
      this builder for method chaining
    • correlationId

      public Message.MessageBuilder correlationId(String correlationId)
      Sets the correlation ID for request-response patterns.

      The correlation ID links a response message to its originating request. Typically set to the ID of the request message.

      Parameters:
      correlationId - the correlation identifier
      Returns:
      this builder for method chaining
    • content

      public Message.MessageBuilder content(Object content)
      Sets the message content (payload).

      The content can be any object. For distributed scenarios, ensure the content is serializable.

      Parameters:
      content - the message payload
      Returns:
      this builder for method chaining
    • headers

      public Message.MessageBuilder headers(Map<String,String> headers)
      Adds multiple headers at once, merging with existing headers.

      If a header key already exists, it will be replaced with the new value.

      Parameters:
      headers - map of headers to add
      Returns:
      this builder for method chaining
    • header

      public Message.MessageBuilder header(String key, String value)
      Adds a single header, preserving existing headers.

      This method can be called multiple times to add multiple headers.

      Parameters:
      key - the header key
      value - the header value
      Returns:
      this builder for method chaining
    • timestamp

      public Message.MessageBuilder timestamp(Instant timestamp)
      Sets the message timestamp.

      If not set, the timestamp will be set to the current time when build() is called.

      Parameters:
      timestamp - the message creation timestamp
      Returns:
      this builder for method chaining
    • build

      public Message build()
      Builds an immutable Message with the accumulated values.

      This method can be called multiple times to create multiple messages with the same configuration.

      Returns:
      a new immutable Message instance