类 MessageHeaderAccessor
- java.lang.Object
- org.springframework.messaging.support.MessageHeaderAccessor
- 直接已知子类:
NativeMessageHeaderAccessor
public class MessageHeaderAccessor extends Object
A base for classes providing strongly typed getters and setters as well as behavior around specific categories of headers (e.g. STOMP headers). Supports creating new headers, modifying existing headers (when still mutable), or copying and modifying existing headers.The method
getMessageHeaders()
provides access to the underlying, fully-preparedMessageHeaders
that can then be used as-is (i.e. without copying) to create a single message as follows:MessageHeaderAccessor accessor = new MessageHeaderAccessor(); accessor.setHeader("foo", "bar"); Message message = MessageBuilder.createMessage("payload", accessor.getMessageHeaders());
After the above, by default the
MessageHeaderAccessor
becomes immutable. However it is possible to leave it mutable for further initialization in the same thread, for example:MessageHeaderAccessor accessor = new MessageHeaderAccessor(); accessor.setHeader("foo", "bar"); accessor.setLeaveMutable(true); Message message = MessageBuilder.createMessage("payload", accessor.getMessageHeaders()); // later on in the same thread... MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message); accessor.setHeader("bar", "baz"); accessor.setImmutable();
The method
toMap()
returns a copy of the underlying headers. It can be used to prepare multiple messages from the sameMessageHeaderAccessor
instance:MessageHeaderAccessor accessor = new MessageHeaderAccessor(); MessageBuilder builder = MessageBuilder.withPayload("payload").setHeaders(accessor); accessor.setHeader("foo", "bar1"); Message message1 = builder.build(); accessor.setHeader("foo", "bar2"); Message message2 = builder.build(); accessor.setHeader("foo", "bar3"); Message message3 = builder.build();
However note that with the above style, the header accessor is shared and cannot be re-obtained later on. Alternatively it is also possible to create one
MessageHeaderAccessor
per message:MessageHeaderAccessor accessor1 = new MessageHeaderAccessor(); accessor.set("foo", "bar1"); Message message1 = MessageBuilder.createMessage("payload", accessor1.getMessageHeaders()); MessageHeaderAccessor accessor2 = new MessageHeaderAccessor(); accessor.set("foo", "bar2"); Message message2 = MessageBuilder.createMessage("payload", accessor2.getMessageHeaders()); MessageHeaderAccessor accessor3 = new MessageHeaderAccessor(); accessor.set("foo", "bar3"); Message message3 = MessageBuilder.createMessage("payload", accessor3.getMessageHeaders());
Note that the above examples aim to demonstrate the general idea of using header accessors. The most likely usage however is through subclasses.
- 从以下版本开始:
- 4.0
- 作者:
- Rossen Stoyanchev, Juergen Hoeller
字段概要
字段 修饰符和类型 字段 说明 static Charset
DEFAULT_CHARSET
构造器概要
构造器 构造器 说明 MessageHeaderAccessor()
A constructor to create new headers.MessageHeaderAccessor(Message<?> message)
A constructor accepting the headers of an existing message to copy.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 void
copyHeaders(Map<String,?> headersToCopy)
Copy the name-value pairs from the provided Map.void
copyHeadersIfAbsent(Map<String,?> headersToCopy)
Copy the name-value pairs from the provided Map.protected MessageHeaderAccessor
createAccessor(Message<?> message)
Build a 'nested' accessor for the given message.static <T extends MessageHeaderAccessor>
TgetAccessor(Message<?> message, Class<T> requiredType)
Return the originalMessageHeaderAccessor
used to create the headers of the givenMessage
, ornull
if that's not available or if its type does not match the required type.static <T extends MessageHeaderAccessor>
TgetAccessor(MessageHeaders messageHeaders, Class<T> requiredType)
A variation ofgetAccessor(org.springframework.messaging.Message, Class)
with aMessageHeaders
instance instead of aMessage
.MimeType
getContentType()
String
getDetailedLogMessage(Object payload)
Return a more detailed message for logging purposes.protected String
getDetailedPayloadLogMessage(Object payload)
Object
getErrorChannel()
Object
getHeader(String headerName)
Retrieve the value for the header with the given name.UUID
getId()
MessageHeaders
getMessageHeaders()
Return the underlyingMessageHeaders
instance.static MessageHeaderAccessor
getMutableAccessor(Message<?> message)
Return a mutableMessageHeaderAccessor
for the given message attempting to match the type of accessor used to create the message headers, or otherwise wrapping the message with aMessageHeaderAccessor
instance.Object
getReplyChannel()
String
getShortLogMessage(Object payload)
Return a concise message for logging purposes.protected String
getShortPayloadLogMessage(Object payload)
Long
getTimestamp()
boolean
isModified()
Check whether the underlying message headers have been marked as modified.boolean
isMutable()
Whether the underlying headers can still be modified.protected boolean
isReadableContentType()
protected boolean
isReadOnly(String headerName)
void
removeHeader(String headerName)
Remove the value for the given header name.void
removeHeaders(String... headerPatterns)
Removes all headers provided via array of 'headerPatterns'.void
setContentType(MimeType contentType)
void
setErrorChannel(MessageChannel errorChannel)
void
setErrorChannelName(String errorChannelName)
void
setHeader(String name, Object value)
Set the value for the given header name.void
setHeaderIfAbsent(String name, Object value)
Set the value for the given header name only if the header name is not already associated with a value.void
setImmutable()
By default whengetMessageHeaders()
is called,"this"
MessageHeaderAccessor
instance can no longer be used to modify the underlying message headers.void
setLeaveMutable(boolean leaveMutable)
By default whengetMessageHeaders()
is called,"this"
MessageHeaderAccessor
instance can no longer be used to modify the underlying message headers and the returnedMessageHeaders
is immutable.protected void
setModified(boolean modified)
Mark the underlying message headers as modified.void
setReplyChannel(MessageChannel replyChannel)
void
setReplyChannelName(String replyChannelName)
Map<String,Object>
toMap()
Return a copy of the underlying header values as a plainMap
object.MessageHeaders
toMessageHeaders()
Return a copy of the underlying header values as aMessageHeaders
object.String
toString()
protected void
verifyType(String headerName, Object headerValue)
字段详细资料
DEFAULT_CHARSET
public static final Charset DEFAULT_CHARSET
构造器详细资料
MessageHeaderAccessor
public MessageHeaderAccessor()
A constructor to create new headers.
MessageHeaderAccessor
public MessageHeaderAccessor(Message<?> message)
A constructor accepting the headers of an existing message to copy.- 参数:
message
- a message to copy the headers from, ornull
if none
方法详细资料
createAccessor
protected MessageHeaderAccessor createAccessor(Message<?> message)
Build a 'nested' accessor for the given message.- 参数:
message
- the message to build a new accessor for- 返回:
- the nested accessor (typically a specific subclass)
setLeaveMutable
public void setLeaveMutable(boolean leaveMutable)
By default whengetMessageHeaders()
is called,"this"
MessageHeaderAccessor
instance can no longer be used to modify the underlying message headers and the returnedMessageHeaders
is immutable.However when this is set to
true
, the returned (underlying)MessageHeaders
instance remains mutable. To make further modifications continue to use the same accessor instance or re-obtain it via:MessageHeaderAccessor.getAccessor(Message, Class)
When modifications are complete use
setImmutable()
to prevent further changes. The intended use case for this mechanism is initialization of a Message within a single thread.By default this is set to
false
.- 从以下版本开始:
- 4.1
setImmutable
public void setImmutable()
By default whengetMessageHeaders()
is called,"this"
MessageHeaderAccessor
instance can no longer be used to modify the underlying message headers. However ifsetLeaveMutable(boolean)
is used, this method is necessary to indicate explicitly when theMessageHeaders
instance should no longer be modified.- 从以下版本开始:
- 4.1
isMutable
public boolean isMutable()
Whether the underlying headers can still be modified.- 从以下版本开始:
- 4.1
setModified
protected void setModified(boolean modified)
Mark the underlying message headers as modified.- 参数:
modified
- typicallytrue
, orfalse
to reset the flag- 从以下版本开始:
- 4.1
isModified
public boolean isModified()
Check whether the underlying message headers have been marked as modified.- 返回:
true
if the flag has been set,false
otherwise
getMessageHeaders
public MessageHeaders getMessageHeaders()
Return the underlyingMessageHeaders
instance.Unless
setLeaveMutable(boolean)
was set totrue
, after this call, the headers are immutable and this accessor can no longer modify them.This method always returns the same
MessageHeaders
instance if invoked multiples times. To obtain a copy of the underlying headers, usetoMessageHeaders()
ortoMap()
instead.- 从以下版本开始:
- 4.1
toMessageHeaders
public MessageHeaders toMessageHeaders()
Return a copy of the underlying header values as aMessageHeaders
object.This method can be invoked many times, with modifications in between where each new call returns a fresh copy of the current header values.
- 从以下版本开始:
- 4.1
toMap
public Map<String,Object> toMap()
Return a copy of the underlying header values as a plainMap
object.This method can be invoked many times, with modifications in between where each new call returns a fresh copy of the current header values.
getHeader
public Object getHeader(String headerName)
Retrieve the value for the header with the given name.- 参数:
headerName
- the name of the header- 返回:
- the associated value, or
null
if none found
setHeader
public void setHeader(String name, Object value)
Set the value for the given header name.If the provided value is
null
, the header will be removed.
verifyType
protected void verifyType(String headerName, Object headerValue)
setHeaderIfAbsent
public void setHeaderIfAbsent(String name, Object value)
Set the value for the given header name only if the header name is not already associated with a value.
removeHeader
public void removeHeader(String headerName)
Remove the value for the given header name.
removeHeaders
public void removeHeaders(String... headerPatterns)
Removes all headers provided via array of 'headerPatterns'.As the name suggests, array may contain simple matching patterns for header names. Supported pattern styles are: "xxx*", "*xxx", "*xxx*" and "xxx*yyy".
copyHeaders
public void 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.
copyHeadersIfAbsent
public void copyHeadersIfAbsent(Map<String,?> headersToCopy)
Copy the name-value pairs from the provided Map.This operation will not overwrite any existing values.
isReadOnly
protected boolean isReadOnly(String headerName)
getTimestamp
public Long getTimestamp()
setContentType
public void setContentType(MimeType contentType)
getContentType
public MimeType getContentType()
setReplyChannelName
public void setReplyChannelName(String replyChannelName)
setReplyChannel
public void setReplyChannel(MessageChannel replyChannel)
getReplyChannel
public Object getReplyChannel()
setErrorChannelName
public void setErrorChannelName(String errorChannelName)
setErrorChannel
public void setErrorChannel(MessageChannel errorChannel)
getErrorChannel
public Object getErrorChannel()
getShortLogMessage
public String getShortLogMessage(Object payload)
Return a concise message for logging purposes.- 参数:
payload
- the payload that corresponds to the headers.- 返回:
- the message
getDetailedLogMessage
public String getDetailedLogMessage(Object payload)
Return a more detailed message for logging purposes.- 参数:
payload
- the payload that corresponds to the headers.- 返回:
- the message
getShortPayloadLogMessage
protected String getShortPayloadLogMessage(Object payload)
getDetailedPayloadLogMessage
protected String getDetailedPayloadLogMessage(Object payload)
isReadableContentType
protected boolean isReadableContentType()
getAccessor
public static <T extends MessageHeaderAccessor> T getAccessor(Message<?> message, Class<T> requiredType)
Return the originalMessageHeaderAccessor
used to create the headers of the givenMessage
, ornull
if that's not available or if its type does not match the required type.This is for cases where the existence of an accessor is strongly expected (followed up with an assertion) or where an accessor will be created otherwise.
- 参数:
message
- the message to get an accessor forrequiredType
- the required accessor type (ornull
for any)- 返回:
- an accessor instance of the specified type, or
null
if none - 从以下版本开始:
- 4.1
getAccessor
public static <T extends MessageHeaderAccessor> T getAccessor(MessageHeaders messageHeaders, Class<T> requiredType)
A variation ofgetAccessor(org.springframework.messaging.Message, Class)
with aMessageHeaders
instance instead of aMessage
.This is for cases when a full message may not have been created yet.
- 参数:
messageHeaders
- the message headers to get an accessor forrequiredType
- the required accessor type (ornull
for any)- 返回:
- an accessor instance of the specified type, or
null
if none - 从以下版本开始:
- 4.1
getMutableAccessor
public static MessageHeaderAccessor getMutableAccessor(Message<?> message)
Return a mutableMessageHeaderAccessor
for the given message attempting to match the type of accessor used to create the message headers, or otherwise wrapping the message with aMessageHeaderAccessor
instance.This is for cases where a header needs to be updated in generic code while preserving the accessor type for downstream processing.
- 返回:
- an accessor of the required type (never
null
) - 从以下版本开始:
- 4.1