当前位置: 首页>>代码示例>>Java>>正文


Java CloseableHttpAsyncClient类代码示例

本文整理汇总了Java中org.apache.http.impl.nio.client.CloseableHttpAsyncClient的典型用法代码示例。如果您正苦于以下问题:Java CloseableHttpAsyncClient类的具体用法?Java CloseableHttpAsyncClient怎么用?Java CloseableHttpAsyncClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


CloseableHttpAsyncClient类属于org.apache.http.impl.nio.client包,在下文中一共展示了CloseableHttpAsyncClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createHttpAsyncClient

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
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;
}
 
开发者ID:yunpian,项目名称:yunpian-java-sdk,代码行数:19,代码来源:YunpianClient.java

示例2: send

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
public void send(List<Map<Object, Object>> events, AsyncSuccessCallback<ProducedEventsResult> onSuccess, AsyncFailCallback onFail, AsyncCancelledCallback onCancel) throws IOException, InterruptedException {
    CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
    httpClient.start();

    String url = String.format("%s/%s/bulk-produce", this.endpoint, this.topicId);
    System.out.println(url);
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("Authorization", this.writeApiKey);
    httpPost.addHeader("Content-type", this.format);

    String jsonString = MAPPER.writeValueAsString(events);
    HttpEntity entity = new ByteArrayEntity(jsonString.getBytes());
    httpPost.setEntity(entity);

    ResponseParser<ProducedEventsResult> parser = new BulkProduceEventsParser();
    AsyncCallback cb = new AsyncCallback(httpClient, parser, MAPPER, onSuccess, onFail, onCancel);
    httpClient.execute(httpPost, cb);
}
 
开发者ID:PyroclastIO,项目名称:pyroclast-java,代码行数:19,代码来源:PyroclastProducer.java

示例3: FiberApacheHttpClientRequestExecutor

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
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;
}
 
开发者ID:pinterest,项目名称:jbender,代码行数:19,代码来源:FiberApacheHttpClientRequestExecutor.java

示例4: before

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Before
public void before() throws Exception {
  config = new ESPluginConfig(new Config(false));
  client = mock(CloseableHttpAsyncClient.class);
  es = mock(ElasticSearch.class);
  meta = new UIDMeta(UniqueIdType.METRIC, new byte[] { 1 }, "sys.cpu.user");
  index = config.getString("tsd.search.elasticsearch.index");
  doc_type = config.getString("tsd.search.elasticsearch.uidmeta_type");
  
  when(es.httpClient()).thenReturn(client);
  when(es.host()).thenReturn(HOST);
  when(es.index()).thenReturn(index);
  when(es.config()).thenReturn(config);
  when(client.execute(any(HttpUriRequest.class), 
      any(FutureCallback.class)))
    .thenAnswer(new Answer<Void>() {
      @Override
      public Void answer(InvocationOnMock invocation) throws Throwable {
        request = (HttpUriRequest) invocation.getArguments()[0];
        cb = (FutureCallback<HttpResponse>) invocation.getArguments()[1];
        return null;
      }
    });
}
 
开发者ID:OpenTSDB,项目名称:opentsdb-elasticsearch,代码行数:26,代码来源:TestDefaultUIDMetaSchema.java

示例5: sourceWithMockedClient

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
private RemoteScrollableHitSource sourceWithMockedClient(boolean mockRemoteVersion, CloseableHttpAsyncClient httpClient)
        throws Exception {
    HttpAsyncClientBuilder clientBuilder = mock(HttpAsyncClientBuilder.class);
    when(clientBuilder.build()).thenReturn(httpClient);

    RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200))
            .setHttpClientConfigCallback(httpClientBuilder -> clientBuilder).build();

    TestRemoteScrollableHitSource hitSource = new TestRemoteScrollableHitSource(restClient) {
        @Override
        void lookupRemoteVersion(Consumer<Version> onVersion) {
            if (mockRemoteVersion) {
                onVersion.accept(Version.CURRENT);
            } else {
                super.lookupRemoteVersion(onVersion);
            }
        }
    };
    if (mockRemoteVersion) {
        hitSource.remoteVersion = Version.CURRENT;
    }
    return hitSource;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:24,代码来源:RemoteScrollableHitSourceTests.java

