Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polling File Consumer In Apache

I have started playing around with Apache camel recently. So being the experimental type I thought I would create a simple project that would scan a file directory every couple of seconds to see if I have a new file pick that file up and then copy it to another folder. Later on I want to place these files into a database.

So keeping with the basics first I created a route such as the following:

 from("file://c:/CTest/inbox?noop=true")
                .to("file://C:/Ctest/outbox");

This worked and I thought ok lets add a timer. Initially I made the mistake of using a timer and trying this:

from("timer://myTimer?period=50").to("file://c:/CTest/inbox?noop=true")
                .to("file://C:/Ctest/outbox");

I then had some strange exception about not being able to write a file. I then realised by placing the file route after the timer route it became a producer. So I researched a bit and here is where I am little confused.

So from my understanding the file component uses the scheduled poll pattern. There is even a pollStrategy option in the file route url pattern. There is also a Polling Consumer EIP.

So here is where my confusion sets in.

1) If the file component uses the scheduled poll pattern does it use/implement the polling conumser EIP?

2) How do I add a simple schedule to the file component to consume files say every 30 seconds?

3) How do I create my own pollingStrategy by implementing org.apache.camel.PollingConsumerPollStrategy?

I suspect I can do something like this:

    from("file://c:/CTest/inbox?noop=true&pollStrategy=some-expression")
                .to("file://C:/Ctest/outbox");

I have tried to get some examples around this but I am either not looking at the right places or missing the plot completely. I guess this is not so much a code related question but more of a what is the best strategy/pattern for this approach.

Thanks Namphibian

like image 563
Namphibian Avatar asked Dec 28 '22 04:12

Namphibian


1 Answers

Camel offers a feature called routepolicy http://camel.apache.org/routepolicy.html

It allows you to associate policies to routes. We provide a number of policies out of the box. A policy can be any kind of logic. For example we offer a throttling policy, which on-the-fly suspend/resume routes based on threadsholds.

Another out the box policy is a scheduled policy, so you can specify 'opening hours' for a route. So you can use that as well. But mind its for opening hours, so you specify a start/end time. There is a quartz cron based policy http://camel.apache.org/cronscheduledroutepolicy.html so you can configure this to start the route on monday, and let it run for a little while.

If you need to stop a route from within a route, then thats a bit more complicated, there is a FAQ about this here: http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html

like image 79
Claus Ibsen Avatar answered Jan 05 '23 17:01

Claus Ibsen