Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

When we are processing messages and using Java code for that purpose (ie. via bean class or processor class), it is often quite necessary to create a fresh exchange instance. Thus, if you need to create an Exchange object, the easiest approach is to invoke the methods of the ExchangeBuilder class.

ExchangeBuilder class

The fully qualified name of the ExchangeBuilder class is as follows:

org.apache.camel.builder.ExchangeBuilder

The ExchangeBuilder class will expose the static method of anExchange, which we can utilize to start building an exchange object.

For example, the following code snippet will create a new Exchange object containing the message body string, Hello World!, and with headers containing username and password credentials:

import org.apache.camel.Exchange;
import org.apache.camel.builder.ExchangeBuilder;
...
Exchange exch = ExchangeBuilder.anExchange(myCamelCtx)
                    .withBody("Hello World!")
                    .withHeader("username", "janedoe")
                    .withHeader("password", "pa$$w0rd")
                    .build();

ExchangeBuilder methods

The ExchangeBuilder class supports the following methods:

Method Signature

Description

ExchangeBuilder anExchange(CamelContext context)

A static method to Initiate building an exchange object.

Exchange build()

Build the Exchange

ExchangeBuilder withBody(Object body)

Set a header on the exchange, sets a header on the exchange’s In message body.

ExchangeBuilder withHeader(String key, Object value)

Set a header on the exchange, sets a header on the exchange’s In message.

ExchangeBuilder withPattern(ExchangePattern pattern)

Sets the exchange pattern on the exchange.

ExchangeBuilder withProperty(String key, Object value)

Sets a property on the exchange.

ExchangeBuilder anExchange(CamelContext context)(static method) Initiate building an exchange object

Exchange build()Build the exchange.ExchangeBuilder withBody(Object body)Set the message body on the exchange (that is, sets the exchange’s In message body).ExchangeBuilder withHeader(String key, Object value)Set a header on the exchange (that is, sets a header on the exchange’s In message).ExchangeBuilder withPattern(ExchangePattern pattern)Sets the exchange pattern on the exchange.ExchangeBuilder withProperty(String key, Object value)Sets a property on the exchange.

  • No labels