我们可以让AsyncHttpClient使用现有线程执行代码吗?

时间:2019-04-25 08:46:53

标签: java apache-camel netty asynchttpclient

我试图使用AsyncHttpClient库从Java Spring MVC应用程序进行NIO调用。由于我已经在应用程序中合并了Apache Camel(带有camel-netty),因此我想利用从其线程池产生的Camel-Netty线程直接处理请求,而不是AsyncHttpClient创建自己的线程线程。

我正在尝试做这样的事情:

import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.BoundRequestBuilder;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.asynchttpclient.Dsl;

@Service
public class MyNIOClass {

  private io.netty.channel.nio.NioEventLoopGroup camelNettyEventLoopGroup = applicationContext.getBean("sharedNIOPool",io.netty.channel.nio.NioEventLoopGroup.class);
  private DefaultAsyncHttpClientConfig clientBuilder = clientBuilder = Dsl.config()
            .setMaxConnections(10)
            .setPooledConnectionIdleTimeout(100)
            .setConnectionTtl(500)
            .setRequestTimeout(5000)
            .setThreadPoolName( "My AHC ThreadPool" )
            .setKeepAlive( true )
            .setEventLoopGroup(camelNettyEventLoopGroup)
            .build();
  private AsyncHttpClient asyncClient = Dsl.asyncHttpClient(clientBuilder);

  public void makeAsyncHttpGetCall() throws SomeException {
    BoundRequestBuilder getRequest = asyncClient.prepareGet("http://someurl.com");
    AsyncCompletionHandler myAsyncCompletionHandler = getMyAsyncCompletionHandler();
    org.asynchttpclient.ListenableFuture<String> listenableFuture = getRequest.execute(myAsyncCompletionHandler);
    // Do something with the listenableFuture object. 
  }
}

makeAsyncHttpGetCall()从骆驼路线被调用。

AsyncHttpClient是否必须创建自己的线程,而不是使用调用此代码块的现有camel-netty线程,是否必要?我们可以使用调用线程吗?这样,就就像函数调用一样。

0 个答案:

没有答案