org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor Java Examples

The following examples show how to use org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpAsyncClientFactory.java    From flower with Apache License 2.0 7 votes vote down vote up
public HttpAsyncClientFactory() {
  try {
    int ioThreadCount = Runtime.getRuntime().availableProcessors() * 8;
    IOReactorConfig ioReactorConfig =
        IOReactorConfig.custom().setIoThreadCount(ioThreadCount)
            .setConnectTimeout(httpConfig.getConnectTimeout()).setSoTimeout(httpConfig.getReadTimeout()).build();

    ConnectingIOReactor ioReactor =
        new DefaultConnectingIOReactor(ioReactorConfig, new NamedThreadFactory("flower-http"));
    this.connectionManager = new PoolingNHttpClientConnectionManager(ioReactor);
    this.connectionManager.setMaxTotal(1024);
    this.connectionManager.setDefaultMaxPerRoute(256);
    initHttpClient();
  } catch (Exception e) {
    logger.error("", e);
  }
}
 
Example #2
Source File: YunpianClient.java    From yunpian-java-sdk with MIT License 6 votes vote down vote up
private CloseableHttpAsyncClient createHttpAsyncClient(YunpianConf conf) throws IOReactorException {
    IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(Runtime.getRuntime().availableProcessors())
            .setConnectTimeout(conf.getConfInt(YunpianConf.HTTP_CONN_TIMEOUT, "10000"))
            .setSoTimeout(conf.getConfInt(YunpianConf.HTTP_SO_TIMEOUT, "30000")).build();
    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);

    PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .setCharset(Charset.forName(conf.getConf(YunpianConf.HTTP_CHARSET, YunpianConf.HTTP_CHARSET_DEFAULT))).build();
    connManager.setDefaultConnectionConfig(connectionConfig);
    connManager.setMaxTotal(conf.getConfInt(YunpianConf.HTTP_CONN_MAXTOTAL, "100"));
    connManager.setDefaultMaxPerRoute(conf.getConfInt(YunpianConf.HTTP_CONN_MAXPERROUTE, "10"));

    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setConnectionManager(connManager).build();
    httpclient.start();
    return httpclient;
}
 
Example #3
Source File: FiberApacheHttpClientRequestExecutor.java    From jbender with Apache License 2.0 6 votes vote down vote up
public FiberApacheHttpClientRequestExecutor(final Validator<CloseableHttpResponse> resValidator, final int maxConnections, final int timeout, final int parallelism) throws IOReactorException {
  final DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(IOReactorConfig.custom().
    setConnectTimeout(timeout).
    setIoThreadCount(parallelism).
    setSoTimeout(timeout).
    build());

  final PoolingNHttpClientConnectionManager mngr = new PoolingNHttpClientConnectionManager(ioreactor);
  mngr.setDefaultMaxPerRoute(maxConnections);
  mngr.setMaxTotal(maxConnections);

  final CloseableHttpAsyncClient ahc = HttpAsyncClientBuilder.create().
    setConnectionManager(mngr).
    setDefaultRequestConfig(RequestConfig.custom().setLocalAddress(null).build()).build();

  client = new FiberHttpClient(ahc);
  validator = resValidator;
}
 
Example #4
Source File: ApacheHttpAsyncClient.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
private NHttpClientConnectionManager getNHttpConnManager(Config config) throws IOException {
  NHttpClientConnectionManager httpConnManager;

  String connMgrStr = config.getString(HTTP_CONN_MANAGER);
  switch (ApacheHttpClient.ConnManager.valueOf(connMgrStr.toUpperCase())) {
    case POOLING:
      ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
      PoolingNHttpClientConnectionManager poolingConnMgr = new PoolingNHttpClientConnectionManager(ioReactor);
      poolingConnMgr.setMaxTotal(config.getInt(POOLING_CONN_MANAGER_MAX_TOTAL_CONN));
      poolingConnMgr.setDefaultMaxPerRoute(config.getInt(POOLING_CONN_MANAGER_MAX_PER_CONN));
      httpConnManager = poolingConnMgr;
      break;
    default:
      throw new IllegalArgumentException(connMgrStr + " is not supported");
  }

  LOG.info("Using " + httpConnManager.getClass().getSimpleName());
  return httpConnManager;
}
 
