Java 驼峰请求/应答相关性
我有一个CXFRS端点,在这里,诸如“GET/files/x”之类的请求应该从特定目录返回文件“x”,然后将其删除。这些文件从另一个进程输出,然后通过此路由快速使用。因此,如果需要的话,对它们进行轮询并暂时保存在内存中是可以接受的 下面是我的web服务Java 驼峰请求/应答相关性,java,cxf,apache-camel,enterprise-integration,cxfrs,Java,Cxf,Apache Camel,Enterprise Integration,Cxfrs,我有一个CXFRS端点,在这里,诸如“GET/files/x”之类的请求应该从特定目录返回文件“x”,然后将其删除。这些文件从另一个进程输出,然后通过此路由快速使用。因此,如果需要的话,对它们进行轮询并暂时保存在内存中是可以接受的 下面是我的web服务 @Component @Path("/") public class WebService { @GET @Path("files/{id}") public String getFile(@PathParam("id") String i
@Component
@Path("/")
public class WebService {
@GET
@Path("files/{id}")
public String getFile(@PathParam("id") String id) {
return null;
}
}
下面是不完整的路线
<route>
<from uri="cxfrs://bean:webService"/>
<choice>
<when>
<simple>${in.headers.operationName} == 'getFile'</simple>
<setHeader headerName="correlationId">
<simple>mandatoryBodyAs(java.lang.String)</simple>
</setHeader>
???
</when>
<choice>
</route>
我还尝试在web服务路由和单独的文件路由之间使用聚合器模式,聚合策略如下所示
@Component
public class Aggregator implements AggregationStrategy {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
return newExchange;
} else {
oldExchange.getOut().setBody(newExchange.getIn().getBody());
return oldExchange;
}
}
}
这不起作用,因为聚合器模式似乎是用于“只在”交换;使用“in-out”交换,消息到达聚合器时,而不是聚合完成后,会立即将回复返回给客户端。随后,我开始写下面的过程
@Component
public class FileEnricher implements Processor {
@Value("${folder}")
private String folder;
public void process(Exchange exchange) throws Exception {
Endpoint endpoint = exchange.getContext().getEndpoint(String.format(
"file://%s?fileName=%s",
folder,
exchange.getIn().getHeader("correlationId")
));
PollingConsumer consumer = endpoint.createPollingConsumer();
PollEnricher enricher = new PollEnricher(consumer);
enricher.setTimeout(10000);
consumer.start();
enricher.process(exchange);
enricher.shutdown();
consumer.stop();
}
}
这个过程只允许我动态配置一个内容充实器。我很难相信没有比这更好的方法了。特别是,我关心的是线程和在上下文中不断添加/删除组件/端点
有人能为这个问题提出更好的解决方案吗?只需将消息体设置为java.io.File,以使文件更丰富,例如从java bean中。然后将消息路由到bean:
public File whichFileToPick(@Header("id") String id) {
return new File("somedir/" + id");
}
在未来,这条路线将得到加强,因此直接走骆驼路线将更容易。Christian想出了一个好主意,允许指定一个Camel表达式作为pollRich的uri。在未来的Camel版本中,有一个JIRA票证可以改进pollRich:
public File whichFileToPick(@Header("id") String id) {
return new File("somedir/" + id");
}