nik*_*gra 6 java rest cxf apache-camel java-ee
我正在实现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)");
Run Code Online (Sandbox Code Playgroud)
此配置的问题是响应仅为第二个(为什么?)请求具有非空主体,没有超时设置服务在第二个请求上使用调试消息进入永久循环
DEBUG o.a.c.c.f.FileConsumer - Took 0.000 seconds to poll <base path>\xml
Run Code Online (Sandbox Code Playgroud)
Apace Camel版本是2.10.4
任何帮助,将不胜感激
UPD1:Content Enricher页面
上有警告,说'pollEnrich不访问当前Exchange的任何数据'.但是,如果添加到文件URL,则没有任何变化fileName=${body}
UPD2:
看起来pollEnrich不支持fileName
URL(链接)中指定的动态.目前的路线:
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)");
Run Code Online (Sandbox Code Playgroud)
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());
}
Run Code Online (Sandbox Code Playgroud)
现在它在第一个响应时返回文件内容,但是在每个后续请求中,我得到了上面日志消息的永久循环:
DEBUG o.a.c.c.f.FileConsumer - Took 0.000 seconds to poll <base path>\xml
Run Code Online (Sandbox Code Playgroud)
UPD4
我实现了动态路由,在处理之前添加并在之后删除.此方法在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);
}
Run Code Online (Sandbox Code Playgroud)
现在它的工作就像一个魅力
归档时间: |
|
查看次数: |
12627 次 |
最近记录: |