101.5. Spring PropertyPlaceholderConfigurer と File コンポーネントの使用
Camel では、Simple 言語から直接 File 言語を使用することができます。これにより、Spring XML で Content Based Router を簡単に実行できます。ここでは、以下のようにファイルエクステンションに基づいてルーティングすることができます。
<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\{ } 構文を使用することに注意してください。
これを行わないと、競合が発生し、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'