类org.apache.http.pool.PoolStats源码实例Demo

下面列出了怎么用org.apache.http.pool.PoolStats的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: blog-sample   文件: HttpClientUtils.java
public static Map<HttpRoute, PoolStats> getConnManagerStats() {
    if (connManager != null) {
        Set<HttpRoute> routeSet = connManager.getRoutes();
        if (routeSet != null && !routeSet.isEmpty()) {
            Map<HttpRoute, PoolStats> routeStatsMap = new HashMap<HttpRoute, PoolStats>();
            for (HttpRoute route : routeSet) {
                PoolStats stats = connManager.getStats(route);
                routeStatsMap.put(route, stats);
            }
            return routeStatsMap;
        }
    }
    return null;
}
 
源代码2 项目: hugegraph-common   文件: AbstractRestClient.java
public AbstractRestClient(String url, ClientConfig config) {
    Client client = null;
    Object protocol = config.getProperty("protocol");
    if (protocol != null && protocol.equals("https")) {
        client = wrapTrustConfig(url, config);
    } else {
        client = ClientBuilder.newClient(config);
    }
    this.client = client;
    this.client.register(GZipEncoder.class);
    this.target = this.client.target(url);
    this.pool = (PoolingHttpClientConnectionManager) config.getProperty(
                ApacheClientProperties.CONNECTION_MANAGER);
    if (this.pool != null) {
        this.cleanExecutor = ExecutorUtil.newScheduledThreadPool(
                                          "conn-clean-worker-%d");
        Number idleTimeProp = (Number) config.getProperty("idleTime");
        final long idleTime = idleTimeProp == null ?
                              IDLE_TIME : idleTimeProp.longValue();
        final long checkPeriod = idleTime / 2L;
        this.cleanExecutor.scheduleWithFixedDelay(() -> {
            PoolStats stats = this.pool.getTotalStats();
            int using = stats.getLeased() + stats.getPending();
            if (using > 0) {
                // Do clean only when all connections are idle
                return;
            }
            // Release connections when all clients are inactive
            this.pool.closeIdleConnections(idleTime, TimeUnit.MILLISECONDS);
            this.pool.closeExpiredConnections();
        }, checkPeriod, checkPeriod, TimeUnit.MILLISECONDS);
    }
}
 
源代码3 项目: paas   文件: HttpClientUtils.java
public static Map<HttpRoute, PoolStats> getConnManagerStats() {
    if (connManager != null) {
        Set<HttpRoute> routeSet = connManager.getRoutes();
        if (routeSet != null && !routeSet.isEmpty()) {
            Map<HttpRoute, PoolStats> routeStatsMap = new HashMap<HttpRoute, PoolStats>();
            for (HttpRoute route : routeSet) {
                PoolStats stats = connManager.getStats(route);
                routeStatsMap.put(route, stats);
            }
            return routeStatsMap;
        }
    }
    return null;
}
 
@Test
void totalMax() {
    PoolStats poolStats = mock(PoolStats.class);
    when(poolStats.getMax()).thenReturn(13);
    when(connPoolControl.getTotalStats()).thenReturn(poolStats);
    assertThat(registry.get("httpcomponents.httpclient.pool.total.max")
        .tags("httpclient", "test")
        .gauge().value()).isEqualTo(13.0);
}
 
@Test
void totalAvailable() {
    PoolStats poolStats = mock(PoolStats.class);
    when(poolStats.getAvailable()).thenReturn(17);
    when(connPoolControl.getTotalStats()).thenReturn(poolStats);
    assertThat(registry.get("httpcomponents.httpclient.pool.total.connections")
        .tags("httpclient", "test", "state", "available")
        .gauge().value()).isEqualTo(17.0);
}
 
@Test
void totalLeased() {
    PoolStats poolStats = mock(PoolStats.class);
    when(poolStats.getLeased()).thenReturn(23);
    when(connPoolControl.getTotalStats()).thenReturn(poolStats);
    assertThat(registry.get("httpcomponents.httpclient.pool.total.connections")
        .tags("httpclient", "test", "state", "leased")
        .gauge().value()).isEqualTo(23.0);
}
 