示例6: createHttpClient

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
private CloseableHttpAsyncClient createHttpClient() {
    //default timeouts are all infinite
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
            .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS)
            .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT_MILLIS)
            .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS);
    if (requestConfigCallback != null) {
        requestConfigBuilder = requestConfigCallback.customizeRequestConfig(requestConfigBuilder);
    }

    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfigBuilder.build())
            //default settings for connection pooling may be too constraining
            .setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE).setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL);
    if (httpClientConfigCallback != null) {
        httpClientBuilder = httpClientConfigCallback.customizeHttpClient(httpClientBuilder);
    }
    return httpClientBuilder.build();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:19,代码来源:RestClientBuilder.java

示例7: generateClient

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
@Override
public CloseableHttpAsyncClient generateClient ()
{
   CredentialsProvider credsProvider = new BasicCredentialsProvider();
   credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
           new UsernamePasswordCredentials(serviceUser, servicePass));
   RequestConfig rqconf = RequestConfig.custom()
         .setCookieSpec(CookieSpecs.DEFAULT)
         .setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
         .setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
         .setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
         .build();
   CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
         .setDefaultCredentialsProvider (credsProvider)
         .setDefaultRequestConfig(rqconf)
         .build ();
   res.start ();
   return res;
}
 
开发者ID:SentinelDataHub,项目名称:dhus-core,代码行数:20,代码来源:ODataProductSynchronizer.java

示例8: generateClient

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
@Override
public CloseableHttpAsyncClient generateClient ()
{
   CredentialsProvider credsProvider = new BasicCredentialsProvider();
   credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
            new UsernamePasswordCredentials(username, password));
   RequestConfig rqconf = RequestConfig.custom()
         .setCookieSpec(CookieSpecs.DEFAULT)
         .setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
         .setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
         .setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
         .build();
   CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
         .setDefaultCredentialsProvider (credsProvider)
         .setDefaultRequestConfig(rqconf)
         .build ();
   res.start ();
   return res;
}
 
开发者ID:SentinelDataHub,项目名称:dhus-core,代码行数:20,代码来源:ODataClient.java

示例9: createHttpClient

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
public static HttpClient createHttpClient(HiTSDBConfig config) throws HttpClientInitException {
	Objects.requireNonNull(config);
	
	// 创建 ConnectingIOReactor
	ConnectingIOReactor ioReactor = initIOReactorConfig(config);
	
	// 创建链接管理器
	final PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
	
	// 创建令牌管理器
	semaphoreManager = createSemaphoreManager(config);
	
	// 创建HttpAsyncClient
	CloseableHttpAsyncClient httpAsyncClient = createPoolingHttpClient(config,cm,semaphoreManager);
	
	// 组合生产HttpClientImpl
	HttpClient httpClientImpl = new HttpClient(config,httpAsyncClient,semaphoreManager);
	
	return httpClientImpl;
}
 
开发者ID:aliyun,项目名称:HiTSDB-Client,代码行数:21,代码来源:HttpClientFactory.java

示例10: RestClient

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
RestClient(String baseUrl,
           ObjectMapper objectMapper,
           Map<String, Object> defaultHeaders,
           Function<String, String> urlTransformer,
           PoolingNHttpClientConnectionManager asyncConnectionManager,
           PoolingHttpClientConnectionManager syncConnectionManager,
           CloseableHttpAsyncClient asyncClient,
           CloseableHttpClient syncClient) {
    this.objectMapper = objectMapper;
    this.baseUrl = baseUrl;
    this.urlTransformer = urlTransformer;
    this.asyncConnectionManager = asyncConnectionManager;
    this.syncConnectionManager = syncConnectionManager;
    this.asyncClient = asyncClient;
    this.syncClient = syncClient;
    this.defaultHeaders.putAll(defaultHeaders);
    this.id = UUID.randomUUID().toString().substring(0, 8);
}
 
