Example usage for java.util.concurrent ExecutorService shutdown

List of usage examples for java.util.concurrent ExecutorService shutdown

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService shutdown.

Prototype

void shutdown();

Source Link

Document

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

Usage

From source file:com.espertech.esper.multithread.TestMTDeterminismInsertInto.java

private void tryChainedCountSum(int numThreads, int numEvents) throws Exception {
    Configuration config = SupportConfigFactory.getConfiguration();
    // This should fail all test in this class
    // config.getEngineDefaults().getThreading().setInsertIntoDispatchPreserveOrder(false);

    EPServiceProvider engine = EPServiceProviderManager.getDefaultProvider(config);
    engine.initialize();/*from  ww w .  ja v  a 2 s  .c om*/

    // setup statements
    EPStatement stmtInsertOne = engine.getEPAdministrator()
            .createEPL("insert into MyStreamOne select count(*) as cnt from " + SupportBean.class.getName());
    EPStatement stmtInsertTwo = engine.getEPAdministrator()
            .createEPL("insert into MyStreamTwo select sum(cnt) as mysum from MyStreamOne");
    EPStatement stmtInsertThree = engine.getEPAdministrator().createEPL("select * from MyStreamTwo");
    SupportUpdateListener listener = new SupportUpdateListener();
    stmtInsertThree.addListener(listener);

    // execute
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future future[] = new Future[numThreads];
    ReentrantReadWriteLock sharedStartLock = new ReentrantReadWriteLock();
    sharedStartLock.writeLock().lock();
    for (int i = 0; i < numThreads; i++) {
        future[i] = threadPool.submit(
                new SendEventRWLockCallable(i, sharedStartLock, engine, new GeneratorIterator(numEvents)));
    }
    Thread.sleep(100);
    sharedStartLock.writeLock().unlock();

    threadPool.shutdown();
    threadPool.awaitTermination(10, TimeUnit.SECONDS);

    for (int i = 0; i < numThreads; i++) {
        assertTrue((Boolean) future[i].get());
    }

    // assert result
    EventBean newEvents[] = listener.getNewDataListFlattened();
    for (int i = 0; i < numEvents - 1; i++) {
        long expected = total(i + 1);
        assertEquals(expected, newEvents[i].get("mysum"));
    }

    stmtInsertOne.destroy();
    stmtInsertTwo.destroy();
    stmtInsertThree.destroy();
}

From source file:com.espertech.esper.multithread.TestMTDeterminismInsertInto.java

private void trySendCountFollowedBy(int numThreads, int numEvents,
        ConfigurationEngineDefaults.Threading.Locking locking) throws Exception {
    Configuration config = SupportConfigFactory.getConfiguration();
    config.getEngineDefaults().getThreading().setInsertIntoDispatchLocking(locking);
    config.getEngineDefaults().getThreading().setInsertIntoDispatchTimeout(5000); // 5 second timeout
    // This should fail all test in this class
    // config.getEngineDefaults().getThreading().setInsertIntoDispatchPreserveOrder(false);

    EPServiceProvider engine = EPServiceProviderManager.getDefaultProvider(config);
    engine.initialize();//from   w w  w. ja  va2 s  .c o  m

    // setup statements
    EPStatement stmtInsert = engine.getEPAdministrator()
            .createEPL("insert into MyStream select count(*) as cnt from " + SupportBean.class.getName());
    stmtInsert.addListener(new UpdateListener() {

        public void update(EventBean[] newEvents, EventBean[] oldEvents) {
            log.debug(".update cnt=" + newEvents[0].get("cnt"));
        }
    });

    SupportUpdateListener listeners[] = new SupportUpdateListener[numEvents];
    for (int i = 0; i < numEvents; i++) {
        String text = "select * from pattern [MyStream(cnt=" + (i + 1) + ") -> MyStream(cnt=" + (i + 2) + ")]";
        EPStatement stmt = engine.getEPAdministrator().createEPL(text);
        listeners[i] = new SupportUpdateListener();
        stmt.addListener(listeners[i]);
    }

    // execute
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future future[] = new Future[numThreads];
    ReentrantReadWriteLock sharedStartLock = new ReentrantReadWriteLock();
    sharedStartLock.writeLock().lock();
    for (int i = 0; i < numThreads; i++) {
        future[i] = threadPool.submit(
                new SendEventRWLockCallable(i, sharedStartLock, engine, new GeneratorIterator(numEvents)));
    }
    Thread.sleep(100);
    sharedStartLock.writeLock().unlock();

    threadPool.shutdown();
    threadPool.awaitTermination(10, TimeUnit.SECONDS);

    for (int i = 0; i < numThreads; i++) {
        assertTrue((Boolean) future[i].get());
    }

    // assert result
    for (int i = 0; i < numEvents - 1; i++) {
        assertEquals("Listener not invoked: #" + i, 1, listeners[i].getNewDataList().size());
    }
}

