Class Message.MessageBuilder
- Enclosing class:
Message
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds an immutable Message with the accumulated values.Sets the message content (payload).correlationId(String correlationId) Sets the correlation ID for request-response patterns.Adds a single header, preserving existing headers.Adds multiple headers at once, merging with existing headers.Sets the message ID.receiverId(String receiverId) Sets the receiver agent identifier for point-to-point messaging.Sets the sender agent identifier.Sets the message timestamp.Sets the topic for publish-subscribe routing.
-
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
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
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
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
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
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
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
Adds a single header, preserving existing headers.This method can be called multiple times to add multiple headers.
- Parameters:
key- the header keyvalue- the header value- Returns:
- this builder for method chaining
-
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
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
-