Package org.apache.camel
Interface Message
public interface Message
Implements the Message pattern and represents an inbound or
outbound message as part of an
Exchange
.
Headers are represented in Camel using a CaseInsensitiveMap
. The
implementation of the map can be configured by the HeadersMapFactory
which can be set on the
CamelContext
. The default implementation uses the CaseInsensitiveMap
.-
Method Summary
Modifier and TypeMethodDescriptioncopy()
Creates a copy of this message so that it can be used and possibly modified further in another exchange.void
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 insteadgetBody()
Returns the body of the message as a POJO<T> T
Returns the body as the specified typeReturns the exchange this message is related toAccesses a specific header<T> T
Returns a header associated with this message by name and specifying the type requiredAccesses a specific header<T> T
Returns a header associated with this message by name and specifying the type requiredAccesses a specific header<T> T
Returns a header associated with this message by name and specifying the type requiredReturns all the headers associated with the message.Returns the body of the message as a POJO<T> T
getMandatoryBody
(Class<T> type) Returns the mandatory body as the specified typeReturns the id of the message.long
Returns the timestamp that this messages originates from.getPayloadForTrait
(MessageTrait trait) Gets the payload for theMessageTrait
boolean
Returns whether any headers have been set.boolean
Whether the message has any message ID assigned.boolean
hasTrait
(MessageTrait trait) Checks whether the message has a givenMessageTrait
Returns a new instance of this type.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
removeTrait
(MessageTrait trait) Removes the traitvoid
reset()
Clears the message from user data, so the message can be reused.void
Sets the body of the message<T> void
Sets the body of the message as a specific typevoid
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 messagevoid
setPayloadForTrait
(MessageTrait trait, Object object) Sets the payload for theMessageTrait
-
Method Details
-
newInstance
Message newInstance()Returns a new instance of this type. -
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:
-
setMessageId
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
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
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
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 the value generated by the defaultValueSupplier if there is no header for the given name
-
getHeader
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
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
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 the 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
Sets a header on the message- Parameters:
name
- of the headervalue
- to associate with the name
-
removeHeader
Removes the named header from this message- Parameters:
name
- name of the header- Returns:
- the old value of the header
-
removeHeaders
Removes the headers from this message- Parameters:
pattern
- pattern of names- Returns:
- boolean whether any headers matched
-
removeHeaders
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
Returns all the headers associated with the message. Headers are 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
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 any headers have 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 body has not been 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
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
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. The helper methodinvalid reference
org.apache.camel.support.ExchangeHelper#getBodyAndResetStreamCache(Exchange, Class)
- Parameters:
type
- the type that the body- Returns:
- the body of the message as the specified type, or null if body does not exist
- Throws:
TypeConversionException
- is thrown if error during type conversion- See Also:
-
getMandatoryBody
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. The helper methodinvalid reference
org.apache.camel.support.ExchangeHelper#getBodyAndResetStreamCache(Exchange, Class)
- 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- See Also:
-
setBody
Sets the body of the message- Parameters:
body
- the body
-
setBody
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
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
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
-
hasTrait
Checks whether the message has a givenMessageTrait
- Parameters:
trait
- theMessageTrait
to check- Returns:
- true if the message instance has the trait or false otherwise
-
getPayloadForTrait
Gets the payload for theMessageTrait
- Parameters:
trait
- theMessageTrait
to obtain the payload- Returns:
- The trait payload or null if not available
-
setPayloadForTrait
Sets the payload for theMessageTrait
- Parameters:
trait
- theMessageTrait
to set the payloadobject
- the payload
-
removeTrait
Removes the trait
-