Apache Camel:带有bean URI和async-http-client的请求 - 回复模式

时间:2014-10-23 17:26:15

标签: java apache-camel asynchttpclient

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>

发生的事情的要点是某些&#34;选择器&#34;选择&#34;东西&#34;。所述内容包括可以从中提取某些内容的URL。因此,该消息被分成需要&#34;解决方案的每个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,这实际上需要检索到的内容......实际上还没有回来。< / p>

我在想这里的请求 - 回复模式可能会有所帮助,但是这些示例似乎都是面向JMS的,我不清楚我将如何转换为bean / POJO世界。我想象onCompleted()方法需要以某种方式回复&#34;。任何具体的建议都会受到欢迎!

而且,现在我输入了这个,我不确定这是正确的模式。我真正想要实现的是同时发生的n个HTTP请求,以及仅在所有这些请求完成时才进行的路由。对http请求/响应的请求 - 回复似乎会将它们序列化,而不是允许它们同时运行...因此,对于更好的架构方法的任何建议也将非常受欢迎。

1 个答案:

答案 0 :(得分:1)

bean组件不支持驼峰路由中的异步API。 为什么不直接使用camel-ahc组件? 它基于async-http-client。