Friday, October 21, 2011

Using Apache Camel for Application Notification

I was watching a forum thread (http://fusesource.com/forums/thread.jspa?threadID=3433) this week where the use case was not to consume the file content but rather the file name. That seems to be an interesting use of Apache Camel because most of people used the Camel File Component to consume the contents of the file and then do some kind of processing on top of that (routing, web services invocation, ftp, bean execution, database interaction, etc).

What brought my attention to this specific use case is that the important information here was the file name and not the content of the file. Given that the file itself was pretty large (around ~1GB) it would be a nightmare to handle all of that in memory on a Camel route or any other middleware available out there. But, this use case is really about notifying other systems or applications that a large file is available to be processed outside the route (think about a file being available for download for example) and the reason to only care about the file name is that information could be part of the URL generated during the processing of the Camel route.

So, basically the Camel route was watching the file system for files generated, then grabbed the file name and then notify another system or application (or even the end user i.e. e-mail) that the file was ready to be consumed. I've recreated below the Camel route generated by default in the sample project to illustrate how to do that.


<beans xmlns="http://www.springframework.org/schema/beans"
       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">

<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
    <camel:package>com.fusesource.fusebyexample</camel:package>
 <camel:route>
      <!-- Consume Files from the File System -->
      <camel:from uri="file:src/data/?noop=true"/>
      <!-- Set the Message Payload with the File Name --> 
      <camel:setBody>
          <camel:simple>header.CamelFileName</camel:simple>
      </camel:setBody>
      <camel:log message="The message contains ${body}"/>
      <!-- Perform simple Content-Based Routing -->
      <camel:choice>
      <camel:when>
          <camel:simple>${body} == 'message1.xml'</camel:simple>
          <camel:log message="Going to UK message"/>
          <camel:to uri="file:target/messages/uk"/>
        </camel:when>
        <camel:otherwise>
          <camel:log message="Going to Other message"/>
          <camel:to uri="file:target/messages/others"/>
        </camel:otherwise>
      </camel:choice>
    </camel:route>
  </camel:camelContext>
</beans>


Of course, if you want to try that out, the project is available at: https://github.com/mjabali/FileNameBasedRouter

Enjoy the ride!

Setting Up Local Environment for Developing Oracle Intelligent Bots Custom Components

Oh the joy of having a local development environment is priceless. For most cloud based solutions the story repeats itself being hard to tr...