camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <>
Subject Re: Periodic route activation use case
Date Sun, 26 Aug 2012 07:14:08 GMT
On Sat, Aug 25, 2012 at 7:13 PM, Ashwin Pejavar
<> wrote:
> I have a use-case where I need to consume files at a fixed cron schedule.
> For example, I need my input route to resume everyday at 6AM, process all
> files collected in the input directory and go into suspension till 6AM the
> following day.
> I was hoping the CronScheduledRoutePolicy would be useful, but that doesn't
> quite do what I need. I can use it to start the route at 6AM, but there
> seems to be no way of automatically suspending it when all the files are
> processed.
> Is there a way of doing this in Camel?

See this FAQ

You could possible either extend the cron scheduled route policy, and
add logic in the onExchangeDone and figure out if its the last
exchange from the file batch. And then if so stop/suspend the
consumer. The file consumer is cheap to suspend as its an internal
flag that indicate its suspended (AFAIR). And therefore you most
likely wont need to use a separate thread as the FAQ tells to do.

The file consumer will set a property on the exchange when there is no
more files (eg the batch is complete)
 exchange.setProperty(Exchange.BATCH_COMPLETE, true);

So you can check for that property, and know when you are done, and
can suspend the consumer.
Something a like

onExchangeDone(Route route, Exchange exchange) {
  // check for batch complete
  if (...) {
      // stop will favor suspend over stopping, so its fine to invoke
this method

And on the file component you would need to enable the
sendEmptyMessageWhenIdle=true option to send an empty message if there
is no files. So the routing will kick in. You would need to cater for
an empty message in your route logic though.

A total different approach would be to use a custom
PollingConsumerPollStrategy on the file endpoint.

It has callbacks when the polling starts / commits. And the number of
polled messages (eg number of files found).
You can then use that to implement logic to suspend the consumer in
the commit method.

That may be an easier task to try out at first. Extending the
DefaultPollingConsumerPollStrategy and override the commit method.

> --
> View this message in context:
> Sent from the Camel - Users mailing list archive at

Claus Ibsen
Twitter: davsclaus, fusenews
Author of Camel in Action:

View raw message