简体   繁体   English

骆驼分割文件和交换

[英]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). 我正在逐行处理一个csv文件,现在在处理内容之前,需要先验证标题(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. 我正在尝试在标头(Exchange)中设置属性,但是当我从文件中读取下一行时,我丢失了之前设置的属性。

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... 谢谢...

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;
}

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. 我没有检查出来,所以可能是错误的,但是我认为拆分器的每次迭代都在更改Message,这就是为什么您丢失标头的原因。

You could try using Exchange properties which should survive the next iteration of your splitter: 您可以尝试使用应该在拆分器的下一次迭代中保留下来的Exchange属性:

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. 编辑:如果您确实要共享bean的实例,则有一个重载的bean方法 ,该方法采用对象实例而不是类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM