【发布时间】:2023-10-30 06:01:01
【问题描述】:
我试图找到一种方法来使用 spring bean 来配置 Camel 端点,该 spring bean 是从骆驼上下文中路由中的端点声明中引用的,但它不起作用。
例如,有时使用许多参数定义端点 URI 是非常可怕的(!!),使用 bean 及其属性配置端点会容易得多。 (或者更好的是,当在 XML 中配置端点时,或元素应该有子元素,比如我们可以配置端点参数的常规 bean。
下面的第一种方法效果很好,非常标准且非常简单。第二种方法是我想改用的方法,但它不起作用。我尝试了很多变化,但没有成功!下面的第三种选择实际上对 Camel 开发人员来说只是一个有趣的建议,但它也说明了我的观点。
在下面的示例中,我只为文件端点配置了 3 个参数,但想象一下 URI 有 10 个参数!我的问题是如何使我的第二种方法正常工作,我确定有一个简单的解决方案!?我也尝试过使用工厂 bean 和工厂方法,但也没有用。
1) 在 XML (spring beans) 中配置骆驼端点的标准方法:
...
<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) 我希望有效但不起作用的替代方案(对我来说!):
<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>
【问题讨论】:
标签: spring uri apache-camel javabeans endpoint