org.springframework.messaging.support

Class MessageBuilder<T>

  • java.lang.Object
    • org.springframework.messaging.support.MessageBuilder<T>
  • Type Parameters:
    T - the message payload type


    public final class MessageBuilder<T>
    extends java.lang.Object
    A builder for creating a GenericMessage (or ErrorMessage if the payload is of type Throwable).
    Since:
    4.0
    Author:
    Arjen Poutsma, Mark Fisher, Rossen Stoyanchev
    See Also:
    GenericMessage, ErrorMessage
    • Method Detail

      • setHeaders

        public MessageBuilder<T> setHeaders(MessageHeaderAccessor accessor)
        Set the message headers to use by providing a MessageHeaderAccessor.
        Parameters:
        accessor - the headers to use
      • setHeader

        public MessageBuilder<T> setHeader(java.lang.String headerName,
                                           @Nullable
                                           java.lang.Object headerValue)
        Set the value for the given header name. If the provided value is null, the header will be removed.
      • setHeaderIfAbsent

        public MessageBuilder<T> setHeaderIfAbsent(java.lang.String headerName,
                                                   java.lang.Object headerValue)
        Set the value for the given header name only if the header name is not already associated with a value.
      • removeHeaders

        public MessageBuilder<T> removeHeaders(java.lang.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".
      • removeHeader

        public MessageBuilder<T> removeHeader(java.lang.String headerName)
        Remove the value for the given header name.
      • copyHeaders

        public MessageBuilder<T> copyHeaders(@Nullable
                                             java.util.Map<java.lang.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.
      • copyHeadersIfAbsent

        public MessageBuilder<T> copyHeadersIfAbsent(@Nullable
                                                     java.util.Map<java.lang.String,?> headersToCopy)
        Copy the name-value pairs from the provided Map. This operation will not overwrite any existing values.
      • setReplyChannelName

        public MessageBuilder<T> setReplyChannelName(java.lang.String replyChannelName)
      • setErrorChannelName

        public MessageBuilder<T> setErrorChannelName(java.lang.String errorChannelName)
      • fromMessage

        public static <T> MessageBuilder<T> fromMessage(Message<T> message)
        Create a builder for a new Message instance pre-populated with all of the headers copied from the provided message. The payload of the provided Message will also be used as the payload for the new message.
        Parameters:
        message - the Message from which the payload and all headers will be copied
      • withPayload

        public static <T> MessageBuilder<T> withPayload(T payload)
        Create a new builder for a message with the given payload.
        Parameters:
        payload - the payload
      • createMessage

        public static <T> Message<T> createMessage(@Nullable
                                                   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 - the payload to use (never null)
        messageHeaders - the headers to use (never null)
        Returns:
        the created message
        Since:
        4.1