From source file:net.mindengine.galen.GalenMain.java

private void runSuitesInThreads(List<GalenSuite> suites, int parallelSuites, final CompleteListener listener) {
    ExecutorService executor = Executors.newFixedThreadPool(parallelSuites);
    for (final GalenSuite suite : suites) {
        Runnable thread = new Runnable() {
            @Override//from w  w w  .jav  a  2s .  co m
            public void run() {
                GalenSuiteRunner suiteRunner = new GalenSuiteRunner();
                suiteRunner.setSuiteListener(listener);
                suiteRunner.setValidationListener(listener);
                suiteRunner.runSuite(suite);
            }
        };

        executor.execute(thread);
    }
    executor.shutdown();
    while (!executor.isTerminated()) {
    }
}

From source file:com.blacklocus.jres.request.index.JresUpdateDocumentTest.java

@Test(expected = ExecutionException.class)
public void testRetryOnConflictExpectError() throws InterruptedException, ExecutionException {
    final String index = "JresUpdateDocumentTest.testRetryOnConflictExpectError".toLowerCase();
    final String type = "test";
    final String id = "warzone";

    final AtomicReference<String> error = new AtomicReference<String>();
    final int numThreads = 16, numIterations = 100;

    ExecutorService x = Executors.newFixedThreadPool(numThreads);
    List<Future<?>> futures = new ArrayList<Future<?>>(numThreads);
    for (int i = 0; i < numThreads; i++) {
        futures.add(x.submit(new Callable<Void>() {
            @Override/*from  w w  w . j av a 2 s  .co  m*/
            public Void call() throws Exception {
                for (int j = 0; j < numIterations; j++) {
                    jres.quest(new JresUpdateDocument(index, type, id, ImmutableMap.of("value", 0)));
                }
                return null;
            }
        }));
    }
    x.shutdown();
    x.awaitTermination(1, TimeUnit.MINUTES);

    for (Future<?> future : futures) {
        // expecting a conflict exception from ElasticSearch
        future.get();
    }
}

From source file:org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClientTest.java

@Test
public void testConcurrentCollectionUpdate() throws Exception {

    int cussThreadCount = 2;
    int cussQueueSize = 100;
    int numDocs = 100;
    int numRunnables = 5;
    int expected = numDocs * numRunnables;

    try (ConcurrentUpdateSolrClient concurrentClient = new ConcurrentUpdateSolrClient(
            jetty.getBaseUrl().toString(), cussQueueSize, cussThreadCount)) {
        concurrentClient.setPollQueueTime(0);

        // ensure it doesn't block where there's nothing to do yet
        concurrentClient.blockUntilFinished();

        // Delete all existing documents.
        concurrentClient.deleteByQuery("collection1", "*:*");

        int poolSize = 5;
        ExecutorService threadPool = ExecutorUtil.newMDCAwareFixedThreadPool(poolSize,
                new SolrjNamedThreadFactory("testCUSS"));

        for (int r = 0; r < numRunnables; r++)
            threadPool//  w  ww. j a v a2 s .  c o  m
                    .execute(new SendDocsRunnable(String.valueOf(r), numDocs, concurrentClient, "collection1"));

        // ensure all docs are sent
        threadPool.awaitTermination(5, TimeUnit.SECONDS);
        threadPool.shutdown();

        concurrentClient.commit("collection1");

        assertEquals(expected,
                concurrentClient.query("collection1", new SolrQuery("*:*")).getResults().getNumFound());

        // wait until all requests are processed by CUSS 
        concurrentClient.blockUntilFinished();
        concurrentClient.shutdownNow();
    }

    try (ConcurrentUpdateSolrClient concurrentClient = new ConcurrentUpdateSolrClient(
            jetty.getBaseUrl().toString() + "/collection1", cussQueueSize, cussThreadCount)) {
        assertEquals(expected, concurrentClient.query(new SolrQuery("*:*")).getResults().getNumFound());
    }

}

From source file:org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClientTest.java

