2014-10-02 66 views
1

我试图找到一种方法来配置一个骆驼端点,它使用一个spring bean,它在骆驼上下文中从路由中的端点声明引用,但它不起作用。替代长URI来配置Spring Bean的骆驼端点?

例如,有时使用多个参数定义端点URI非常可怕(!!),那么使用bean及其属性配置端点会更容易。 (或者甚至更好的是,当用XML配置端点时,或者元素应该有像常规bean那样的子元素,我们可以在其中配置端点的参数)。

下面的第一种方法运作良好,非常标准,非常简单。第二种方法是我想要使用的方法,但它不起作用。我尝试了许多变化,但没有成功!事实上,下面的第三种选择对骆驼开发者来说只是一个有趣的提议,但它也说明了我的观点。

在我下面的示例中,我只为文件端点配置了3个参数,但想象一下带有10个参数的URI!我的问题是如何让我的第二种方法正常工作,我确定有一个简单的解决方案!?我也试过使用一个工厂bean和一个工厂方法,但它没有工作。

1)配置XML骆驼端点(春豆)标准方式:

... 
<camel:camelContext id="camelContext" > 

    <camel:route id="deviceDataLogsPoller" > 
     <camel:from uri="file://long/path/to/input?preMove=../inprogress&amp;move=../done&amp;moveFailed=../error" /> 

     <camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" /> 

    </camel:route> 
</camel:camelContext> 

2)的替代方案,我预计会的Valide但不工作(对我来说):

<bean id="filePoller" class="org.apache.camel.component.file.FileEndpoint" > 

    <property name="camelContext" ref="camelContext" /> 
    <property name="localWorkDirectory" value="/long/path/to/input" /> 
    <property name="preMove" value="../inprogress" /> 
    <property name="move"  value="../done" /> 
    <property name="moveFailed" value="../error" /> 
    ... 
</bean> 

... 
<camel:camelContext id="camelContext" > 

    <camel:route id="deviceDataLogsPoller" > 
     <camel:from ref="filePoller" /> 

     <camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" /> 

    </camel:route> 
</camel:camelContext> 

3)替代,这将是在未来的有趣上述两个备选方案)之间(混合:

...

<camel:route id="deviceDataLogsPoller" > 
     <camel:from uri="file://long/path/to/input" > 
      <property name="preMove" value="../inprogress" /> 
      <property name="move"  value="../done" /> 
      <property name="moveFailed" value="../error" /> 
      ... 
     </camel:from> 

     <camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" /> 

    </camel:route> 
</camel:camelContext> 

回答

1

究竟有什么不适合你?

下面的设置做了工作按预期:

<bean id="filePoller" class="org.apache.camel.component.file.FileEndpoint"> 
    <property name="file" value="src/data/bean-ref" /> 
    <property name="move" ref="moveExpression"/> 
</bean> 

<bean id="moveExpression" class="org.apache.camel.model.language.SimpleExpression"> 
    <constructor-arg value="${file:parent}/.done/${file:onlyname}" /> 
</bean> 

<camelContext xmlns="http://camel.apache.org/schema/spring" id="camelContext"> 
    <route> 
     <from ref="filePoller" /> 
     <log message="${body}" /> 
    </route> 
</camelContext> 

注:

  • 酒店file是强制性
  • 性能movemoveFailedpreMovejava.lang.String类型但键入org.apache.camel.Expression并且必须相应地进行初始化。
  • 属性moveExpression需要一个完整的文件表达式。如果仅使用.done而不是${file:parent}/.done/${file:onlyname},则该文件将重命名为.done,而不会移至名为.done的目录。
+0

是的,这是行得通的,但是此刻您添加了一些属性来设置参数,它们并不总是以正确的方式工作,或者如同它们被指定为URI参数一样。例如,如果我使用'move'属性和一个SimpleExpression类型的bean作为值(而不是字符串)来指定'move'参数,那么一切都会启动,但输入文件夹中的文件不能移动到'移动'文件夹,因为它期待一个文件而不是一个文件夹(显然,根据异常消息)。 – The4Summers 2014-10-03 15:55:13

+0

@ The4Summers你是对的,它不以同样的方式工作。我添加了一个'moveExpression'示例来展示如何正确处理这个问题。 – 2014-10-03 18:33:08

+0

是的,我可以使它工作,但这种方法终究比简单地使用URI更加复杂和繁重!有一种方法来配置端点会更有趣,就像我在上面第三种替代方法中提出的那样。也许如果我有时间,我会尝试创建我自己的“from”和“to”标签,它们将通过从params元素构造完整的URI来包装现有的标签...!我也可以向骆驼开发者提出这个建议。谢谢。 – The4Summers 2014-10-03 18:40:40

0

正如我上一条评论所述,我能够为端点工作制作bean配置(请参阅上面的注释),但这种方法终究比简单地使用URI更加复杂和繁重!

有一种配置端点的方式会更有趣,就像我在上面第三种替代方法中提出的那样。也许如果我有时间的话,我会尝试创建我自己的和标签,通过从params元素构造完整的URI来包装现有的标签...!我也可以向骆驼开发者提出这个建议。

请参见下面的怎么可能是有趣的,在未来配置的端点的例子(或与XML包装我想代码):

<camel:route id="deviceDataLogsPoller"> 

    <camel:from uri="file://long/path/to/input" > 

     <param name="preMove" value="../inprogress" /> 
     <param name="move"  value="../done" /> 
     <param name="moveFailed" value="../error" /> 
     ... 
    </camel:from> 

    ... 

</camel:route> 

不幸的是,如上所示的端点配置暂时不可能,但我认为这将是一件好事!目前,唯一的方法是将所有参数指定为非常长的URI中的参数,或者将端点配置为常规bean,并且具有它所隐含的所有复杂性(请参阅上面的注释以获取详细信息)。