开发者ID:josueeduardo,项目名称:rest-client,代码行数:19,代码来源:RestClient.java

示例11: main

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    try {
        httpclient.start();
        HttpHost proxy = new HttpHost("someproxy", 8080);
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        HttpGet request = new HttpGet("https://issues.apache.org/");
        request.setConfig(config);
        Future<HttpResponse> future = httpclient.execute(request, null);
        HttpResponse response = future.get();
        System.out.println("Response: " + response.getStatusLine());
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
}
 
开发者ID:yunpian,项目名称:yunpian-java-sdk,代码行数:17,代码来源:AsyncClientExecuteProxy.java

示例12: main

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    try {
        httpclient.start();
        Future<Boolean> future = httpclient.execute(HttpAsyncMethods.createGet("http://localhost:8080/"),
                new MyResponseConsumer(), null);
        Boolean result = future.get();
        if (result != null && result.booleanValue()) {
            System.out.println("Request successfully executed");
        } else {
            System.out.println("Request failed");
        }
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
    System.out.println("Done");
}
 
开发者ID:yunpian,项目名称:yunpian-java-sdk,代码行数:19,代码来源:AsyncClientHttpExchangeStreaming.java

示例13: main

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("localhost", 443),
            new UsernamePasswordCredentials("username", "password"));
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider)
            .build();
    try {
        HttpGet httpget = new HttpGet("http://localhost/");

        System.out.println("Executing request " + httpget.getRequestLine());
        Future<HttpResponse> future = httpclient.execute(httpget, null);
        HttpResponse response = future.get();
        System.out.println("Response: " + response.getStatusLine());
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
}
 
开发者ID:yunpian,项目名称:yunpian-java-sdk,代码行数:19,代码来源:AsyncClientAuthentication.java

示例14: main

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("someproxy", 8080),
            new UsernamePasswordCredentials("username", "password"));
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider)
            .build();
    try {
        httpclient.start();
        HttpHost proxy = new HttpHost("someproxy", 8080);
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        HttpGet httpget = new HttpGet("https://issues.apache.org/");
        httpget.setConfig(config);
        Future<HttpResponse> future = httpclient.execute(httpget, null);
        HttpResponse response = future.get();
        System.out.println("Response: " + response.getStatusLine());
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
}
 
开发者ID:yunpian,项目名称:yunpian-java-sdk,代码行数:21,代码来源:AsyncClientProxyAuthentication.java

示例15: submit

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; //导入依赖的package包/类
@Override
public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception {
    ResponseEntity<String> stringResponseEntity = null;
    try (CloseableHttpAsyncClient hc = createCloseableHttpAsyncClient()) {
        for (int i = 0; i < requestOptions.getCount(); i++) {
            final HttpHeaders headers = new HttpHeaders();
            for (Map.Entry<String, String> e : requestOptions.getHeaderMap().entrySet()) {
                headers.put(e.getKey(), Collections.singletonList(e.getValue()));
            }

            final HttpEntity<Void> requestEntity = new HttpEntity<>(headers);

            AsyncRestTemplate template = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory(hc));
            final ListenableFuture<ResponseEntity<String>> exchange = template.exchange(requestOptions.getUrl(), HttpMethod.GET, requestEntity, String.class);
            stringResponseEntity = exchange.get();
            System.out.println(stringResponseEntity.getBody());

        }
        return stringResponseEntity;
    }
}
 
开发者ID:eeichinger,项目名称:jcurl,代码行数:22,代码来源:HCNIOEngine.java


注:本文中的org.apache.http.impl.nio.client.CloseableHttpAsyncClient类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。