Example #5
Source File: ApacheAsyncClientExecutor.java    From BootNettyRpc with Apache License 2.0 5 votes vote down vote up
public void initialize(final NettyRpcProperties properties) throws Exception {
    final CommonProperties cp = properties.getCommonProperties();
    final CyclicBarrier barrier = new CyclicBarrier(2);
    Executors.newCachedThreadPool().submit(new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            try {


                IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
                        .setIoThreadCount(CommonProperties.CPUS * 2)
                        .setConnectTimeout(Integer.parseInt(cp.getHttpConnectTimeout()))
                        .setSoTimeout(Integer.parseInt(cp.getHttpSocketTimeout()))
                        .setSndBufSize(Integer.parseInt(cp.getHttpSendBufSize()))
                        .setRcvBufSize(Integer.parseInt(cp.getHttpRcvBufSize()))
                        .setBacklogSize(Integer.parseInt(cp.getHttpBackLogSize()))
                        .setTcpNoDelay(true)
                        .setSoReuseAddress(true)
                        .setSoKeepAlive(true)
                        .build();
                ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
                PoolingNHttpClientConnectionManager httpManager = new PoolingNHttpClientConnectionManager(ioReactor);
                httpManager.setMaxTotal(Integer.parseInt(cp.getHttpMaxTotal()));

                httpAsyncClient = HttpAsyncClients.custom().setConnectionManager(httpManager).build();
                httpAsyncClient.start();

                LOG.info("Create apache async client successfully");

                barrier.await();
            } catch (IOReactorException e) {
                LOG.error("Create apache async client failed", e);
            }

            return null;
        }
    });

    barrier.await();
}
 
Example #6
Source File: ApacheAsyncClientExecutor.java    From distributed-limit with Apache License 2.0 5 votes vote down vote up
public void initialize() throws Exception {

        final CyclicBarrier barrier = new CyclicBarrier(2);
        Executors.newCachedThreadPool().submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                try {

                    IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
                            .setIoThreadCount(CPUS * 2)
                            .setConnectTimeout(Integer.parseInt(HTTPCLIENT_CONNCT_TIMEOUT_DEFAULT))
                            .setSoTimeout(Integer.parseInt(HTTPCLIENT_SOCKET_TIMEOUT_DEFAULT))
                            .setSndBufSize(Integer.parseInt(HTTPCLIENT_SEDBUFSIZE_DEFAULT))
                            .setRcvBufSize(Integer.parseInt(HTTPCLIENT_RCV_BUFSIZE_DEFAULT))
                            .setBacklogSize(Integer.parseInt(HTTPCLIENT_BACK_LOG_SIZE_DEFAULT))
                            .setTcpNoDelay(true)
                            .setSoReuseAddress(true)
                            .setSoKeepAlive(true)
                            .build();
                    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
                    PoolingNHttpClientConnectionManager httpManager = new PoolingNHttpClientConnectionManager(ioReactor);
                    httpManager.setMaxTotal(Integer.parseInt(HTTPCLIENT_MAX_TOTAL_DEFAULT));
                    httpAsyncClient = HttpAsyncClients.custom().setConnectionManager(httpManager).build();
                    httpAsyncClient.start();

                    LOG.info("Create apache async client successfully");
                    isStarted=true;
                    barrier.await();
                } catch (IOReactorException e) {
                    LOG.error("Create apache async client failed", e);
                }

                return null;
            }
        });

        barrier.await();
    }
 
Example #7
Source File: AsyncInternalClient.java    From fc-java-sdk with MIT License 5 votes vote down vote up
public AsyncInternalClient(Config config){
    this.config = config;

    try {
        IOReactorConfig ioReactorConfig = IOReactorConfig.custom().build();
        ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
        PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
        cm.setMaxTotal(config.getMaxConnectCount());
        cm.setDefaultMaxPerRoute(config.getMaxPerRoute());
        httpClient = createHttpAsyncClient(config, cm);
        httpClient.start();
    } catch (IOReactorException e) {
        throw new ClientException(e);
    }
}
 
Example #8
Source File: AutoCleanedPoolingNHttpClientConnectionManager.java    From caravan with Apache License 2.0 5 votes vote down vote up
protected static ConnectingIOReactor createConnectingIOReactor(int ioThreadCount) {
    IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(ioThreadCount).setTcpNoDelay(true).build();
    try {
        return new DefaultConnectingIOReactor(ioReactorConfig);
    } catch (IOReactorException ex) {
        throw new IOReactorRuntimeException(ex);
    }
}
 
