advanced file naming in camel

Thu, Jul 2, 2009

So you’ve got a queue and your route says to bung the messages into files in a processed directory. If you leave it up to Camel, you’ll get weird filenames but you can choose the filename yourself by setting the header in the route:

<route>
  <from uri=“activemq:queue:mqtest.q”/>
  <setHeader headerName=“org.apache.camel.file.name”>
    <constant>rubyfile.txt</constant>
  </setHeader>
  <to uri=“file:/tmp/t/processed”/>
</route>
you’ll then get a file, processed/rubyfile.txt when the message comes into the queue. But what if you want to name the file according to some criteria you’ve come up with to differentiate message payloads? Simple, use a processor in your route:
<route>
  <from uri=“activemq:queue:mqtest.q”/>
  <process ref=“fileNameProcessor” />
  <to uri=“file:/tmp/t/processed”/>
</route>
add the processor bean to Spring:
<bean id=“fileNameProcessor” class=“org.funnyfarm.processing.FileNameProcessor” />
and implement the processor:
package org.funnyfarm.processing;

import org.apache.camel.Processor; import org.apache.camel.Exchange;

public class FileNameProcessor implements Processor { public void process(Exchange exchange) throws Exception { exchange.getIn().setHeader(“org.apache.camel.file.name”, generateFileName()); }

private String generateFileName() { private String groovyFileName = null; // Do some groovy filename generation here return groovyFileName; } }

You now have a way to control the filenames for message payloads.

comments powered by Disqus