1

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

divideByZero
  • 1,120
  • 16
  • 27

4 Answers4

4

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

Vyacheslav Enis
  • 1,676
  • 12
  • 17
  • As far as I understand, "wireTap" doesn't interrupt route B execution. I needed something similar to "return from function" in programming. Correct me if I'm wrong – divideByZero Jan 12 '16 at 09:01
  • Correct, it doesn't interrupt. But in my example above it will run "direct:B-cont" async so B will finish and return value to the A (see my comment about preparing response) and B-cont will run in parallel after B will finish – Vyacheslav Enis Jan 12 '16 at 11:22
  • This is a helpful example, thank you. It would be a bit more informative if you could add the simulated output of the log statements generated for the two cases (since you presumably added those log statements to demonstrate the final result). Thank you again – Eric Kramer Sep 28 '16 at 13:55
  • I have added some log messages and example log outputs to describe wire-tap logic – Vyacheslav Enis Sep 28 '16 at 14:58
2

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.

Ramin Arabbagheri
  • 790
  • 2
  • 11
  • 25
1

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")

Alexey Yakunin
  • 1,743
  • 1
  • 19
  • 21
0

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.

Ivo
  • 450
  • 3
  • 18