Example #9
Source File: WxMpTemplateMsgSender.java    From WePush with MIT License 5 votes vote down vote up
public static CloseableHttpAsyncClient getCloseableHttpAsyncClient() throws IOReactorException {
    if (closeableHttpAsyncClient == null) {
        synchronized (WxMpTemplateMsgSender.class) {
            if (closeableHttpAsyncClient == null) {
                RequestConfig requestConfig = RequestConfig.custom()
                        .setConnectTimeout(-1)
                        .setSocketTimeout(-1)
                        .setConnectionRequestTimeout(-1)
                        .build();

                //配置io线程
                IOReactorConfig ioReactorConfig = IOReactorConfig.custom().
                        setIoThreadCount(Runtime.getRuntime().availableProcessors())
                        .setSoKeepAlive(true).setConnectTimeout(-1).setSoTimeout(-1)
                        .build();
                //设置连接池大小
                ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
                PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
                //最大连接数
                connManager.setMaxTotal(5000);
                //per route最大连接数
                connManager.setDefaultMaxPerRoute(5000);

                closeableHttpAsyncClient = HttpAsyncClients.custom().
                        setConnectionManager(connManager)
                        .setDefaultRequestConfig(requestConfig)
                        .build();

                closeableHttpAsyncClient.start();
            }
        }
    }
    return closeableHttpAsyncClient;
}
 
Example #10
Source File: AsyncServiceClient.java    From aliyun-tablestore-java-sdk with Apache License 2.0 5 votes vote down vote up
public AsyncServiceClient(ClientConfiguration config) {
    try {
        IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
            .setIoThreadCount(config.getIoThreadCount()).build();
        ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(
            ioReactorConfig);
        PoolingNHttpClientConnectionManager cm =
            new PoolingNHttpClientConnectionManager(ioReactor);
        cm.setMaxTotal(config.getMaxConnections());
        cm.setDefaultMaxPerRoute(config.getMaxConnections());
        httpClient = HttpFactory.createHttpAsyncClient(config, cm);

        /*
         * socketTimeout的值限制了closeIdleConnections执行的周期。
         * 如果周期相对socketTimeout的值过长,有可能一个请求分配到一个即将socketTimeout的连接,
         * 在请求发送之前即抛出SocketTimeoutException。
         * 现在让closeIdleConnections的执行周期为socketTimeout / 2.5。
         */
        long closePeriod = 5000;
        if (config.getSocketTimeoutInMillisecond() > 0) {
            closePeriod = (long) (config.getSocketTimeoutInMillisecond() / 2.5);
        }
        closePeriod = closePeriod < 5000 ? closePeriod : 5000;
        connEvictor = new IdleConnectionEvictor(cm, closePeriod);
        httpClient.start();
        connEvictor.start();
    } catch (IOReactorException ex) {
        throw new ClientException(String.format("IOReactorError: %s",
                ex.getMessage()), ex);
    }
}
 
Example #11
Source File: ApacheAsyncClientExecutor.java    From Thunder with Apache License 2.0 5 votes vote down vote up
public void initialize(final ThunderProperties properties) throws Exception {
    final CyclicBarrier barrier = new CyclicBarrier(2);
    Executors.newCachedThreadPool().submit(new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            try {
                IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
                        .setIoThreadCount(ThunderConstant.CPUS)
                        .setConnectTimeout(properties.getInteger(ThunderConstant.APACHE_CONNECT_TIMEOUT_ATTRIBUTE_NAME))
                        .setSoTimeout(properties.getInteger(ThunderConstant.APACHE_SO_TIMEOUT_ATTRIBUTE_NAME))
                        .setSndBufSize(properties.getInteger(ThunderConstant.APACHE_SNDBUF_SIZE_ATTRIBUTE_NAME))
                        .setRcvBufSize(properties.getInteger(ThunderConstant.APACHE_RCVBUF_SIZE_ATTRIBUTE_NAME))
                        .setBacklogSize(properties.getInteger(ThunderConstant.APACHE_BACKLOG_SIZE_ATTRIBUTE_NAME))
                        .setTcpNoDelay(true)
                        .setSoReuseAddress(true)
                        .setSoKeepAlive(true)
                        .build();
                ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
                PoolingNHttpClientConnectionManager httpManager = new PoolingNHttpClientConnectionManager(ioReactor);
                httpManager.setMaxTotal(ThunderConstant.CPUS * properties.getInteger(ThunderConstant.APACHE_MAX_TOTAL_ATTRIBUTE_NAME));

                httpAsyncClient = HttpAsyncClients.custom().setConnectionManager(httpManager).build();
                httpAsyncClient.start();

                LOG.info("Create apache async client successfully");

                barrier.await();
            } catch (IOReactorException e) {
                LOG.error("Create apache async client failed", e);
            }

            return null;
        }
    });

    barrier.await(properties.getLong(ThunderConstant.APACHE_CONNECT_TIMEOUT_ATTRIBUTE_NAME) * 2, TimeUnit.MILLISECONDS);
}
 
