Home > OS >  Multiple when() not woking as expected in apache camel route
Multiple when() not woking as expected in apache camel route

Time:01-06

I've a route which uses multiple when() based on the fileName in header. I'm setting additional header value. In my case the choice() is not working as expected and its falling into only one condition. Can someone help me with alternative approach ?

It was working only if the file name prefixed with _On_Hand_SSTK which is last statement in that route. I need that to work based on the file names.

    from("file-watch:hello?events=CREATE&antInclude=**/*.csv&recursive=true")
        .routeId("fileWatch")
        .log("File Consumed Name: ${header.CamelFileName}")
        .to("direct:updateMessageHeaders")
        .end();

    from("direct:updateMessageHeaders")
        .routeId("updateHeaders")
        .choice()

         .when(exchange -> exchange.getIn().getHeader(Exchange.FILE_NAME).toString().trim().matches("report_\\d{8}_\\d{8}.csv"))
            .setHeader("CamelAzureStorageBlobContainerName", constant(AppConstants.CHECK_REPORT))

         .when(exchange -> exchange.getIn().getHeader(Exchange.FILE_NAME).toString().trim().matches("report_two_\\d{8}_\\d{8}.csv"))
            .setHeader("CamelAzureStorageBlobContainerName", constant(AppConstants.REPORT_TWO))
            
         .when(exchange -> exchange.getIn().getHeader(Exchange.FILE_NAME).toString().trim().matches("\\d{8}-\\d{6}_On_Hand_SSTK_\\d{4}.csv"))
            .setHeader("CamelAzureStorageBlobContainerName",constant(AppConstants.ON_HAND))

        .toD("direct:uploadFile")
        .end();

CodePudding user response:

On workaround try with following solution

Use .endChoice(): and end()

1) Use .endChoice() in order to return "back" to the Content Based Router, i.e., use .endChoice() to end a when condition if the code block is not a simple statement, see here for more information about this problem.

2) Use .end() in order to end the whole choice block.

Note :Nested choice definitions work just fine with Camel. sometimes terminators are wrong:

.endChoice() --> to close a "when" predicate

end() --> to close the entire "choice" block

Example:

.choice()
    .when(X1)
        // do stuff

        .choice()
            .when(Y)
                //do more stuff
            .endChoice() // close Y condition

        .end() // close inner choice block

    .endChoice() // close X1 condition

    .when(X2)
        // do other stuff
    .endChoice() // close X2 condition

    .otherwise()
        // default case
    .endChoice() // close default condition

.end()

For more details refer this SO Thread :

  •  Tags:  
  • Related