我正在使用Apache Camel编写演示程序。 Spring Boot调度程序正在调用Out Camel路由,它将把文件从源目录C:\ CamelDemo \ inputFolder传输到目标目录C:\ CamelDemo \ outputFolder
Spring Boot调度程序如下
@Component
public class Scheduler {
@Autowired
private ProducerTemplate producerTemplate;
@Scheduled(cron = "#{@getCronValue}")
public void scheduleJob() {
System.out.println("Scheduler executing");
String inputEndpoint = "file:C:\\CamelDemo\\inputFolder?noop=true&sendEmptyMessageWhenIdle=true";
String outputEndpoint = "file:C:\\CamelDemo\\outputFolder?autoCreate=false";
Map<String, Object> headerMap = new HashMap<String, Object>();
headerMap.put("inputEndpoint", inputEndpoint);
headerMap.put("outputEndpoint", outputEndpoint);
producerTemplate.sendBodyAndHeaders("direct:transferFile", null, headerMap);
System.out.println("Scheduler complete");
}
}
Apache Camel路由如下
@Component
public class FileTransferRoute extends RouteBuilder {
@Override
public void configure() {
errorHandler(defaultErrorHandler()
.maximumRedeliveries(3)
.redeliverDelay(1000)
.retryAttemptedLogLevel(LoggingLevel.WARN));
from("direct:transferFile")
.log("Route reached")
.log("Input Endpoint: ${in.headers.inputEndpoint}")
.log("Output Endpoint: ${in.headers.outputEndpoint}")
.pollEnrich().simple("${in.headers.inputEndpoint}")
.recipientList(header("outputEndpoint"));
//.to("file:C:\\CamelDemo\\outputFolder?autoCreate=false")
}
}
当我注释出receiveList()的行并取消注释to()时,即在to()中的gigistatic静态端点上,该流程正在工作。但是,当我对()进行注释并取消对receiveerList()的注释时,它不起作用。请帮助如何将消息路由到动态端点(outputEndpoint)?
答案 0 :(得分:1)
您在使用pollEnrich
时未指定AggregationStrategy
:在这种情况下,Camel将根据检索到的资源创建新的OUT消息,而不将其与原始的IN消息组合:这意味着您将丢失先前在IN消息上设置的标头。
请参阅文档:https://camel.apache.org/manual/latest/enrich-eip.html#_a_little_enrich_example_using_java
strategyRef 引用一个AggregationStrategy,用于将来自外部服务的答复合并为一条外发消息。默认情况下,骆驼会将外部服务的回复用作传出消息。
一种简单的解决方案是在AggregationStrategy
组件上定义一个简单的pollEnrich
,该组件将标头从IN消息复制到新的OUT消息(请注意,然后您将使用原始的IN消息身体,但我认为这不是问题)
from("direct:transferFile")
.log("Route reached")
.log("Input Endpoint: ${in.headers.inputEndpoint}")
.log("Output Endpoint: ${in.headers.outputEndpoint}")
.pollEnrich().simple("${in.headers.inputEndpoint}")
.aggregationStrategy((oldExchange, newExchange) -> {
// Copy all headers from IN message to the new OUT Message
newExchange.getIn().getHeaders().putAll(oldExchange.getIn().getHeaders());
return newExchange;
})
.log("Output Endpoint (after pollEnrich): ${in.headers.outputEndpoint}")
.recipientList(header("outputEndpoint"));
//.to("file:C:\\var\\CamelDemo\\outputFolder?autoCreate=false");