Apache Camel 根据请求使用文件内容丰富消息

2024-04-21

我正在实现 RESTful 服务(使用 CXFRS 组件),它应该返回某些请求的文件。每个文件都通过其 id 和扩展名来获取,即restfulservice.com/path/file/1/pdf。每个文件一旦添加就不会改变。文件在获取后不应移动或删除,通常它们应该可以同时访问。这是我的 Camel 上下文的一部分:

from("direct:fetchFile")
    .process(fetchFileProcessor) // set file.id & file.extension
    .bean(fileService, "fetchFile(${header.file.id}, ${header.file.extension})") // set body to filename
    .setHeader("CamelFileName", simple("${body}"))
    .choice()
        .when(header("file.extension").isEqualTo("xml"))
            .pollEnrich("file:///{{application.fileStorage.basePath}}/xml?noop=true", 500)
        .when(header("file.extension").isEqualTo("pdf"))
            .pollEnrich("file:///{{application.fileStorage.basePath}}/pdf?noop=true", 500)
    .end()
    .convertBodyTo(File.class)
    .bean(responseProvider, "getResponse(${body}, 200)");

此配置的问题在于,响应仅针对第二个(为什么?)请求具有非空主体,没有超时设置服务在带有调试消息的第二个请求上进入永恒循环

DEBUG o.a.c.c.f.FileConsumer - Took 0.000 seconds to poll <base path>\xml

Apache Camel 版本是 2.10.4

任何帮助,将不胜感激

UPD1:
有警告内容丰富器 http://camel.apache.org/content-enricher.html页面,显示“pollEnrich 不访问当前 Exchange 中的任何数据”。但如果我添加什么都不会改变fileName=${body}到文件 URL

UPD2:
pollEnrich 好像不支持动态fileName在 URL 中指定(link http://camel.465427.n5.nabble.com/pollEnrich-consumer-with-selector-td4939908.html)。当前时刻的路线:

from("direct:fetchFile")
    .process(fetchFileProcessor) // set file.id & file.extension
    .bean(fileService, "fetchFile(${header.file.id}, ${header.file.extension})") // set body to filename
    .choice()
        .when(header("file.extension").isEqualTo("xml"))
            .pollEnrich("file:///{{application.fileStorage.basePath}}/xml?fileName=${body}&noop=true", 500)
            .setHeader("asset.type", simple(MediaType.APPLICATION_XML))
        .when(header("file.extension").isEqualTo("pdf"))
            .pollEnrich("file:///{{application.fileStorage.basePath}}/pdf?fileName=${body}&noop=true", 500)
            .setHeader("asset.type", simple("application/pdf"))
    .end()
    .convertBodyTo(File.class)
    .process(multipartProcessor) // add file ass attachment to multipart body and set it as body
    .bean(responseProvider, "getResponse(${body}, 200)");

UPD3
我正在尝试实现自定义处理器以将 PollingConsumer 与动态文件名一起使用:

@Override
public void process(Exchange exchange) throws Exception {
    Long timeout = exchange.getIn().getHeader("file.timeout", Long.class);
    if (enrichUri == null) {
        throw new FileNotFoundException("'file.url' header not set");
    }

    CamelContext context = exchange.getContext();
    Endpoint endpoint = context.getEndpoint(enrichUri);
    PollingConsumer consumer = endpoint.createPollingConsumer();
    consumer.start();

    Exchange consumedExchange;
    try {
        if (timeout == null || timeout < 0) {
            consumedExchange = consumer.receive();
        } else if (timeout == 0) {
            consumedExchange = consumer.receiveNoWait();
        } else {
            consumedExchange = consumer.receive(timeout);
        }
    } catch (Exception e) {
        throw new AssetNotFoundException(e);
    } finally {
        consumer.stop();
    }
    exchange.getIn().setBody(consumedExchange.getIn().getBody());
}

现在它在第一次响应时返回文件内容,但在每个后续请求中,我得到了上述日志消息的永恒循环:

DEBUG o.a.c.c.f.FileConsumer - Took 0.000 seconds to poll <base path>\xml

UPD4
我已经实现了动态路由,该路由在处理之前添加并在处理之后删除。该方法描述于this http://camel.465427.n5.nabble.com/How-to-grab-a-only-one-file-with-camel-tp4930104p5508497.html在 Apache Camel 论坛中发帖。路由使用上述处理器来消费文件。结果是一样的


简单的方法往往是最好的方法。在这种情况下,我拒绝处理 Apache Camel 文件组件并实现了以下处理器:

public class FileLoadingProcessor implements Processor {

@Override
public void process(Exchange exchange) throws Exception {
    String filename = exchange.getIn().getBody(String.class); // message body contains filename
    String filePath = exchange.getIn().getHeader("fileprocessor.filepath", String.class);

    if (filePath == null || filename == null) {
        // throw some custom exception
    }

    URI uri = new URI(filePath.concat(filename));
    File file = new File(uri);

    if (!file.exists()) {
        throw new FileNotFoundException(String.format("File %s not found on %s", filename, filePath));
    }

    exchange.getIn().setBody(file);
}

现在它就像魅力一样发挥作用

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Apache Camel 根据请求使用文件内容丰富消息 的相关文章

随机推荐