问题
What is the best way to start a camel route optionally from another route? My use case is i'm sending out automated reports based on a table in a database. If the table is stale, the route to fetch fresh data should be started.
I have routes for generating and sending out the reports and i have a route for fetching a file from a remote server, saving it locally, reading it into a database and updating the database log (this tells the reporting routes if the data is fresh).
But how to tie them together?
// write to database
from(routeFrom)
.routeId(routeId)
.to(String.format(BEAN_INIT_DB_TABLE, routeId))
.to(String.format(BEAN_VALIDATE_TABLE_COLUMNS, routeId))
.to(String.format(BEAN_LOAD_CSV_FILE_TO_DB, routeId));
// fetch from database
from(reportFrom)
.routeId(reportRouteId)
.to(String.format(BEAN_CHECK_FILEINDB, reportRouteId)
.to(String.format(BEAN_LOAD_DB_TABLE_TO_XLSX_FILE, reportRouteId)
.to(BEAN_START_MAIL, reportRouteId);
In the above routeFrom, routeId, reportFrom and reportRouteId are set in a (yml) properties file, as wel as other route properties. The actual heavy work is done with java beans.
The BEAN_CHECK_FILEINDB will now throw an exception if no current data is available in the database. This could be used in a camel Predicate, then i could use camel's choice. But how can i start a camel route from a choice?
回答1:
I have not tried it, but using the controlbus feature mentioned by @claus-ibsen in the comments, it should be something like this:
.choice()
.when(<your no-data-available predicate>)
.to("controlbus:route?routeId=<yourRouteId>&action=suspend")
.otherwise()
.to(<continue to process db-contents>);
There are stop
, start
, suspend
and resume
available as actions. If you want to stop/start the route multiple times, suspend
and resume
sounds more suitable, but I don't know the exact difference.
Notice that .to(...)
only takes static endpoint addresses. If you want to insert an expression as route ID, you have to use .toD(...)
来源:https://stackoverflow.com/questions/49030844/how-to-start-camel-route-optionally-from-other-route