Example usage for java.util.concurrent ScheduledThreadPoolExecutor getPoolSize

List of usage examples for java.util.concurrent ScheduledThreadPoolExecutor getPoolSize

Introduction

In this page you can find the example usage for java.util.concurrent ScheduledThreadPoolExecutor getPoolSize.

Prototype

public int getPoolSize() 

Source Link

Document

Returns the current number of threads in the pool.

Usage

From source file:com.ksc.http.timers.ClientExecutionAndRequestTimerTestUtils.java

/**
 * Waits until a little after the thread pools keep alive time and then asserts that all thre
 * /*from   ww  w  .  j  av  a2 s  .c om*/
 * @param timerExecutor
 *            Executor used by timer implementation
 * @throws InterruptedException
 */
public static void assertCoreThreadsShutDownAfterBeingIdle(ScheduledThreadPoolExecutor timerExecutor) {
    try {
        Thread.sleep(timerExecutor.getKeepAliveTime(TimeUnit.MILLISECONDS) + 1000);
    } catch (InterruptedException ignored) {
    }
    assertEquals(0, timerExecutor.getPoolSize());
}

From source file:com.amazonaws.http.AmazonHttpClientRequestTimeoutTest.java

@Test
public void testRequestTimerCancelledTask() throws IOException, InterruptedException {
    ClientConfiguration config = new ClientConfiguration().withRequestTimeout(5 * 1000).withMaxErrorRetry(0);
    HttpClientFactory httpClientFactory = new HttpClientFactory();
    HttpClient rawHttpClient = spy(httpClientFactory.createHttpClient(config));

    HttpResponseProxy responseProxy = createHttpResponseProxySpy();
    doReturn(responseProxy).when(rawHttpClient).execute(any(HttpRequestBase.class), any(HttpContext.class));

    String localhostEndpoint = "http://localhost:0";
    Request<?> request = new EmptyHttpRequest(localhostEndpoint, HttpMethodName.GET);

    AmazonHttpClient httpClient = new AmazonHttpClient(config, rawHttpClient, null);

    try {/*www.ja  va  2  s . com*/
        httpClient.execute(request, new NullResponseHandler(), new NullErrorResponseHandler(),
                new ExecutionContext());
        Assert.fail("Should have been unable to unmarshall the response!");
    } catch (AmazonClientException e) {
        Assert.assertTrue(e.getCause() instanceof RuntimeException);
        RuntimeException re = (RuntimeException) e.getCause();
        Assert.assertTrue(re.getMessage().contains("Unable to unmarshall response"));
    }

    /* Verify the response was buffered when enforcing the request timeout. */
    verify(responseProxy).setEntity(any(BufferedHttpEntity.class));

    /* Verify cancelled tasks are removed on demand and the core threads die out after the keep alive time. */
    ScheduledThreadPoolExecutor httpRequestTimer = httpClient.getHttpRequestTimer().getExecutor();
    Assert.assertEquals(0, httpRequestTimer.getCompletedTaskCount());
    Assert.assertEquals(0, httpRequestTimer.getQueue().size());
    Assert.assertEquals(1, httpRequestTimer.getPoolSize());
    Thread.sleep(httpRequestTimer.getKeepAliveTime(TimeUnit.MILLISECONDS) + 1000);
    Assert.assertEquals(0, httpRequestTimer.getPoolSize());

    httpClient.shutdown();
}

From source file:com.ksc.http.timers.client.MockedClientTests.java