@Test
public void testConcurrentUpdate() throws Exception {
    TestServlet.clear();//from   ww  w  .ja  v  a2s.  c o m

    String serverUrl = jetty.getBaseUrl().toString() + "/cuss/foo";

    int cussThreadCount = 2;
    int cussQueueSize = 100;

    // for tracking callbacks from CUSS
    final AtomicInteger successCounter = new AtomicInteger(0);
    final AtomicInteger errorCounter = new AtomicInteger(0);
    final StringBuilder errors = new StringBuilder();

    @SuppressWarnings("serial")
    ConcurrentUpdateSolrClient concurrentClient = new OutcomeCountingConcurrentUpdateSolrClient(serverUrl,
            cussQueueSize, cussThreadCount, successCounter, errorCounter, errors);

    concurrentClient.setPollQueueTime(0);

    // ensure it doesn't block where there's nothing to do yet
    concurrentClient.blockUntilFinished();

    int poolSize = 5;
    ExecutorService threadPool = ExecutorUtil.newMDCAwareFixedThreadPool(poolSize,
            new SolrjNamedThreadFactory("testCUSS"));

    int numDocs = 100;
    int numRunnables = 5;
    for (int r = 0; r < numRunnables; r++)
        threadPool.execute(new SendDocsRunnable(String.valueOf(r), numDocs, concurrentClient));

    // ensure all docs are sent
    threadPool.awaitTermination(5, TimeUnit.SECONDS);
    threadPool.shutdown();

    // wait until all requests are processed by CUSS 
    concurrentClient.blockUntilFinished();
    concurrentClient.shutdownNow();

    assertEquals("post", TestServlet.lastMethod);

    // expect all requests to be successful
    int expectedSuccesses = TestServlet.numReqsRcvd.get();
    assertTrue(expectedSuccesses > 0); // at least one request must have been sent

    assertTrue("Expected no errors but got " + errorCounter.get() + ", due to: " + errors.toString(),
            errorCounter.get() == 0);
    assertTrue("Expected " + expectedSuccesses + " successes, but got " + successCounter.get(),
            successCounter.get() == expectedSuccesses);

    int expectedDocs = numDocs * numRunnables;
    assertTrue("Expected CUSS to send " + expectedDocs + " but got " + TestServlet.numDocsRcvd.get(),
            TestServlet.numDocsRcvd.get() == expectedDocs);
}

From source file:com.espertech.esper.multithread.TestMTDeterminismInsertInto.java

private void tryMultiInsertGroup(int numThreads, int numStatements, int numEvents) throws Exception {
    Configuration config = SupportConfigFactory.getConfiguration();
    // This should fail all test in this class
    // config.getEngineDefaults().getThreading().setInsertIntoDispatchPreserveOrder(false);

    EPServiceProvider engine = EPServiceProviderManager.getDefaultProvider(config);
    engine.initialize();/*from   ww w  .j  a  v a 2 s.c om*/

    // setup statements
    EPStatement[] insertIntoStmts = new EPStatement[numStatements];
    for (int i = 0; i < numStatements; i++) {
        insertIntoStmts[i] = engine.getEPAdministrator().createEPL("insert into MyStream select " + i
                + " as ident,count(*) as cnt from " + SupportBean.class.getName());
    }
    EPStatement stmtInsertTwo = engine.getEPAdministrator()
            .createEPL("select ident, sum(cnt) as mysum from MyStream group by ident");
    SupportUpdateListener listener = new SupportUpdateListener();
    stmtInsertTwo.addListener(listener);

    // execute
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future future[] = new Future[numThreads];
    ReentrantReadWriteLock sharedStartLock = new ReentrantReadWriteLock();
    sharedStartLock.writeLock().lock();
    for (int i = 0; i < numThreads; i++) {
        future[i] = threadPool.submit(
                new SendEventRWLockCallable(i, sharedStartLock, engine, new GeneratorIterator(numEvents)));
    }
    Thread.sleep(100);
    sharedStartLock.writeLock().unlock();

    threadPool.shutdown();
    threadPool.awaitTermination(10, TimeUnit.SECONDS);

    for (int i = 0; i < numThreads; i++) {
        assertTrue((Boolean) future[i].get());
    }

    // assert result
    EventBean newEvents[] = listener.getNewDataListFlattened();
    ArrayList resultsPerIdent[] = new ArrayList[numStatements];
    for (EventBean theEvent : newEvents) {
        int ident = (Integer) theEvent.get("ident");
        if (resultsPerIdent[ident] == null) {
            resultsPerIdent[ident] = new ArrayList();
        }
        long mysum = (Long) theEvent.get("mysum");
        resultsPerIdent[ident].add(mysum);
    }

    for (int statement = 0; statement < numStatements; statement++) {
        for (int i = 0; i < numEvents - 1; i++) {
            long expected = total(i + 1);
            assertEquals(expected, resultsPerIdent[statement].get(i));
        }
    }

    // destroy
    for (int i = 0; i < numStatements; i++) {
        insertIntoStmts[i].destroy();
    }
    stmtInsertTwo.destroy();
}

