问题
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
回答1:
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
回答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.
回答3:
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")
来源:https://stackoverflow.com/questions/34617646/return-back-from-current-route-in-apache-camel-and-continue-routing