@Test
void totalPending() {
    PoolStats poolStats = mock(PoolStats.class);
    when(poolStats.getPending()).thenReturn(37);
    when(connPoolControl.getTotalStats()).thenReturn(poolStats);
    assertThat(registry.get("httpcomponents.httpclient.pool.total.pending")
        .tags("httpclient", "test")
        .gauge().value()).isEqualTo(37.0);
}
 
源代码8 项目: ibm-cos-sdk-java   文件: AmazonHttpClient.java
/**
 * Captures the connection pool metrics.
 */
private void captureConnectionPoolMetrics() {
    if (awsRequestMetrics.isEnabled() &&
        httpClient.getHttpClientConnectionManager() instanceof
                ConnPoolControl<?>) {
        final PoolStats stats = ((ConnPoolControl<?>) httpClient
                .getHttpClientConnectionManager()).getTotalStats();

        awsRequestMetrics
                .withCounter(HttpClientPoolAvailableCount, stats.getAvailable())
                .withCounter(HttpClientPoolLeasedCount, stats.getLeased())
                .withCounter(HttpClientPoolPendingCount, stats.getPending());
    }

}
 
源代码9 项目: riptide   文件: HttpConnectionPoolMetrics.java
@Override
public void bindTo(final MeterRegistry registry) {
    gauge("available", PoolStats::getAvailable)
            .description("The number idle connections")
            .baseUnit(CONNECTIONS)
            .register(registry);

    gauge("leased", PoolStats::getLeased)
            .description("The number of connections that are actively executing requests")
            .baseUnit(CONNECTIONS)
            .register(registry);

    gauge("total", stats -> stats.getAvailable() + stats.getLeased())
            .description("The number of connections that are currently in the pool")
            .baseUnit(CONNECTIONS)
            .register(registry);

    gauge("min", stats -> 0)
            .description("The minimum number of connections in the pool")
            .baseUnit(CONNECTIONS)
            .register(registry);

    gauge("max", PoolStats::getMax)
            .description("The maximum number of connections in the pool")
            .baseUnit(CONNECTIONS)
            .register(registry);

    gauge("queued", PoolStats::getPending)
            .description("The number of queued connection lease requests")
            .baseUnit(REQUESTS)
            .register(registry);
}
 
源代码10 项目: riptide   文件: HttpConnectionPoolMetrics.java
private Gauge.Builder<Supplier<Number>> gauge(
        final String name,
        final ToIntFunction<PoolStats> function) {

    return Gauge.builder(metricName + "." + name, () -> function.applyAsInt(stats.get()))
            .tags(defaultTags);
}
 
源代码11 项目: blog-sample   文件: HttpClientUtils.java
public static PoolStats getConnManagerTotalStats() {
    if (connManager != null) {
        return connManager.getTotalStats();
    }
    return null;
}
 
源代码12 项目: hugegraph-common   文件: RestClientTest.java
@Test
public void testCleanExecutor() throws Exception {
    // Modify IDLE_TIME 100ms to speed test
    int newIdleTime = 100;
    int newCheckPeriod = newIdleTime + 20;

    RestClient client = new RestClientImpl("/test", 1000, newIdleTime,
                                           10, 5, 200);

    PoolingHttpClientConnectionManager pool;
    pool = Whitebox.getInternalState(client, "pool");
    pool = Mockito.spy(pool);
    Whitebox.setInternalState(client, "pool", pool);
    HttpRoute route = new HttpRoute(HttpHost.create(
                                    "http://127.0.0.1:8080"));
    // Create a connection manually, it will be put into leased list
    HttpClientConnection conn = pool.requestConnection(route, null)
                                    .get(1L, TimeUnit.SECONDS);
    PoolStats stats = pool.getTotalStats();
    int usingConns = stats.getLeased() + stats.getPending();
    Assert.assertGte(1, usingConns);

    // Sleep more than two check periods for busy connection
    Thread.sleep(newCheckPeriod);
    Mockito.verify(pool, Mockito.never()).closeExpiredConnections();
    stats = pool.getTotalStats();
    usingConns = stats.getLeased() + stats.getPending();
    Assert.assertGte(1, usingConns);

    // The connection will be put into available list
    pool.releaseConnection(conn, null, 0, TimeUnit.SECONDS);

    stats = pool.getTotalStats();
    usingConns = stats.getLeased() + stats.getPending();
    Assert.assertEquals(0, usingConns);
    /*
     * Sleep more than two check periods for free connection,
     * ensure connection has been closed
     */
    Thread.sleep(newCheckPeriod);
    Mockito.verify(pool, Mockito.atLeastOnce())
           .closeExpiredConnections();
    Mockito.verify(pool, Mockito.atLeastOnce())
           .closeIdleConnections(newIdleTime, TimeUnit.MILLISECONDS);
}
 
源代码13 项目: paas   文件: HttpClientUtils.java
public static PoolStats getConnManagerTotalStats() {
    if (connManager != null) {
        return connManager.getTotalStats();
    }
    return null;
}
 
源代码14 项目: lucene-solr   文件: HttpSolrClientConPoolTest.java
public void testLBClient() throws IOException, SolrServerException {
  
  PoolingHttpClientConnectionManager pool = HttpClientUtil.createPoolingConnectionManager();
  final HttpSolrClient client1 ;
  int threadCount = atLeast(2);
  final ExecutorService threads = ExecutorUtil.newMDCAwareFixedThreadPool(threadCount,
      new SolrNamedThreadFactory(getClass().getSimpleName()+"TestScheduler"));
  CloseableHttpClient httpClient = HttpClientUtil.createClient(new ModifiableSolrParams(), pool);
  try{
    final LBHttpSolrClient roundRobin = new LBHttpSolrClient.Builder().
              withBaseSolrUrl(fooUrl).
              withBaseSolrUrl(barUrl).
              withHttpClient(httpClient)
              .build();
    
    List<ConcurrentUpdateSolrClient> concurrentClients = Arrays.asList(
        new ConcurrentUpdateSolrClient.Builder(fooUrl)
        .withHttpClient(httpClient).withThreadCount(threadCount)
        .withQueueSize(10)
       .withExecutorService(threads).build(),
         new ConcurrentUpdateSolrClient.Builder(barUrl)
        .withHttpClient(httpClient).withThreadCount(threadCount)
        .withQueueSize(10)
       .withExecutorService(threads).build()); 
    
    for (int i=0; i<2; i++) {
      roundRobin.deleteByQuery("*:*");
    }
    
    for (int i=0; i<57; i++) {
      final SolrInputDocument doc = new SolrInputDocument("id", ""+i);
      if (random().nextBoolean()) {
        final ConcurrentUpdateSolrClient concurrentClient = concurrentClients.get(random().nextInt(concurrentClients.size()));
        concurrentClient.add(doc); // here we are testing that CUSC and plain clients reuse pool 
        concurrentClient.blockUntilFinished();
      } else {
        if (random().nextBoolean()) {
          roundRobin.add(doc);
        } else {
          final UpdateRequest updateRequest = new UpdateRequest();
          updateRequest.add(doc); // here we mimic CloudSolrClient impl
          final List<String> urls = Arrays.asList(fooUrl, barUrl);
          Collections.shuffle(urls, random());
          LBHttpSolrClient.Req req = new LBHttpSolrClient.Req(updateRequest, 
                  urls);
           roundRobin.request(req);
        }
      }
    }
    
    for (int i=0; i<2; i++) {
      roundRobin.commit();
    }
    int total=0;
    for (int i=0; i<2; i++) {
      total += roundRobin.query(new SolrQuery("*:*")).getResults().getNumFound();
    }
    assertEquals(57, total);
    PoolStats stats = pool.getTotalStats();
    //System.out.println("\n"+stats);
    assertEquals("expected number of connections shouldn't exceed number of endpoints" + stats, 
        2, stats.getAvailable());
  }finally {
    threads.shutdown();
    HttpClientUtil.close(httpClient);
  }
}
 
源代码15 项目: Poseidon   文件: HttpConnectionPool.java
/**
 * Get the statistics
 */
public String getStats() {
    PoolingClientConnectionManager cm = (PoolingClientConnectionManager) this.client.getConnectionManager();
    PoolStats stats = cm.getTotalStats();
    return "Connections: " + stats.toString() + " AvailableRequests: " + processQueue.availablePermits();
}
 
源代码16 项目: data-prep   文件: APIService.java
/**
 * @return the connection pool stats.
 */
protected PoolStats getConnectionStats() {
    return connectionManager.getTotalStats();
}
 
 类所在包
 类方法
 同包方法