Package org.apache.camel
Interface Message
-
public interface Message
Implements the Message pattern and represents an inbound or outbound message as part of anExchange
. Headers is represented in Camel using aCaseInsensitiveMap
. The implementation of the map can be configured by theHeadersMapFactory
which can be set on theCamelContext
. The default implementation uses theCaseInsensitiveMap
.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Message
copy()
Creates a copy of this message so that it can be used and possibly modified further in another exchange.void
copyFrom(Message message)
Copies the contents of the other message into this messagevoid
copyFromWithNewBody(Message message, Object newBody)
Copies the contents (except the body) of the other message into this message and uses the provided new body insteadObject
getBody()
Returns the body of the message as a POJO<T> T
getBody(Class<T> type)
Returns the body as the specified typeExchange
getExchange()
Returns the exchange this message is related toObject
getHeader(String name)
Accesses a specific header<T> T
getHeader(String name, Class<T> type)
Returns a header associated with this message by name and specifying the type requiredObject
getHeader(String name, Object defaultValue)
Accesses a specific header<T> T
getHeader(String name, Object defaultValue, Class<T> type)
Returns a header associated with this message by name and specifying the type requiredObject
getHeader(String name, Supplier<Object> defaultValueSupplier)
Accesses a specific header<T> T
getHeader(String name, Supplier<Object> defaultValueSupplier, Class<T> type)
Returns a header associated with this message by name and specifying the type requiredMap<String,Object>
getHeaders()
Returns all the headers associated with the message.Object
getMandatoryBody()
Returns the body of the message as a POJO<T> T
getMandatoryBody(Class<T> type)
Returns the mandatory body as the specified typeString
getMessageId()
Returns the id of the message.long
getMessageTimestamp()
Returns the timestamp that this messages originates from.boolean
hasHeaders()
Returns whether has any headers has been set.boolean
hasMessageId()
Whether the message has any message ID assigned.Object
removeHeader(String name)
Removes the named header from this messageboolean
removeHeaders(String pattern)
Removes the headers from this messageboolean
removeHeaders(String pattern, String... excludePatterns)
Removes the headers from this message that match the given pattern, except for the ones matching one or more excludePatternsvoid
reset()
Clears the message from user data, so the message can be reused.void
setBody(Object body)
Sets the body of the message<T> void
setBody(Object body, Class<T> type)
Sets the body of the message as a specific typevoid
setHeader(String name, Object value)
Sets a header on the messagevoid
setHeaders(Map<String,Object> headers)
Set all the headers associated with this messagevoid
setMessageId(String messageId)
Sets the id of the message
-
-
-
Method Detail
-
reset
void reset()
Clears the message from user data, so the message can be reused. Important: This API is NOT intended for Camel end users, but used internally by Camel itself.
-
getMessageId
String getMessageId()
Returns the id of the message. By default, the message uses the same id asExchange.getExchangeId()
as messages are associated with the exchange and using different IDs does not offer much value. Another reason is to optimize for performance to avoid generating new IDs. A few Camel components do provide their own message IDs such as the JMS components.- Returns:
- the message id
-
getMessageTimestamp
long getMessageTimestamp()
Returns the timestamp that this messages originates from. Some systems like JMS, Kafka, AWS have a timestamp on the event/message, that Camel received. This method returns the timestamp, if a timestamp exists. The message timestamp and exchange created are not the same. An exchange always have a created timestamp which is the local timestamp when Camel created the exchange. The message timestamp is only available in some Camel components when the consumer is able to extract the timestamp from the source event.- Returns:
- the timestamp, or 0 if the message has no source timestamp.
- See Also:
Exchange.getCreated()
-
setMessageId
void setMessageId(String messageId)
Sets the id of the message- Parameters:
messageId
- id of the message
-
hasMessageId
boolean hasMessageId()
Whether the message has any message ID assigned.
-
getExchange
Exchange getExchange()
Returns the exchange this message is related to- Returns:
- the exchange
-
getHeader
Object getHeader(String name)
Accesses a specific header- Parameters:
name
- name of header- Returns:
- the value of the given header or null if there is no header for the given name
-
getHeader
Object getHeader(String name, Object defaultValue)
Accesses a specific header- Parameters:
name
- name of headerdefaultValue
- the default value to return if header was absent- Returns:
- the value of the given header or defaultValue if there is no header for the given name
-
getHeader
Object getHeader(String name, Supplier<Object> defaultValueSupplier)
Accesses a specific header- Parameters:
name
- name of headerdefaultValueSupplier
- the default value supplier used to generate the value to return if header was absent- Returns:
- the value of the given header or he value generated by the defaultValueSupplier if there is no header for the given name
-
getHeader
<T> T getHeader(String name, Class<T> type)
Returns a header associated with this message by name and specifying the type required- Parameters:
name
- the name of the headertype
- the type of the header- Returns:
- the value of the given header or null if there is no header for the given name
- Throws:
TypeConversionException
- is thrown if error during type conversion
-
getHeader
<T> T getHeader(String name, Object defaultValue, Class<T> type)
Returns a header associated with this message by name and specifying the type required- Parameters:
name
- the name of the headerdefaultValue
- the default value to return if header was absenttype
- the type of the header- Returns:
- the value of the given header or defaultValue if there is no header for the given name or null if it cannot be converted to the given type
-
getHeader
<T> T getHeader(String name, Supplier<Object> defaultValueSupplier, Class<T> type)
Returns a header associated with this message by name and specifying the type required- Parameters:
name
- the name of the headerdefaultValueSupplier
- the default value supplier used to generate the value to return if header was absenttype
- the type of the header- Returns:
- the value of the given header or he value generated by the defaultValueSupplier if there is no header for the given name or null if it cannot be converted to the given type
-
setHeader
void setHeader(String name, Object value)
Sets a header on the message- Parameters:
name
- of the headervalue
- to associate with the name
-
removeHeader
Object removeHeader(String name)
Removes the named header from this message- Parameters:
name
- name of the header- Returns:
- the old value of the header
-
removeHeaders
boolean removeHeaders(String pattern)
Removes the headers from this message- Parameters:
pattern
- pattern of names- Returns:
- boolean whether any headers matched
-
removeHeaders
boolean removeHeaders(String pattern, String... excludePatterns)
Removes the headers from this message that match the given pattern, except for the ones matching one or more excludePatterns- Parameters:
pattern
- pattern of names that should be removedexcludePatterns
- one or more pattern of header names that should be excluded (= preserved)- Returns:
- boolean whether any headers matched
-
getHeaders
Map<String,Object> getHeaders()
Returns all the headers associated with the message. Headers is represented in Camel using aCaseInsensitiveMap
. The implementation of the map can be configured by theHeadersMapFactory
which can be set on theCamelContext
. The default implementation uses theCaseInsensitiveMap
. Important: If you want to walk the returnedMap
and fetch all the keys and values, you should use theMap.entrySet()
method, which ensure you get the keys in the original case.- Returns:
- all the headers in a Map
-
setHeaders
void setHeaders(Map<String,Object> headers)
Set all the headers associated with this message Important: If you want to copy headers from anotherMessage
to thisMessage
, then use getHeaders().putAll(other) to copy the headers, where other is the other headers.- Parameters:
headers
- headers to set
-
hasHeaders
boolean hasHeaders()
Returns whether has any headers has been set.- Returns:
- true if any headers has been set
-
getBody
Object getBody()
Returns the body of the message as a POJO The body can be null if no body is set. Notice if the message body is stream based then calling this method multiple times may lead to the stream not being able to be re-read again. You can enable stream caching and call theStreamCache.reset()
method to reset the stream to be able to re-read again (if possible). See more details about stream caching.- Returns:
- the body, can be null
-
getMandatoryBody
Object getMandatoryBody() throws InvalidPayloadException
Returns the body of the message as a POJO Notice if the message body is stream based then calling this method multiple times may lead to the stream not being able to be re-read again. See more details about stream caching.- Returns:
- the body, is never null
- Throws:
InvalidPayloadException
- Is thrown if the body being null or wrong class type
-
getBody
<T> T getBody(Class<T> type)
Returns the body as the specified type Notice if the message body is stream based then calling this method multiple times may lead to the stream not being able to be re-read again. You can enable stream caching and call theStreamCache.reset()
method to reset the stream to be able to re-read again (if possible). See more details about stream caching.- Parameters:
type
- the type that the body- Returns:
- the body of the message as the specified type, or null if no body exists
- Throws:
TypeConversionException
- is thrown if error during type conversion
-
getMandatoryBody
<T> T getMandatoryBody(Class<T> type) throws InvalidPayloadException
Returns the mandatory body as the specified type Notice if the message body is stream based then calling this method multiple times may lead to the stream not being able to be re-read again. You can enable stream caching and call theStreamCache.reset()
method to reset the stream to be able to re-read again (if possible). See more details about stream caching.- Parameters:
type
- the type that the body- Returns:
- the body of the message as the specified type, is never null.
- Throws:
InvalidPayloadException
- Is thrown if the body being null or wrong class type
-
setBody
void setBody(Object body)
Sets the body of the message- Parameters:
body
- the body
-
setBody
<T> void setBody(Object body, Class<T> type)
Sets the body of the message as a specific type- Parameters:
body
- the bodytype
- the type of the body
-
copy
Message copy()
Creates a copy of this message so that it can be used and possibly modified further in another exchange. The returnedMessage
copy will have itsExchange
set to the sameExchange
instance as from the source.- Returns:
- a new message instance copied from this message
-
copyFrom
void copyFrom(Message message)
Copies the contents of the other message into this message If you need to do a copy and then set a new body, then usecopyFromWithNewBody(Message, Object)
method instead. The returnedMessage
copy will have itsExchange
set to the sameExchange
instance as from the source.- Parameters:
message
- the other message- See Also:
copyFromWithNewBody(Message, Object)
-
copyFromWithNewBody
void copyFromWithNewBody(Message message, Object newBody)
Copies the contents (except the body) of the other message into this message and uses the provided new body instead The returnedMessage
copy will have itsExchange
set to the sameExchange
instance as from the source.- Parameters:
message
- the other messagenewBody
- the new body to use
-
-