One-way route waiting on a request-response endpoint

Assume that you have a route that uses the InOnly Message Exchange Pattern (MEP). The consumer that fed a message into the route expects no response. Any endpoint listed in the route will, as a consequence, be invoked with the InOnly MEP.

This recipe shows how you can alter the MEP temporarily to InOut in order to request a response from an endpoint used in a one-way route.

Getting ready

The Java code for this recipe is located in the org.camelcookbook.routing.changingmep package. The Spring XML files are located under src/main/resources/META-INF/spring and prefixed with changingMep.

How to do it...

Use the inOut DSL statement to explicitly route a message to that endpoint with that specific MEP.

In the XML DSL, this logic is written as follows:

<route>
  <from uri="direct:in"/>
  <inOut uri="direct:modifyMessage"/>
  <to uri="mock:afterMessageModified"/>
</route>

Tip

The Endpoint invocation could also be written as:

<to uri="direct:modifyMessage" pattern="InOut"/>

In the Java DSL, the same thing is expressed as:

from("direct:start")
  .inOut("direct:modifyMessage")
  .to("mock:afterMessageModified");

Tip

The Endpoint invocation could also be written as:

.to(ExchangePattern.InOut, "direct:modifyMessage")

How it works...

When the message is passed to the modifyMessage endpoint in the example, the MEP on the exchange is temporarily changed from InOnly to InOut. The same thread that processed the message down the main route will process the message through the InOut endpoint. The message continues to take part in any transactions defined in the main route.

When a response is returned, and the message resumes its flow down the main route, the MEP will be restored to that of the original exchange's MEP, as in the preceding example, InOnly.

If you want to permanently switch the MEP to InOut for the remainder of the route use the setExchangePattern DSL statement. This will not affect the behavior of the route's consumer (from) endpoint.

In the XML DSL, this is written as:

<setExchangePattern pattern="InOut"/>

In the Java DSL, the same thing is expressed as:

.setExchangePattern(ExchangePattern.InOut)

There's more...

This recipe is frequently seen in integrations that use JMS messaging as a source. Here, a route consumes messages from a JMS queue, and invokes another queue with the InOut pattern; the intent being to perform a request-response operation over messaging over the second queue.

  from("jms:inbound")
   .inOut("jms:serviceRequest")
   .log("Service responded with: ${body}");

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141