underlying message implementation parses the headers and populates the header map. The message
body is not parsed until you reach the end of the route, at the to("TargetURL") call. At that point, the
body is converted into the format required for writing it to the target endpoint, TargetURL.
By waiting until the last possible moment before populating the bodies, headers, and attachments, you
can ensure that unnecessary type conversions are avoided. In some cases, you can completely avoid
parsing. For example, if a route contains no explicit references to message headers, a message could
traverse the route without ever parsing the headers.
Whether or not lazy creation is implemented in practice depends on the underlying component
implementation. In general, lazy creation is valuable for those cases where creating a message body, a
message header, or a message attachment is expensive. For details about implementing a message type
that supports lazy creation, see Section 44.2, “Implementing the Message Interface” .
Lazy creation of message IDs
Apache Camel supports lazy creation of message IDs. That is, a message ID is generated only when you
actually call the getMessageId() method. The DefaultExchange.getExchangeId() implementation of
this method delegates ID generation to the UUID generator that is registered with the CamelContext.
Some endpoint implementations would call the getMessageId() method implicitly, if the endpoint
implements a protocol that requires a unique message ID. In particular, JMS messages normally include a
header containing unique message ID, so the JMS component automatically calls getMessageId() to
obtain the message ID (this is controlled by the messageIdEnabled option on the JMS endpoint).
For details of how to register UUID generators with the CamelContext, see Section 34.4, “Built-In UUID
Generators”.
Initial message format
The initial format of an In message is determined by the source endpoint, and the initial format of an
Out message is determined by the target endpoint. If lazy creation is supported by the underlying
component, the message remains unparsed until it is accessed explicitly by the application. Most Apache
Camel components create the message body in a relatively raw form — for example, representing it using
types such as byte[], ByteBuffer, InputStream, or OutputStream. This ensures that the overhead
required for creating the initial message is minimal. Where more elaborate message formats are required
components usually rely on type converters or marshalling processors.
Type converters
It does not matter what the initial format of the message is, because you can easily convert a message
from one format to another using the built-in type converters (see Section 34.3, “Built-In Type
Converters”). There are various methods in the Apache Camel API that expose type conversion
functionality. For example, the convertBodyTo(Class type) method can be inserted into a route to
convert the body of an In message, as follows:
from("SourceURL").convertBodyTo(String.class).to("TargetURL");
Where the body of the In message is converted to a java.lang.String. The following example shows how
to append a string to the end of the In message body:
from("SourceURL").setBody(bodyAs(String.class).append("My Special Signature")).to("TargetURL");
Where the message body is converted to a string format before appending a string to the end. It is not