我试图找到一种方法来配置一个骆驼端点,它使用一个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&move=../done&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>
是的,这是行得通的,但是此刻您添加了一些属性来设置参数,它们并不总是以正确的方式工作,或者如同它们被指定为URI参数一样。例如,如果我使用'move'属性和一个SimpleExpression类型的bean作为值(而不是字符串)来指定'move'参数,那么一切都会启动,但输入文件夹中的文件不能移动到'移动'文件夹,因为它期待一个文件而不是一个文件夹(显然,根据异常消息)。 – The4Summers 2014-10-03 15:55:13
@ The4Summers你是对的,它不以同样的方式工作。我添加了一个'moveExpression'示例来展示如何正确处理这个问题。 – 2014-10-03 18:33:08
是的,我可以使它工作,但这种方法终究比简单地使用URI更加复杂和繁重!有一种方法来配置端点会更有趣,就像我在上面第三种替代方法中提出的那样。也许如果我有时间,我会尝试创建我自己的“from”和“to”标签,它们将通过从params元素构造完整的URI来包装现有的标签...!我也可以向骆驼开发者提出这个建议。谢谢。 – The4Summers 2014-10-03 18:40:40