Java 在 Apache 中轮询文件使用者
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9748535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Polling File Consumer In Apache
提问by Namphibian
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.
我最近开始玩 Apache 骆驼。所以作为实验类型,我想我会创建一个简单的项目,每隔几秒钟扫描一个文件目录,看看我是否有一个新文件,选择该文件,然后将其复制到另一个文件夹。稍后我想将这些文件放入数据库中。
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.
所以根据我的理解,文件组件使用预定的轮询模式。文件路由 url 模式中甚至还有一个 pollStrategy 选项。还有一个轮询消费者 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?
1) 如果文件组件使用计划轮询模式,它是否使用/实现了轮询消费者 EIP?
2) How do I add a simple schedule to the file component to consume files say every 30 seconds?
2)如何向文件组件添加一个简单的计划以每 30 秒使用一次文件?
3) How do I create my own pollingStrategy by implementing org.apache.camel.PollingConsumerPollStrategy?
3) 如何通过实现 org.apache.camel.PollingConsumerPollStrategy 创建我自己的 pollingStrategy?
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
谢谢南菲比安
采纳答案by Claus Ibsen
Camel offers a feature called routepolicy http://camel.apache.org/routepolicy.html
Camel 提供了一个名为 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.htmlso you can configure this to start the route on monday, and let it run for a little while.
另一个现成的策略是计划策略,因此您可以为路线指定“开放时间”。所以你也可以使用它。但请注意开放时间,因此您可以指定开始/结束时间。有一个基于quartz cron 的策略http://camel.apache.org/croscheduledroutepolicy.html,因此您可以将其配置为在星期一启动路由,并让它运行一段时间。
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
如果您需要从路线内停止路线,那就有点复杂了,这里有一个常见问题解答:http: //camel.apache.org/how-can-i-stop-a-route-from -a-route.html
回答by ebaxt
1) If the file component uses the scheduled poll pattern does it use/implement the polling conumser EIP?
1) 如果文件组件使用计划轮询模式,它是否使用/实现了轮询消费者 EIP?
Yes, the file producer implements the ScheduledPollEndpoint. From the doc:
是的,文件生产者实现了ScheduledPollEndpoint。从文档:
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?
相当多的入站 Camel 端点使用计划的轮询模式来接收消息并通过 Camel 处理路由推送它们。也就是说,从客户端外部,端点似乎使用事件驱动消费者,但在内部使用预定轮询来监视某种状态或资源,然后触发消息交换。由于这是一个如此常见的模式,轮询组件可以扩展 ScheduledPollConsumer 基类,这使得实现此模式更简单。
2)如何向文件组件添加一个简单的计划以每 30 秒消耗一次文件 sa?
Using the delay
option:
使用delay
选项:
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?
3) 如何通过实现 org.apache.camel.PollingConsumerPollStrategy 创建我自己的 pollingStrategy?
Have a look at the source code: DefaultPollingConsumerPollStrategyor LimitedPollingConsumerPollStrategy.
查看源代码:DefaultPollingConsumerPollStrategy或LimitedPollingConsumerPollStrategy。
You use a custom pollStrategy like this:
您可以使用这样的自定义 pollStrategy:
from("file://inbox/?pollStrategy=#myPoll").to(...)
Where #myPoll i defined in the registrymore info at the bottom of thispage.