Hector
Hector

Reputation: 851

Camel Split File and Exchange

I am processing a csv file, line by line, now before processing the content, is required to validate the headers (first line from csv). I'm trying to set a property in the header (Exchange) but when I read next line from file I lost property that I set up previously.

from("file:/home/archivos/")
   .split().tokenize("\n",1)
       .choice()
         .when(simple("${property.CamelSplitIndex} > 0"))
           .bean(BindingMDS.class, "processContent(${body}, ${file:name})")
         .otherwise()
           .bean(BindingMDS.class, "processHeader(${body}, ${file:name}");

Thats it's the bean

public class BindingMDS {
...

public void processHeader(String cabeceras, String nombreArchivo, Exchange exchange) {
    ... // validate columns from header
    exchange.getIn().setHeader("IS_CORRECT_HEADER", new Integer(1));
}

public String processContent(String body, String nombreArchivo, Exchange exchange) {
    Integer flag = (Integer) exchange.getIn().getHeader("IS_CORRECT_HEADER");
     // ... this value is null
}           

}

Any idea?, I saw in debug mode that they are two different instances...

Thanks...

Upvotes: 1

Views: 2067

Answers (2)

Hector
Hector

Reputation: 851

Finally I resolved this problem using Strategy

from("file:/home/archivos/")
   .split(body().tokenize("\n"), new MyStrategyCSV())
       .choice()           
         .when(simple("${property.CamelSplitIndex} > 0"))
           .bean(BindingMDS.class, "processContent(${body})") 
         .otherwise()
           .bean(BindingMDS.class, "processHeader(${body})") // validate headers from csv and setup property in Exchange
      .end() // end choice
   .end() // end splitter
.to("direct:processNewContent");

from("direct:processNewContent")
    .bean(BindingMDS.class, "validateFile(${body})");

And Strategy...

@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {

    if (oldExchange == null) {
        // the first time we aggregate we only have the new exchange,
        // so we just return it 
        return newExchange;
    }

    ...

    // return old
    return oldExchange;
}

Upvotes: 1

samlewis
samlewis

Reputation: 4048

I havent checked this out so could be wrong but I assume that every iteration of your splitter is changing the Message which is why you are loosing the header.

You could try using Exchange properties which should survive the next iteration of your splitter:

exchange.setProperty("IS_CORRECT_HEADER", new Integer(1));

...

Integer flag = (Integer) exchange.getProperty("IS_CORRECT_HEADER");

Edit: If you really want to share an instance of your bean, there is an overloaded bean method which takes an object instance not a class.

Upvotes: 0

Related Questions