Namphibian
Namphibian

Reputation: 12221

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

Upvotes: 4

Views: 15256

Answers (2)

Claus Ibsen
Claus Ibsen

Reputation: 55525

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

Upvotes: 8

ebaxt
ebaxt

Reputation: 8417

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

Yes, the file producer implements the ScheduledPollEndpoint. From the doc:

Quite a few inbound Camel endpoints use a scheduled poll pattern to receive messages and push them through the Camel processing routes. That is to say externally from the client the endpoint appears to use an Event Driven Consumer but internally a scheduled poll is used to monitor some kind of state or resource and then fire message exchanges. Since this a such a common pattern, polling components can extend the ScheduledPollConsumer base class which makes it simpler to implement this pattern.

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

Using the delay option:

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

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

Have a look at the source code: DefaultPollingConsumerPollStrategy or LimitedPollingConsumerPollStrategy.

You use a custom pollStrategy like this:

from("file://inbox/?pollStrategy=#myPoll").to(...)

Where #myPoll i defined in the registry more info at the bottom of this page.

Upvotes: 4

Related Questions