105.5. 将 Spring PropertyPlaceholderConfigurer 与 File 组件一起使用
在 Camel 中,您可以直接从 简单 语言使用文件 语言,在 Spring XML 中更轻松地使用基于内容的路由路由器,其中我们可以根据文件扩展进行路由,如下所示:
<from uri="file://input/orders"/>
<choice>
<when>
<simple>${file:ext} == 'txt'</simple>
<to uri="bean:orderService?method=handleTextFiles"/>
</when>
<when>
<simple>${file:ext} == 'xml'</simple>
<to uri="bean:orderService?method=handleXmlFiles"/>
</when>
<otherwise>
<to uri="bean:orderService?method=handleOtherFiles"/>
</otherwise>
</choice>
如果您在 File 端点中使用 fileName 选项使用文件语言设置动态文件名,请确保
使用替代语法(来自 Camel 2.5 的提供),以避免使用 Springs PropertyPlaceholderConfigurer 冲突。???
bundle-context.xml
<bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:bundle-context.cfg" />
</bean>
<bean id="sampleRoute" class="SampleRoute">
<property name="fromEndpoint" value="${fromEndpoint}" />
<property name="toEndpoint" value="${toEndpoint}" />
</bean>
bundle-context.cfg
fromEndpoint=activemq:queue:test
toEndpoint=file://fileRoute/out?fileName=test-$simple{date:now:yyyyMMdd}.txt
请注意,我们如何在上面的 toEndpoint 中使用 $simple\{ } 语法。
如果没有这样做,Spring 会抛出异常,如下所示
org.springframework.beans.factory.BeanDefinitionStoreException:
Invalid bean definition with name 'sampleRoute' defined in class path resource [bundle-context.xml]:
Could not resolve placeholder 'date:now:yyyyMMdd'