divideByZero
divideByZero

Reputation: 1188

return back from current route in Apache Camel and continue routing

Say we have 2 routes A and B. A route at some point calls route B. I'd like on certain condition in B route return back to A and continue A routing only, so stop() is unsuitable in my case. I'd like to keep my routes as is without serious refactoring

Upvotes: 2

Views: 4533

Answers (4)

Ivo
Ivo

Reputation: 450

Indeed stop() does fully stopping routing as opposed to a return or a break from the current route. Haven't tested it, but how about filter(false) in your route B? That should make the exchange resume on route A.

Upvotes: 0

Vyacheslav Enis
Vyacheslav Enis

Reputation: 1661

You can implement this using wiretap. In that case your logic must be changed:

  • Route B splitted to B-common and B-cont where B-cont contains logic that must be processed after returning result to A
  • Logic must be changed from "on certain condition return back to A and continue processing B" to "on inverted certain condition wiretap to B-cont"

Details about wiretap are here: http://camel.apache.org/wire-tap.html

Example of configuration:

<route>
    <from uri="direct:A"/>
    <log message="B is starting"/>
    <to uri="direct:B"/>
    <log message="B is finished"/>
</route>
<route>
    <from uri="direct:B"/>
    <!-- Common B logic here -->
    <log message="Common B logic finished"/>
    <choice>
        <when>
            <!-- your condition inverted -->
            <wireTap uri="direct:B-cont"/>
        </when>
    </choice>
    <!-- Remaining B logic if needed to prepare required response for A -->
    <log message="Response to A prepared"/>
</route>
<route>
    <from uri="direct:B-cont"/>
    <log message="B processing after returning response to A"/>
    <!-- remaining B logic -->
</route>

Log output for such routes will looks like:

B is starting
Common B logic finished
Response to A prepared
B is finished
B processing after returning response to A

or:

B is starting
Common B logic finished
Response to A prepared
B processing after returning response to A
B is finished

or:

B is starting
Common B logic finished
B processing after returning response to A
Response to A prepared
B is finished

As you can see "wire-tapped" route will run in parallel (for most cases) to the rest of the route from where it was called. Details can be found in wire-tap documentation http://camel.apache.org/wire-tap.html

Upvotes: 4

Ramin Arabbagheri
Ramin Arabbagheri

Reputation: 820

I presume you are using direct to link to route B. Can you simply set a header in your route B (when the condition is satisfied) and when it gets back to route A, Filter the exchanges that don't have this header set? Let me know if this is unclear and I will provide you with an example.

R.

Upvotes: 2

Alexey Yakunin
Alexey Yakunin

Reputation: 1771

Make you route A from several routes connected by direct component. In route B, on certain condition call the desired part of a route A, using to("direct:partRouteA")

Upvotes: 1

Related Questions