2014-01-21 228 views
0

我用骆驼(2.11.0)至尝试,实现了以下功能:骆驼文件处理

  • 如果文件存在于某一位置,将它复制到另一个位置,然后开始处理它
  • 如果没有这样的文件存在,那么我不希望文件消费者/轮询阻止;我只想要继续处理direct:cleanup路线

我只希望文件被轮询一次!

这里是我到目前为止(使用Spring XML):

<camelContext id="my-camel-context" xmlns="http://camel.apache.org/schema/spring"> 
    <route id="my-route 
     <from uri="file:///home/myUser/myApp/fizz?include=buzz_.*txt"/> 

     <choice> 
      <when> 
       <!-- If the body is empty/NULL, then there was no file. Send to cleanup route. --> 
       <simple>${body} == null</simple> 
       <to uri="direct:cleanup" /> 
      </when> 

      <otherwise> 
       <!-- Otherwise we have a file. Copy it to the parent directory, and then continue processing. --> 
       <to uri="file:///home/myUser/myApp" /> 
      </otherwise> 
     </choice> 

     <!-- We should only get here if a file existed and we've already copied it to the parent directory. --> 
     <to uri="bean:shouldOnlyGetHereIfFileExists?method=doSomething" /> 
    </route> 

    <!-- 
     Other routes defined down here, including one with a "direct:cleanup" endpoint. 
    --> 
</camelContext> 

通过上述配置,如果在/home/myUser/myApp/fizz没有文件,那么骆驼只是等待/块,直到有一个。相反,我想让它放弃并转向direct:cleanup

如果有一个文件,我看到它在shouldOnlyGetHereIfFileExists bean中得到处理,但我没有看到它被复制到/home/myUser/myApp;所以就好像<otherwise>元素被完全忽略/忽略!

任何想法?提前致谢!

回答

1

试试这个设置和调整您的轮询间隔,以满足:

Camel File Component文档:

sendEmptyMessageWhenIdle

默认=假

骆驼2.9:如果轮询消费者做不轮询任何文件,您可以启用此选项来发送空的消息(无主体)。

关于写入文件,请在<otherwise>内部添加一条日志语句以确保它正在执行。如果是这样,检查文件/文件夹的权限等。

祝你好运。

+0

感谢@vikingsteve - 我给你一个+1,但我没有足够的代表。我感觉不好,有一件我忘记提及的重要部分:**我只想要路线轮询文件一次。**你对'sendEmptyMessageWhenIdle'的建议确实有效,但是路线不断轮询,一遍一遍地重现一个文件。关于如何配置Camel只能轮询一次的任何想法?再次感谢! – AdjustingForInflation

+0

那么还有一个'delay'选项 - 你可以尝试将它设置为0或-1,或者一些非常大的值,然后看看会发生什么。否则,在第一次执行路由期间,您可以使用'controlbus'来停止路由。 – vikingsteve

+0

看起来像延迟不能<= 0,但你可以尝试一个非常高的值。否则,抛出异常(你以某种方式处理)也可能会停止路由。 – vikingsteve

0

这是很老了,但如果任何人发现这一点,你可以查询只 一次