Chapter 44. Message Interface
Abstract
This chapter describes how to implement the Message interface, which is an optional step in the implementation of a Apache Camel component.
44.1. The Message Interface Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
An instance of org.apache.camel.Message
type can represent any kind of message (In or Out). Figure 44.1, “Message Inheritance Hierarchy” shows the inheritance hierarchy for the message type. You do not always need to implement a custom message type for a component. In many cases, the default implementation, DefaultMessage
, is adequate.
Figure 44.1. Message Inheritance Hierarchy
The Message interface Copy linkLink copied to clipboard!
Example 44.1, “Message Interface” shows the definition of the org.apache.camel.Message
interface.
Example 44.1. Message Interface
Message methods Copy linkLink copied to clipboard!
The Message interface defines the following methods:
-
setMessageId()
,getMessageId()
— Getter and setter methods for the message ID. Whether or not you need to use a message ID in your custom component is an implementation detail. -
getExchange()
— Returns a reference to the parent exchange object. -
isFault()
,setFault()
— Getter and setter methods for the fault flag, which indicates whether or not this message is a fault message. -
getHeader()
,getHeaders()
,setHeader()
,setHeaders()
,removeHeader()
,hasHeaders()
— Getter and setter methods for the message headers. In general, these message headers can be used either to store actual header data, or to store miscellaneous metadata. -
getBody()
,getMandatoryBody()
,setBody()
— Getter and setter methods for the message body. The getMandatoryBody() accessor guarantees that the returned body is non-null, otherwise theInvalidPayloadException
exception is thrown. -
getAttachment()
,getAttachments()
,getAttachmentNames()
,removeAttachment()
,addAttachment()
,setAttachments()
,hasAttachments()
— Methods to get, set, add, and remove attachments. -
copy()
— Creates a new, identical (including the message ID) copy of the current custom message object. -
copyFrom()
— Copies the complete contents (including the message ID) of the specified generic message object,message
, into the current message instance. Because this method must be able to copy from any message type, it copies the generic message properties, but not the custom properties. -
createExchangeId()
— Returns the unique ID for this exchange, if the message implementation is capable of providing an ID; otherwise, returnnull
.
44.2. Implementing the Message Interface Copy linkLink copied to clipboard!
How to implement a custom message Copy linkLink copied to clipboard!
Example 44.2, “Custom Message Implementation” outlines how to implement a message by extending the DefaultMessage
class.
Example 44.2. Custom Message Implementation
- 1
- Implements a custom message class, CustomMessage, by extending the
org.apache.camel.impl.DefaultMessage
class. - 2
- Typically, you need a default constructor that creates a message with default properties.
- 3
- Override the
toString()
method to customize message stringification. - 4
- The
newInstance()
method is called from inside theMessageSupport.copy()
method. Customization of thenewInstance()
method should focus on copying all of the custom properties of the current message instance into the new message instance. TheMessageSupport.copy()
method copies the generic message properties by callingcopyFrom()
. - 5
- The
createBody()
method works in conjunction with theMessageSupport.getBody()
method to implement lazy access to the message body. By default, the message body isnull
. It is only when the application code tries to access the body (by callinggetBody()
), that the body should be created. TheMessageSupport.getBody()
automatically callscreateBody()
, when the message body is accessed for the first time. - 6
- The
populateInitialHeaders()
method works in conjunction with the header getter and setter methods to implement lazy access to the message headers. This method parses the message to extract any message headers and inserts them into the hash map,map
. ThepopulateInitialHeaders()
method is automatically called when a user attempts to access a header (or headers) for the first time (by callinggetHeader()
,getHeaders()
,setHeader()
, orsetHeaders()
). - 7
- The
populateInitialAttachments()
method works in conjunction with the attachment getter and setter methods to implement lazy access to the attachments. This method extracts the message attachments and inserts them into the hash map,map
. ThepopulateInitialAttachments()
method is automatically called when a user attempts to access an attachment (or attachments) for the first time by callinggetAttachment()
,getAttachments()
,getAttachmentNames()
, oraddAttachment()
.