104.5. Spring PropertyPlaceholderConfigurer を File コンポーネントと一緒に使用する

Camel では、Simple 言語から ファイル言語 を直接使用できます。これにより、以下に示すように、ファイル拡張子に基づいてルーティングできる 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'