Example #12
Source File: BceHttpClient.java    From bce-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Create connection manager for asynchronous http client.
 *
 * @return Connection manager for asynchronous http client.
 * @throws IOReactorException in case if a non-recoverable I/O error.
 */
protected NHttpClientConnectionManager createNHttpClientConnectionManager() throws IOReactorException {
    ConnectingIOReactor ioReactor =
            new DefaultConnectingIOReactor(IOReactorConfig.custom()
                    .setSoTimeout(this.config.getSocketTimeoutInMillis()).setTcpNoDelay(true).build());
    PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(ioReactor);
    connectionManager.setDefaultMaxPerRoute(this.config.getMaxConnections());
    connectionManager.setMaxTotal(this.config.getMaxConnections());
    return connectionManager;
}
 
Example #13
Source File: RestClient.java    From light with Apache License 2.0 5 votes vote down vote up
private ConnectingIOReactor ioReactor() throws Exception {
    // Create I/O reactor configuration
    Map<String, Object> asyncRestTemplateMap = (Map)configMap.get(ASYNC_REST_TEMPLATE);
    Map<String, Object> reactorMap = (Map)asyncRestTemplateMap.get(REACTOR);
    Integer ioThreadCount = (Integer)reactorMap.get(REACTOR_IO_THREAD_COUNT);
    IOReactorConfig.Builder builder = IOReactorConfig.custom();
    builder.setIoThreadCount(ioThreadCount == null? Runtime.getRuntime().availableProcessors(): ioThreadCount);
    Integer connectTimeout = (Integer)reactorMap.get(REACTOR_CONNECT_TIMEOUT);
    builder.setConnectTimeout(connectTimeout == null? DEFAULT_REACTOR_CONNECT_TIMEOUT: connectTimeout);
    Integer soTimeout = (Integer)reactorMap.get(REACTOR_SO_TIMEOUT);
    builder.setSoTimeout(soTimeout == null? DEFAULT_REACTOR_SO_TIMEOUT: soTimeout);
    IOReactorConfig ioReactorConfig = builder.build();
    return new DefaultConnectingIOReactor(ioReactorConfig);
}
 
Example #14
Source File: ExtendedJestClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
ConnectingIOReactor createIOReactor() throws IOReactorException {
    return new DefaultConnectingIOReactor(createIoReactorConfig());
}
 
Example #15
Source File: HttpClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
ConnectingIOReactor createIOReactor() throws IOReactorException {
    return new DefaultConnectingIOReactor(createIOReactorConfig());
}
 
Example #16
Source File: AsyncClientEvictExpiredConnections.java    From yunpian-java-sdk with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
    PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
    cm.setMaxTotal(100);
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setConnectionManager(cm).build();
    try {
        httpclient.start();

        // create an array of URIs to perform GETs on
        String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core-ga/",
                "http://hc.apache.org/httpcomponents-client-ga/", };

        IdleConnectionEvictor connEvictor = new IdleConnectionEvictor(cm);
        connEvictor.start();

        final CountDownLatch latch = new CountDownLatch(urisToGet.length);
        for (final String uri : urisToGet) {
            final HttpGet httpget = new HttpGet(uri);
            httpclient.execute(httpget, new FutureCallback<HttpResponse>() {

                @Override
                public void completed(final HttpResponse response) {
                    latch.countDown();
                    System.out.println(httpget.getRequestLine() + "->" + response.getStatusLine());
                }

                @Override
                public void failed(final Exception ex) {
                    latch.countDown();
                    System.out.println(httpget.getRequestLine() + "->" + ex);
                }

                @Override
                public void cancelled() {
                    latch.countDown();
                    System.out.println(httpget.getRequestLine() + " cancelled");
                }

            });
        }
        latch.await();

        // Sleep 10 sec and let the connection evictor do its job
        Thread.sleep(20000);

        // Shut down the evictor thread
        connEvictor.shutdown();
        connEvictor.join();

    } finally {
        httpclient.close();
    }
}