- Apache Camel Developer's Cookbook
- Scott Cranton Jakub Korab
- 396字
- 2025-02-24 23:14:00
Request-response route sending a one-way message
When processing a message in a Request-Response (InOut
) route, you sometimes need to send the message to an endpoint, but do not want to receive a response. This recipe shows you how to invoke such an endpoint using the InOnly
MEP.
The MEP used can radically alter the behavior of an endpoint. For example, if you invoke a JMS endpoint within a request-response (InOut
) route, it will send a message to a queue and set up a listener on a temporary destination waiting for a response; this is known as request-response over messaging. If the consumer of the message on the other side of the queue has not been written to send a response message, your route will wait indefinitely (or as long as the configurable timeout of the component).
This recipe shows how you can alter the MEP temporarily in order to send messages one way in a request-response 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 inOnly
DSL statement to explicitly route a message to that endpoint with that specific MEP:
In the XML DSL, this is written as:
<route>
<from uri="direct:start"/>
<inOnly uri="direct:oneWay"/>
<transform>
<constant>Done</constant>
</transform>
</route>
In the Java DSL, the same thing is expressed as:
from("direct:start")
.inOnly("direct:oneWay")
.transform().constant("Done");
How it works...
When the message reaches the stage in the route that sends to the InOnly
endpoint, the MEP associated with the exchange is temporarily changed to InOnly
for the call to the specified endpoint and restored on completion of that call.
There is no need to copy the message as this DSL statement is just like a normal to
statement sending the message to an endpoint; it just temporarily changes the MEP such that it does not wait for a response from that endpoint.
There's more...
The thread that processes the exchange through the main route is also used to process it through the producer endpoint (direct:oneWay
). This makes this pattern very different to a Wire Tap, where we hand over the processing of the message to another thread.
See also
- The Wire Tap – sending a copy of the message elsewhere recipe
- Wire Tap: http://camel.apache.org/wire-tap.html
- Multicast: http://camel.apache.org/multicast.html