我正在学习Camel并创建了如下路线:
from("timer:stream?period={{inbound.timer.period}}")
...
.setHeader(Exchange.HTTP_URI, simple(outboundUri()))
...
.multicast()
.to(
"stream:header",
"file://build?autoCreate=false",
outboundHttp
);
outboundUri()
方法返回路径${header.CamelFileName}
中带占位符的URI。我想要做的是使用某种标题映射器来解决这个问题,我可以按优先级顺序查看一些标题,如果没有,则设置CamelFileName
的默认值。
如何使用HTTP4
组件实现此目的?
答案 0 :(得分:0)
我最终实现了Processor,如下所示:
public class FilenameHeaderMessageProcessor implements org.apache.camel.Processor {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd-kkmm");
@Override
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
Map<String, Object> inHeaders = in.getHeaders();
log.debug("In headers: {}.", inHeaders);
Message out = exchange.getOut();
// Without this, out body is null
out.setBody(in.getBody());
Object filename = inHeaders.computeIfAbsent(FILE_NAME,
k -> Optional.ofNullable(inHeaders.get(KEY))
.orElse(defaultFilename())
);
out.setHeader(FILE_NAME, filename);
}
private final String defaultFilename() {
return DATE_TIME_FORMATTER.format(LocalDateTime.now()) + ".out";
}
}