105.5. 将 Spring PropertyPlaceholderConfigurer 与 File 组件一起使用
在 Camel 中,您可以直接使用 Simple 语言的 File Language,从而使基于内容的路由器更容易在 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 选项使用 File 语言设置动态文件名,请确保
使用替代语法(在 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\{ } 语法。
如果没有这样做,则有一个 clash,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'