我有一個CXFRS端點,其中諸如「GET/files/x」的請求應從特定目錄返回文件「x」,然後將其刪除。這些文件是從另一個進程輸出的,然後通過此路由很快消耗。因此,如果需要的話,他們可以被接受調查並暫時保留在記憶中。駱駝請求/回覆關聯
以下是我的網絡服務。
@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>
我已經調查內容富集模式,但是這不會幫助,因爲原來的交換不能由濃縮塔內的消費者進行訪問。因此,文件名不能由輸入消息動態確定。換句話說,下面的例子不會讀取文件「x」,因爲文件端點永遠不會看到文件頭。
...setHeader(Exchange.FILE_NAME, "x").pollEnrich("file://dir")...
我也嘗試使用網絡服務的路由和一個單獨的文件路徑之間的集成模式,如下所示的聚合策略。
@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;
}
}
}
這不起作用,因爲聚合器模式似乎是「僅在」交換;在「進出」交換中,只要消息到達聚合器,回覆就會返回給客戶端,而不是在聚合完成後。隨後,我開始編寫下面的流程。
@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();
}
}
此過程只不過是允許我動態配置內容豐富。我覺得很難相信沒有更好的方法來做到這一點。特別是,我擔心在上下文中線程化並不斷添加/刪除組件/端點。
任何人都可以提出更好的解決方案來解決這個問題嗎?
有一個JIRA票可以改善未來駱駝版本中的pollEnrich:https://issues.apache.org/jira/browse/CAMEL-4163 – 2012-02-08 04:47:17