6 Replies Latest reply on Oct 23, 2011 3:23 AM by davsclaus

    Transform a file into a filename inside a route.

    markk

      I'm trying to setup an example that will watch a directory for new files and then alert another component about the name of this file.  I was thinking that I could stuff the name of the new file into a message queue and let another component deal with it from there.  The file may be a gigabyte or more, so I don't want to put the entire file into the MQ.

       

      I guess, my first question is:  Is using the mediation router a bad idea for such a large file?  Does it suck the entire contents of the file into the exchange or not until getIn().getBody() is called?

       

      Is there a built-in DSL way to transform the body of the message into just the name of a file or would you have to create a simple bean to do getOut().setBody(Exchange.FILE_NAME)?

       

       

      <from uri="file:/tmp&&amp;move=tagged/$"/>

      $</constant>

      </setBody>

      <to uri="activemq:queue:newFiles"/>

      </route>

        • 1. Re: Transform a file into a filename inside a route.
          jradecki

          Great spring; see explore <to uri stuff; easy; be patient...++1

          • 2. Re: Transform a file into a filename inside a route.
            davsclaus

            Hi

             

            Use the simple language instead of constant, and refer to the file as

            http://camel.apache.org/simple.html

            http://camel.apache.org/file-language.html

             

            So basically something like

            <simple>${file:name}</simple>
            

             

            • 3. Re: Transform a file into a filename inside a route.
              markk

              Simple seems like the proper solution, but it doesn't seem like it is allowed inside route tags

              <route>
                <from uri="file:inftp/?move=tagged/${file:name}"></from>
                  <setBody>
                    <simple>${file:name}</simple>
                  </setBody>
                <to uri="activemq:queue:newFiles"></to>
              </route>
              

               

              Throws an exception:

               

              Element 'route' cannot have character children, because the type's content type is element-only.

               

              Using a simple bean is fine, it would have been nice with only XML though.

               

              <route>
                <from uri="file:inftp/?move=tagged/${file:name}"></from>
                <bean ref="myFileName" method="process"></bean>
                <to uri="activemq:queue:newFiles"></to>
              </route>
              
                 <bean id="myFileName" class="com.foo.examples.camel_file.FileNameReplacer"></bean>
              

               

               

              import org.apache.camel.Processor;
              import org.apache.camel.Exchange;
              
              public class FileNameReplacer implements Processor {
                public void process(Exchange exchange) throws Exception {
                exchange.getIn().setBody( exchange.getIn().getHeader("CamelFileName") );
                }
              }
              

               

               

              p.s. is there no way to format code in this forum?

              yes, it is with curly brace code tags... not listed with the rest of the formatting, doesn't show up in preview, and doesn't follow square brackets like any of the other markup... nice ... oh, and the end code tag does not have a slash.

               

              Edited by: markk on Oct 21, 2011 2:10 PM

              • 4. Re: Transform a file into a filename inside a route.
                davsclaus

                Hm

                 

                You should be able to refer to a header as well

                 

                • 5. Re: Transform a file into a filename inside a route.
                  markk

                  Yes, I think that's it!

                   

                    <camelContext xmlns="http://camel.apache.org/schema/spring">
                      <route>
                        <from uri="file:inftp/?move=tagged/${file:name}"></from> 
                        <setBody>
                            <simple>${header.CamelFileName}</simple>
                        </setBody>
                        <log message="The message contains ${body}"></log>
                        <to uri="activemq:queue:newFiles"></to>
                      </route>
                    </camelContext>
                  

                   

                  I'm pretty sure it does not read the entire file body into the message either.  If I remove the setBody tag, and put a 900 Meg .iso file into the "inftp/" directory, then the whole thing grinds to a halt.  (which is a good thing).  When I have that setBody in there, it is near instantaneous delivery to the queue.

                   

                  Brilliant!

                  • 6. Re: Transform a file into a filename inside a route.
                    davsclaus

                    Yes you replace the message body with the file name, and thus the java.io.File handle for the actual file is no longer in the body, and therefore Camel does not load the 900mb when you log the message body in the log statement.