【发布时间】:2014-12-19 11:01:45
【问题描述】:
Apache Camel 2.13.1 宁 async-http-client 1.7.19
我试图了解是否可以将回复请求模式与 Camel 和 Ning async-http-client 一起使用,以便路由阻塞,直到收到 HTTP 响应。 (“如果你想阻塞直到收到响应,为什么要使用异步客户端?”你可能会问。这不完全是我的选择;但显然它以前被发现是我工作的最可靠的 http 客户端,并且这正是我们现在使用的。)
所以我有一个看起来像这样的骆驼设置:
<camel:camelContext id="camelContext">
<camel:package>com.lijit.blackbird</camel:package>
<camel:threadPoolProfile id="selectionPool" poolSize="100" maxPoolSize="512" maxQueueSize="-1" />
<camel:route id="delivery">
<camel:from uri="direct:start" />
<camel:to uri="direct:select" />
<camel:to uri="direct:resolve" />
<camel:to uri="direct:finalStep" />
</camel:route>
<camel:route id="select">
<camel:from uri="direct:select" />
<camel:multicast executorServiceRef="selectionPool">
<camel:to uri="selectorType1" />
<camel:to uri="selectorType2" />
...
<camel:to uri="selectorTypeN" />
</camel:multicast>
</camel:route>
<camel:route id="resolve">
<camel:from uri="direct:resolve" />
<camel:split stopOnException="false" parallelProcessing="true">
<camel:method bean="myResolutionSplitter" />
<camel:to uri="bean:contentFetchingAsyncHttpClient" />
</camel:split>
</camel:route>
</camel:camelContext>
正在发生的事情的要点是某些“选择器”选择了“东西”。所述内容包括可以从中获取一些内容的URL。因此,消息被拆分为需要“解析”(或者更清楚地说是获取内容)的每个 URL。
这个内容的获取是使用这个 Ning async-http-client 完成的,代码看起来基本上是这样的:
public class ContentFetchingAsyncHttpClient extends AsyncCompletionHandler<Response> {
private Future<Response> future;
private final AsyncHttpClient httpClient;
...
@Handler
public void fetch(URL url) {
Request request = new RequestBuilder()
.setMethod("GET")
.setUrl(url.toExternalForm())
.build();
future = httpClient.executeRequest(request, this);
}
@Override
public Response onCompleted(Response response) { /* do stuff with fetched content */ }
}
所以我的问题是:fetch() 方法为消息被拆分成的每个 URL 发送 HTTP 请求。由于fetch() 在收到响应之前返回,一旦所有请求都发出,Camel 路由将继续到 finalStep,它实际上需要检索到的内容......实际上还没有回来。
我认为请求-回复模式在这里可能会有所帮助,但这些示例似乎都是面向 JMS 的,我不清楚如何转换为 bean/POJO 世界。我在想象onCompleted() 方法需要以某种方式“回复”。非常欢迎任何具体的建议!
此外,现在我已经输入了这个我不太确定这是正确的模式。我真正想要实现的是同时发生 n 个 HTTP 请求,并且只有在所有请求都完成后才能进行路由。对 http 请求/响应的请求-回复似乎会将它们序列化而不是允许它们同时运行......所以任何关于更好的架构方法的建议也将受到高度欢迎。
【问题讨论】:
标签: java apache-camel asynchttpclient