我正在学习来自" Camel in Action"预订,目前我正在进行数据转换。更特别是Content Enricher EIP。我注意到,当我从书中运行下面的代码时,Camel会创建fileName + .camelLock文件,但在完成路径后它不会将其删除。
代码方面有什么问题吗?或者应该这样工作?
import java.io.File;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class OrderToCsvProcessorTest extends CamelTestSupport {
@Test
public void testOrderToCsvProcessor() throws Exception {
// this is the inhouse format we want to transform to CSV
String inhouse = "0000004444000001212320091208 1217@1478@2132";
template.sendBodyAndHeader("direct:start", inhouse, "Date", "20091208");
File file = new File("target/orders/received/report-20091208.csv");
assertTrue("File should exist", file.exists());
// compare the expected file content
String body = context.getTypeConverter().convertTo(String.class, file);
assertEquals("000000444,20091208,000001212,1217,1478,2132\nthis,is,sample,string", body);
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.process(new OrderToCsvProcessor())
.pollEnrich("file://target/input?noop=true",
new AggregationStrategy() {
@Override
public Exchange aggregate( Exchange oldExchange, Exchange newExchange) {
if (newExchange == null) {
return oldExchange;
}
String http = oldExchange.getIn().getBody(String.class);
String ftp = newExchange.getIn().getBody(String.class);
String body = http + "\n" + ftp;
oldExchange.getIn().setBody(body);
return oldExchange;
}
})
.to("file://target/orders/received?fileName=report-${header.Date}.csv");
}
};
}
}
代码中使用的处理器:
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class OrderToCsvProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
String custom = exchange.getIn().getBody(String.class);
String id = custom.substring(0, 9);
String customerId = custom.substring(10, 19);
String date = custom.substring(20, 29);
String items = custom.substring(30);
String[] itemIds = items.split("@");
StringBuilder csv = new StringBuilder();
csv.append(id.trim());
csv.append(",").append(date.trim());
csv.append(",").append(customerId.trim());
for (String item : itemIds) {
csv.append(",").append(item.trim());
}
exchange.getIn().setBody(csv.toString());
}
}
答案 0 :(得分:0)
GenericFileOnCompletion负责删除锁定文件。你需要像这样在AggregationStrategy中切换完成。
new AggregationStrategy() {
@Override
public Exchange aggregate( Exchange oldExchange, Exchange newExchange) { if (newExchange == null) {
return oldExchange; }
String http = oldExchange.getIn().getBody(String.class); String ftp = newExchange.getIn().getBody(String.class);
String body = http + "\n" + ftp;
oldExchange.getIn().setBody(body);
newExchange.handoverCompletions(oldExchange);
return oldExchange; } })
答案 1 :(得分:0)
该问题是由于您正在使用具有自定义AggregationStrategy 的pollEnrich提取文件。
在这种使用情况下使用自定义AggregationStrategy时,则需要将聚合Exchange的某些属性复制到原始Exchange上,以正确删除Camel markerFile
因此,在您的AggregationStrategy策略结束时,您可以执行以下操作:
int checkPrime_fix1(int isItPrime) {
int result = 0, j = 2;
// Avoid sqrt(isItPrime). Floating point has too many subtle problem for an integer task
// Avoid j*j <= isItPrime to prevent overflow with large isItPrime
while(j <= isItPrime/j) {
result = isItPrime%j;
j++;
if (result==0)
return 0;
}
return isItPrime > 1;
}