4 Replies Latest reply on Feb 10, 2009 5:23 PM by Mark Richards

    Spring DSL <Choice> example

    Mark Richards Newbie

      Hi Guys,

       

      Is anybody able to help me translate the following Java DSL into spring

      DSL

       

      private void configureChoiceRoute()

           {

                 

              from(fileURI).choice()

                   .when(header(FileComponent.HEADER_FILE_NAME).contains("String1")).to(uri1)

                   .when(header(FileComponent.HEADER_FILE_NAME).contains("String2)).to(uri2)

                   .when(header(FileComponent.HEADER_FILE_NAME).contains("String3")).to(uri3)

                   .when(header(FileComponent.HEADER_FILE_NAME).contains("String4")).to(uri4)

                       .when(header(FileComponent.HEADER_FILE_NAME).contains("String5")).to(uri5)

                       .otherwise().to(uri_deadletter);         

           }

       

       

      I thoight it could start like this

       

      <camel:route>

         <camel:from uri="file://C:/temp/inFiles?moveNamePrefix=&amp;noop=true" />

              <camel:choice>

           <camel:when>

                <camel:simple>${in.header.file.name} == '.bar_m*'</camel:simple>

                <camel:to uri="file://C:/temp/outFiles/" />

           </camel:when>

               </camel:choice>

      </camel:route>

       

      However, the spring dsl route above doesn't seem to test the file name, and copies across files in the directory.

       

      As anybody implemented a router pattern using sproing dsl?  If so, could you give me some help.

       

      Cheers

      Mark

        • 1. Re: Spring DSL <Choice> example
          Claus Ibsen Master

          <camel:simple>${in.header.file.name} == '.bar_m*'</camel:simple>

           

          The simple language does not support operators such as == for comparison. Its very simple and only to be used for testing if a header exists, or to concat a string etc.

           

          What you need to use instead is the xpath that supports = for comparison:

          http://camel.apache.org/content-based-router.html

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

           

          <camel:xpath>$org.apache.camel.file.name = '.bar_m*'</camel:simple>

           

          But you want to test if the filename starts with bar_m, so you need to use the starts-with() xpath function. Try googling how to use it.

           

          If xpath gets just fuzzed to work with, as many of the xml stuff, then you can try another language that is more java like where you can use .startsWith or .matches or what else methods exists.

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

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

           

          Or you can use regular Java and implement the test in a plain java class = POJO. See the link below for a sample:

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

           

          Edited by: davsclaus on Feb 10, 2009 7:54 AM

          • 2. Re: Spring DSL <Choice> example
            Mark Richards Newbie

            Hey Claus,

             

            Thanks for that.   I'm working with flat files .csv's.  I have to route them to different folders depending on what the file names begin with

             

            I've tried using xpath inside my route

             

            trial 1)

             

            <camel:route>

            <camel:from uri="file://C:/temp/inFiles?moveNamePrefix=&amp;noop=true&amp;" />

            <camel:choice>

            <camel:when>

            <camel:xpath>$org.apache.camel.file.name='.aasd.xml'</camel:xpath>

            <camel:to uri="file://C:/temp/outFiles/" />

            </camel:when>

            </camel:choice>

            <camel:otherwise>

            <camel:to uri="file://C:/temp/deadLetter?autoCreate=false" />

            </camel:otherwise>

            </camel:route>

             

            trial 2)

            <camel:route>

            <camel:from uri="file://C:/temp/inFiles?moveNamePrefix=&amp;noop=true&amp;" />

            <camel:choice>

                 <camel:when>

                      <camel:xpath>in.headers[$org.apache.camel.file.name] ='.*'</camel:xpath>

                      <camel:to uri="file://C:/temp/outFiles/" />

                 </camel:when>

            </camel:choice>

            <camel:otherwise>

                 <camel:to uri="file://C:/temp/deadLetter?autoCreate=false" />

            </camel:otherwise>

            </camel:route>

             

            trial n)

            ..

            ..

              <camel:xpath>$org.apache.camel.file.name='.a*'</camel:xpath>

            ..

            ...

            trial n+1)

            <camel:xpath>${org.apache.camel.file.name}[starts-with(${org.apache.camel.file.name},'.a')]</camel:xpath>

            ..

            ..

            <camel:xpath>in.headers[$org.apache.camel.file.name][starts-with($org.apache.camel.file.name,'a')]</camel:xpath>

            ..

            ..

            <camel:xpath>in:header[org.apache.camel.file.name][starts-with(org.apache.camel.file.name,'a')]</camel:xpath>

             

            I've even tried writing it as a filter to check I can repeat the examples

            <camel:route>

            <camel:from uri="file://C:/temp/inFiles?moveNamePrefix=&amp;noop=true&amp;" />

            <camel:filter>

            <camel:xpath>$org.apache.camel.file.name = '.a*'</camel:xpath>

            <camel:process ref="logger"/>

            <camel:to uri="file://C:/temp/outFiles/" />

            </camel:filter>

             

                  

            </camel:route>

             

            All my files end up in the deadletter folder, which means the comparison isn't working or I get an InvalidXPathExpression: Invalid xpath as my xpath is wrong.

             

            From within xpath,

             

            Does $org.apache.camel.file.name give you the file name?

             

            I've read over a few times  http://camel.apache.org/xpath.html

             

            and I should be able to access the file name from

            message.in.header

             

             

            I've managed to achieve setting the file name comparison from within the xml using my own bean, I've attched the xml and the pojo.  I'd still love to know if it can be done using xpath

             

            Yours hopingly

             

            Cheers

            Mark

            • 3. Re: Spring DSL <Choice> example
              Claus Ibsen Master

              Hi

               

              Does $org.apache.camel.file.name give you the file name?

              The $ notation should be a shorthand to get the in headers. But since the header name itself has dots in it, it might confuse xpath. Have you tried with ' ' around

               

              $'org.apache.camel.file.name'

               

              I am not particual keen on xpath, to do non XML work. So personally I use one of the other scripting languages, or even better with POJO as I can unit test it in isolation.

               

              You might wanna mess around with it for a while until the xpath stuff works. But I stay clear of it mostly and use other solutions.

               

              In Java DSL you have expression builders so you can express it more powerful. We are considering how to bring that into the Spring XML as well.

              • 4. Re: Spring DSL <Choice> example
                Mark Richards Newbie

                Hi Claus,

                 

                Yeah, I'll start looking more into some of the scripting languages.  Writting my own bean was pretty simple so I'll go with that for the moment, while I build up camel knowledge and identify more scenarios where I can use Fuse/Camel.

                 

                Thanks for all help and quick responses,

                 

                Cheers

                Mark