a simple file jms file router in apache camel

Thu, Jul 2, 2009

This is really nice. I haven’t written a line of code. Just some config lines. I monitor a directory and when a file appears, the content is sent to a JMS queue and then the file is moved to a processed directory. This is all accomplished with this small Camel config snippet for the context and routes:

<camelContext id=“camel”
  xmlns=“http://activemq.apache.org/camel/schema/spring">
  <route>
    <from uri=“file:/tmp/inbound?consumer.delay=1000”/>
    <convertBodyTo type=“java.lang.String”/>
    <to uri=“activemq:queue:mqtest.q”/>
  </route>

<route> <from uri=“activemq:queue:mqtest.q”/> <to uri=“file:/tmp/inbound/processed”/> </route> </camelContext>

The first part of the route says to watch the /tmp/inbound directory once a second, convert the content of the file to a String object and send it to the ActiveMQ provided mqtest.q queue. The second part of the route looks for messages on the mqtest.q queue and copies the payload (the original file content) to a file in the /tmp/inbound/processed directory.

The broker and queue are equally easy to set up:

<broker useJmx=“true” persistent=“false”
  xmlns=“http://activemq.apache.org/schema/core">
  <transportConnectors>
  <transportConnector uri=“tcp://localhost:61616” />
  </transportConnectors>
</broker>

<bean id=“activemq” class=“org.apache.activemq.camel.component.ActiveMQComponent”> <property name=“brokerURL” value=“tcp://localhost:61616”/> </bean>

but if you hit namespace errors, have a look at the solution.

comments powered by Disqus