@Ignore
@Test/*from  w w w  . ja va2s .c  o m*/
public void clientExecutionTimeoutEnabled_RequestCompletesWithinTimeout_TaskCanceledAndEntityBuffered()
        throws Exception {
    ClientConfiguration config = new ClientConfiguration().withClientExecutionTimeout(CLIENT_EXECUTION_TIMEOUT)
            .withMaxErrorRetry(0);
    ConnectionManagerAwareHttpClient rawHttpClient = createRawHttpClientSpy(config);

    HttpResponseProxy responseProxy = createHttpResponseProxySpy();
    doReturn(responseProxy).when(rawHttpClient).execute(any(HttpRequestBase.class), any(HttpContext.class));

    httpClient = new KSCHttpClient(config, rawHttpClient, null);

    try {
        execute(httpClient, createMockGetRequest());
        fail("Exception expected");
    } catch (KscClientException e) {
        NullResponseHandler.assertIsUnmarshallingException(e);
    }

    assertResponseIsBuffered(responseProxy);
    ScheduledThreadPoolExecutor requestTimerExecutor = httpClient.getClientExecutionTimer().getExecutor();
    assertTimerNeverTriggered(requestTimerExecutor);
    assertCanceledTasksRemoved(requestTimerExecutor);
    // Core threads should be spun up on demand. Since only one task was submitted only one
    // thread should exist
    assertEquals(1, requestTimerExecutor.getPoolSize());
    assertCoreThreadsShutDownAfterBeingIdle(requestTimerExecutor);
}

From source file:com.ksc.http.timers.request.MockedClientTests.java

@Test
public void requestTimeoutEnabled_RequestCompletesWithinTimeout_TaskCanceledAndEntityBuffered()
        throws Exception {
    ClientConfiguration config = new ClientConfiguration().withRequestTimeout(5 * 1000).withMaxErrorRetry(0);
    ConnectionManagerAwareHttpClient rawHttpClient = createRawHttpClientSpy(config);

    HttpResponseProxy responseProxy = createHttpResponseProxySpy();
    doReturn(responseProxy).when(rawHttpClient).execute(any(HttpRequestBase.class), any(HttpContext.class));

    httpClient = new KSCHttpClient(config, rawHttpClient, null);

    try {/* w w  w. ja va2 s.  c  o  m*/
        execute(httpClient, createMockGetRequest());
        fail("Exception expected");
    } catch (KscClientException e) {
        NullResponseHandler.assertIsUnmarshallingException(e);
    }

    assertResponseIsBuffered(responseProxy);
    ScheduledThreadPoolExecutor requestTimerExecutor = httpClient.getHttpRequestTimer().getExecutor();
    assertTimerNeverTriggered(requestTimerExecutor);
    assertCanceledTasksRemoved(requestTimerExecutor);
    // Core threads should be spun up on demand. Since only one task was submitted only one
    // thread should exist
    assertEquals(1, requestTimerExecutor.getPoolSize());
    assertCoreThreadsShutDownAfterBeingIdle(requestTimerExecutor);
}

From source file:org.opendaylight.genius.utils.batching.ResourceBatchingManager.java

public void registerBatchableResource(String resourceType, final BlockingQueue<ActionableResource> resQueue,
        final ResourceHandler resHandler) {
    Preconditions.checkNotNull(resQueue, "ResourceQueue to use for batching cannot not be null.");
    Preconditions.checkNotNull(resHandler, "ResourceHandler cannot not be null.");
    if (resourceHandlerMapper.contains(resourceType)) {
        throw new RuntimeException("Resource type already registered");
    }//  w ww .  ja va  2 s  .  c o  m
    resourceHandlerMapper.put(resourceType, new ImmutablePair<>(resQueue, resHandler));
    ScheduledThreadPoolExecutor resDelegatorService = (ScheduledThreadPoolExecutor) Executors
            .newScheduledThreadPool(1, ThreadFactoryProvider.builder().namePrefix("ResourceBatchingManager")
                    .logger(LOG).build().get());
    resourceBatchingThreadMapper.put(resourceType, resDelegatorService);
    LOG.info("Registered resourceType {} with batchSize {} and batchInterval {}", resourceType,
            resHandler.getBatchSize(), resHandler.getBatchInterval());
    if (resDelegatorService.getPoolSize() == 0) {
        resDelegatorService.scheduleWithFixedDelay(new Batcher(resourceType), INITIAL_DELAY,
                resHandler.getBatchInterval(), TIME_UNIT);
    }
}