1

我正在学习 Camel 并创建了如下路线:

from("timer:stream?period={{inbound.timer.period}}")
      ...
      .setHeader(Exchange.HTTP_URI, simple(outboundUri()))
      ...
      .multicast()
      .to(
              "stream:header",
              "file://build?autoCreate=false",
              outboundHttp
      );

outboundUri()方法返回一个在路径中带有占位符的 URI ${header.CamelFileName},. 我想做的是使用某种标题映射器来解决这个问题,我可以按优先级顺序查看一些标题,如果不存在,为CamelFileName.

如何使用该HTTP4组件来实现这一点?

4

1 回答 1

0

我最终实现了一个处理器,如下所示:

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";
    }
}
于 2017-06-19T08:12:55.293 回答