官术网_书友最值得收藏!

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

主站蜘蛛池模板: 从江县| 柞水县| 萨迦县| 海口市| 隆昌县| 广昌县| 万载县| 太仆寺旗| 瓦房店市| 高阳县| 新和县| 库车县| 保山市| 大连市| 达拉特旗| 化隆| 老河口市| 昌图县| 威信县| 皋兰县| 拉萨市| 高尔夫| 恩施市| 青岛市| 长春市| 龙口市| 西华县| 建湖县| 丰台区| 涞源县| 襄樊市| 东阳市| 白山市| 绥滨县| 万山特区| 沂水县| 平乐县| 静海县| 宁都县| 宣威市| 镇巴县|