Google

Jun 13, 2013

Apache Camel and Spring with properties file to move files

This is an extension to the camel tutorials


This highlights  the use of a properties file to configure polling folder, destination folder, file pattern to look for, done file name, etc.

Step 1: The applicationContext.xml file is as shown below, and please note that "org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer" is used configure the properties file.

  <beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:task="http://www.springframework.org/schema/task"
 xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

 <context:component-scan base-package="com.mycompany.app3" />

 <context:annotation-config />
 <context:spring-configured />

 <task:annotation-driven />

 <bean id="bridgePropertyPlaceholder"
  class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:*.properties</value>
   </list>
  </property>
 </bean>

 <import resource="classpath*:route.xml" />
 
 </beans>





Step 2: The properties file myapp.propertieswill be something like

myapp.in.dir=C:/temp/simple/input
myapp.in.file.pattern=AK(TAX|PROPERTY|TRADE)\.CSV
myapp.out.dir=C:/temp/simple/output
myapp.out.file.pattern=GBST_${file:name.noext}_${date:now:yyMMdd}_${date:now:HHmm}.${file:name.ext}

Step 3: Note that properties from the myapp.properties are referred with "{{ }}" double curly brackets. The Apache camel file language expressions are denoted with ${} as shown above to extract file name, file extension, etc.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:c="http://www.springframework.org/schema/c"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:camel="http://camel.apache.org/schema/spring"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
    
 <routeContext id="myapp" xmlns="http://camel.apache.org/schema/spring">
  <route id="myappFilePolling" >
   <from uri="file:{{myapp.in.dir}}?include={{myapp.in.file.pattern}}&delay=5000&initialDelay=5000&readLock=rename&move=archive&moveFailed=error&doneFileName=${file:name}.END" />
   <to uri="file:{{myapp.out.dir}}?fileName={{myapp.out.file.pattern}}" />
  </route>
 </routeContext>

</beans>


Step 4: Finally, the route.xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:camel="http://camel.apache.org/schema/spring"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

 <import resource="classpath*:myapp.xml" />

 <camel:errorHandler id="defaultErrorHandler" type="DefaultErrorHandler" />

 <camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring"
  errorHandlerRef="defaultErrorHandler">

  <routeContextRef ref="myapp" />

  <dataFormats>
   <zip id="myZip" />
  </dataFormats>
 </camelContext>

</beans>

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home