Top Description Fields Constructors Methods
org.springframework.messaging.support

public final Class MessageBuilder<T>

extends Object
Class Inheritance
Type Parameters
<T>
the message payload type
Imports
java.util.Map, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageChannel, .MessageHeaders, org.springframework.util.Assert

A builder for creating a GenericMessage (or ErrorMessage if the payload is of type Throwable).
Authors
Arjen Poutsma, Mark Fisher, Rossen Stoyanchev, Sam Brannen
Since
4.0
See Also
GenericMessage, ErrorMessage

Field Summary

Modifier and TypeField and Description
private MessageHeaderAccessor
private final T
private final Message<T>

Constructor Summary

AccessConstructor and Description
private
MessageBuilder(Message<T> providedMessage)

private

Method Summary

Modifier and TypeMethod and Description
public Message<T>
public MessageBuilder<T>
copyHeaders(Map<String, ?> headersToCopy)

Copy the name-value pairs from the provided Map.

public MessageBuilder<T>
copyHeadersIfAbsent(Map<String, ?> headersToCopy)

Copy the name-value pairs from the provided Map.

public static <T> Message<T>

Returns:

the created message
createMessage
(T
the payload to use (never null)
payload
,
MessageHeaders
the headers to use (never null)
messageHeaders
)

A shortcut factory method for creating a message with the given payload and MessageHeaders.

public static <T> MessageBuilder<T>
fromMessage(Message<T>
the Message from which the payload and all headers will be copied
message
)

Create a builder for a new Message instance pre-populated with all the headers copied from the provided message.

public MessageBuilder<T>
removeHeader(String headerName)

Remove the value for the given header name.

public MessageBuilder<T>
removeHeaders(String... headerPatterns)

Removes all headers provided via array of 'headerPatterns'.

public MessageBuilder<T>
public MessageBuilder<T>
setErrorChannelName(String errorChannelName)

public MessageBuilder<T>
setHeader(String headerName, Object headerValue)

Set the value for the given header name.

public MessageBuilder<T>
setHeaderIfAbsent(String headerName, Object headerValue)

Set the value for the given header name only if the header name is not already associated with a value.

public MessageBuilder<T>
setHeaders(MessageHeaderAccessor
the headers to use
accessor
)

Set the message headers to use by providing a MessageHeaderAccessor.

public MessageBuilder<T>
public MessageBuilder<T>
setReplyChannelName(String replyChannelName)

public static <T> MessageBuilder<T>
withPayload(T
the payload
payload
)

Create a new builder for a message with the given payload.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

headerAccessorback to summary
private MessageHeaderAccessor headerAccessor
payloadback to summary
private final T payload
providedMessageback to summary
private final Message<T> providedMessage
Annotations
@Nullable

Constructor Detail

MessageBuilderback to summary
private MessageBuilder(Message<T> providedMessage)
MessageBuilderback to summary
private MessageBuilder(T payload, MessageHeaderAccessor accessor)

Method Detail

buildback to summary
public Message<T> build()
Annotations
@SuppressWarnings:unchecked
copyHeadersback to summary
public MessageBuilder<T> copyHeaders(Map<String, ?> headersToCopy)

Copy the name-value pairs from the provided Map. This operation will overwrite any existing values. Use copyHeadersIfAbsent(Map) to avoid overwriting values. Note that the 'id' and 'timestamp' header values will never be overwritten.

copyHeadersIfAbsentback to summary
public MessageBuilder<T> copyHeadersIfAbsent(Map<String, ?> headersToCopy)

Copy the name-value pairs from the provided Map. This operation will not overwrite any existing values.

createMessageback to summary
public static <T> Message<T> createMessage(T payload, MessageHeaders messageHeaders)

A shortcut factory method for creating a message with the given payload and MessageHeaders.

Note

the given MessageHeaders instance is used directly in the new message, i.e. it is not copied.

Parameters
payload:T

the payload to use (never null)

messageHeaders:MessageHeaders

the headers to use (never null)

Returns:Message<T>

the created message

Annotations
@SuppressWarnings:unchecked
Since
4.1
fromMessageback to summary
public static <T> MessageBuilder<T> fromMessage(Message<T> message)

Create a builder for a new Message instance pre-populated with all the headers copied from the provided message. The payload of the provided Message will also be used as the payload for the new message.

If the provided message is an ErrorMessage, the originalMessage it contains, will be passed on to new instance.

Parameters
message:Message<T>

the Message from which the payload and all headers will be copied

removeHeaderback to summary
public MessageBuilder<T> removeHeader(String headerName)

Remove the value for the given header name.

removeHeadersback to summary
public MessageBuilder<T> removeHeaders(String... headerPatterns)

Removes all headers provided via array of 'headerPatterns'. As the name suggests the array may contain simple matching patterns for header names. Supported pattern styles are: "xxx*", "*xxx", "*xxx*" and "xxx*yyy".

setErrorChannelback to summary
public MessageBuilder<T> setErrorChannel(MessageChannel errorChannel)
setErrorChannelNameback to summary
public MessageBuilder<T> setErrorChannelName(String errorChannelName)
setHeaderback to summary
public MessageBuilder<T> setHeader(String headerName, Object headerValue)

Set the value for the given header name. If the provided value is null, the header will be removed.

setHeaderIfAbsentback to summary
public MessageBuilder<T> setHeaderIfAbsent(String headerName, Object headerValue)

Set the value for the given header name only if the header name is not already associated with a value.

setHeadersback to summary
public MessageBuilder<T> setHeaders(MessageHeaderAccessor accessor)

Set the message headers to use by providing a MessageHeaderAccessor.

Parameters
accessor:MessageHeaderAccessor

the headers to use

setReplyChannelback to summary
public MessageBuilder<T> setReplyChannel(MessageChannel replyChannel)
setReplyChannelNameback to summary
public MessageBuilder<T> setReplyChannelName(String replyChannelName)
withPayloadback to summary
public static <T> MessageBuilder<T> withPayload(T payload)

Create a new builder for a message with the given payload.

Parameters
payload:T

the payload