gpt4 book ai didi

java - Camel 分割文件和交换

转载 作者:太空宇宙 更新时间:2023-11-04 07:26:30 25 4
gpt4 key购买 nike

我正在逐行处理 csv 文件,现在在处理内容之前,需要验证标题(csv 的第一行)。我正在尝试在 header (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}");

这就是 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
}

}

知道吗?,我在 Debug模式下看到它们是两个不同的实例...

谢谢...

最佳答案

最后我用策略解决了这个问题

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

和策略...

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

关于java - Camel 分割文件和交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18454037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com