From source file:com.b2international.index.GroovyMemoryLeakTest.java

@Ignore
@Test//  w  w w  .ja  v  a2  s  . c  om
public void tryToGenerateMemoryLeak() throws Exception {
    final List<String> orderedItems = newArrayList();
    final Map<String, Data> documents = newHashMap();

    for (int i = 0; i < NUM_DOCS; i++) {
        String item = null;
        while (item == null || orderedItems.contains(item)) {
            item = RandomStringUtils.randomAlphabetic(10);
        }
        orderedItems.add(item);

        final Data data = new Data();
        data.setField1(item);
        data.setFloatField(100.0f - i);
        documents.put(Integer.toString(i), data);
    }

    indexDocuments(documents);

    ExecutorService executor = Executors.newFixedThreadPool(2);

    final Runnable theQuery = () -> {
        for (int i = 0; i < 10_000; i++) {
            final Query<Data> query = Query.select(Data.class)
                    .where(Expressions.scriptScore(Expressions.matchAll(), "floatField")).limit(NUM_DOCS)
                    .sortBy(SortBy.SCORE).build();
            search(query);
        }
    };

    // run 4 threads to simulate a bit higher load on the index
    executor.submit(theQuery, null);
    executor.submit(theQuery, null);
    executor.submit(theQuery, null);
    executor.submit(theQuery, null);

    executor.shutdown();
    // this won't pass at all, even if the fix is applied
    // the purpose of this test to detect and verify the GC via external monitoring thus it cannot be automated properly
    assertTrue(executor.awaitTermination(5, TimeUnit.MINUTES));
}

From source file:com.microsoft.aad.adal4jsample.BasicFilter.java

private AuthenticationResult getAccessToken(AuthorizationCode authorizationCode, String currentUri)
        throws Throwable {
    String authCode = authorizationCode.getValue();
    ClientCredential credential = new ClientCredential(clientId, clientSecret);
    AuthenticationContext context;/*www . jav  a  2  s  . c o m*/
    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        context = new AuthenticationContext(authority + tenant + "/", true, service);
        Future<AuthenticationResult> future = context.acquireTokenByAuthorizationCode(authCode,
                new URI(currentUri), credential, null);
        result = future.get();
    } catch (ExecutionException e) {
        throw e.getCause();
    } finally {
        service.shutdown();
    }

    if (result == null) {
        throw new ServiceUnavailableException("authentication result was null");
    }
    return result;
}

From source file:com.microsoft.azure.servicebus.samples.queueswithproxy.QueuesWithProxy.java

public void run(String connectionString) throws Exception {
    // Set the transport type to AmqpWithWebsockets
    ConnectionStringBuilder connStrBuilder = new ConnectionStringBuilder(connectionString, "BasicQueue");
    connStrBuilder.setTransportType(TransportType.AMQP_WEB_SOCKETS);

    // Create a QueueClient instance for receiving using the connection string builder
    // We set the receive mode to "PeekLock", meaning the message is delivered
    // under a lock and must be acknowledged ("completed") to be removed from the queue
    QueueClient receiveClient = new QueueClient(connStrBuilder, ReceiveMode.PEEKLOCK);
    // We are using single thread executor as we are only processing one message at a time
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    this.registerReceiver(receiveClient, executorService);

    // Create a QueueClient instance for sending and then asynchronously send messages.
    // Close the sender once the send operation is complete.
    QueueClient sendClient = new QueueClient(connStrBuilder, ReceiveMode.PEEKLOCK);
    this.sendMessagesAsync(sendClient).thenRunAsync(() -> sendClient.closeAsync());

    // wait for ENTER or 10 seconds elapsing
    waitForEnter(10);/*from www  .  j  a  v a 2s .  c  o  m*/

    // shut down receiver to close the receive loop
    receiveClient.close();
    executorService.shutdown();
}