Java ApacheCamel:带有bean URI和异步http客户端的请求-应答模式

Java ApacheCamel:带有bean URI和异步http客户端的请求-应答模式,java,apache-camel,asynchttpclient,Java,Apache Camel,Asynchttpclient,ApacheCamel 2.13.1 Ning异步http客户端1.7.19 我试图理解我是否可以将应答请求模式与Camel和Ning异步http客户机一起使用,以便在收到http响应之前路由阻塞。(“如果你想阻止直到收到响应,那你为什么要使用异步客户端?”你可能会问。这不完全是我的选择;但很显然,以前人们发现它是我工作的最可靠的http客户端,这正是我们现在使用的。) 因此,我有一个驼峰设置,看起来像这样: <camel:camelContext id="camelContext">

ApacheCamel 2.13.1 Ning异步http客户端1.7.19

我试图理解我是否可以将应答请求模式与Camel和Ning异步http客户机一起使用,以便在收到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>
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 */ }

}

黑鸟
...
发生的事情的要点是,一些“选择器”选择“东西”。所述内容包括一个URL,可以从该URL获取某些内容。因此,消息被分成需要“解析”的每个URL(或者更直白地说是获取内容)

此内容的获取是使用此异步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>
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 */ }

}
公共类ContentFetchingAsyncHttpClient扩展了AsyncCompletionHandler{
私人未来;
私有最终异步httpClient httpClient;
...
@处理者
公共无效获取(URL){
请求=newrequestbuilder()
.setMethod(“GET”)
.setUrl(url.toExternalForm())
.build();
future=httpClient.executeRequest(请求,此);
}
@凌驾
已完成的公共响应(响应响应){/*对获取的内容执行操作*/}
}
所以我的问题是:
fetch()
方法为消息拆分成的每个URL发送HTTP请求。由于
fetch()
在收到响应之前返回,因此一旦发出所有请求,驼峰路线将继续执行
finalStep
,这实际上需要检索到的内容……但实际上还没有返回

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


而且,现在我已经打出来了,我不确定这是不是正确的模式。我真正想要实现的是n个并发发生的HTTP请求,并且只有当所有请求都完成时,路由才会进行。http请求/响应上的请求-响应似乎将序列化它们,而不是允许它们并发运行。。。因此,任何关于更好的体系结构方法的建议都是非常受欢迎的。

bean组件不支持camel路由中的aysnc API。 为什么不直接使用组件呢?
它基于异步http客户端

谢谢你,威廉。我想我的问题很大程度上是理解实现这一点的可能方法。