Poll file users in Apache

I recently started playing with Apache camel. So, as an experimental type, I thought I would create a simple project that would check the file directory every couple of seconds to see if I have a new file that picks up this file and then copies it to another folder. Later I want to put these files in a database.

So, by first supporting the basics, I created a route like:

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

This worked, and I thought ok lets add a timer. I initially made a mistake when using the timer and tried this:

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

I then had a strange exception due to the inability to write a file. Then I realized that by placing the file route after the timer route, he became a producer. So I did a little research, and here I am a little confused.

So, from my understanding, the file component uses a scheduled poll template. There is even the pollStrategy option in the file URL pattern. There is also a consumer EIP survey.

So this is where my confusion comes in.

1) If the file component uses the scheduled survey template, does it use / implement the EIP questionnaire?

2) How to add a simple schedule to the file component to use files every 30 seconds?

3) How to create your own pollingStrategy function by executing 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 tried to give a few examples around this, but I either do not look at the right places, or the plot is not enough completely. I guess this is not so much a code related issue as a more appropriate strategy / template for this approach.

Thanks Namphibian

+4
source share
2 answers

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

It allows you to associate policies with routes. We provide a number of policies out of the box. A policy can be any logic. For example, we offer a throttling policy that pauses / resumes flow-based routes on the fly.

Another field policy is a planned policy, so you can specify "hours of operation" for the route. Therefore, you can also use this. But consider it during opening hours, so you indicate the start and end times. There is a quartz-based cron policy http://camel.apache.org/cronscheduledroutepolicy.html , so you can configure this to start the route on Monday, and let it work for a while.

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

+6
source

1) If the file component uses the scheduled survey template, use / implement the EIP questionnaire?

Yes, the file maker implements ScheduledPollEndpoint . From doc :

Quite a few camel endpoints use the planned polling template to receive messages and push them through the camel's processing routes. That is, the endpoint seems to be using an Event-driven Consumer from outside the client, but the internally scheduled polling is used to monitor some state or resource, and then report fire exchanges. Since this is such a big picture, survey components can extend the base class ScheduledPollConsumer, which makes it easy to implement this template.

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

Using the delay parameter:

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

3) How to create your own pollingStrategy function by executing org.apache.camel.PollingConsumerPollStrategy?

Look at the source code: DefaultPollingConsumerPollStrategy or LimitedPollingConsumerPollStrategy .

You use custom pollStrategy as follows:

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

Where #myPoll i is defined in the registry for more information at the bottom of this .

+3
source

Source: https://habr.com/ru/post/1401966/


All Articles