gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:02:31 24 4
gpt4 key购买 nike

我正在实现 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

Apace Camel 版本为 2.10.4

任何帮助将不胜感激

UPD1:
Content Enricher 上有警告页面,显示“pollEnrich 不访问当前 Exchange 的任何数据”。但是,如果我将 fileName=${body} 添加到文件 URL

,则不会发生任何变化

UPD2:
似乎 pollEnrich 不支持 URL ( link) 中指定的动态 fileName。当前路线:

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 中描述了此方法在 Apache Camel 论坛发帖。 Route 使用上面的处理器来消费文件。结果是一样的

最佳答案

简单的方法往往是最好的方法。在这种情况下,我拒绝处理 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);
}

现在它像魅力一样工作

关于java - Apache Camel 根据请求使用文件内容丰富消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15947523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com