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:forge.CardStorageReader.java

private void executeLoadTask(final Collection<CardRules> result, final List<Callable<List<CardRules>>> tasks,
        final CountDownLatch cdl) {
    try {/*from   w ww. ja  va 2  s. co m*/
        if (useThreadPool) {
            final ExecutorService executor = ThreadUtil.getComputingPool(0.5f);
            final List<Future<List<CardRules>>> parts = executor.invokeAll(tasks);
            executor.shutdown();
            cdl.await();
            for (final Future<List<CardRules>> pp : parts) {
                result.addAll(pp.get());
            }
        } else {
            for (final Callable<List<CardRules>> c : tasks) {
                result.addAll(c.call());
            }
        }
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    } catch (final Exception e) { // this clause comes from non-threaded branch
        throw new RuntimeException(e);
    }
}

From source file:com.linkedin.pinot.integration.tests.RealtimeClusterIntegrationTest.java

@BeforeClass
public void setUp() throws Exception {
    // Start ZK and Kafka
    startZk();/*from w w  w .  ja va 2  s .c  om*/
    kafkaStarters = KafkaStarterUtils.startServers(getKafkaBrokerCount(), KafkaStarterUtils.DEFAULT_KAFKA_PORT,
            KafkaStarterUtils.DEFAULT_ZK_STR, KafkaStarterUtils.getDefaultKafkaConfiguration());

    // Create Kafka topic
    createKafkaTopic(KAFKA_TOPIC, KafkaStarterUtils.DEFAULT_ZK_STR);

    // Start the Pinot cluster
    startController();
    startBroker();
    startServer();

    // Unpack data
    final List<File> avroFiles = unpackAvroData(_tmpDir, SEGMENT_COUNT);

    File schemaFile = getSchemaFile();

    // Load data into H2
    ExecutorService executor = Executors.newCachedThreadPool();
    setupH2AndInsertAvro(avroFiles, executor);

    // Initialize query generator
    setupQueryGenerator(avroFiles, executor);

    // Push data into the Kafka topic
    pushAvroIntoKafka(avroFiles, executor, KAFKA_TOPIC);

    // Wait for data push, query generator initialization and H2 load to complete
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.MINUTES);

    // Create Pinot table
    setUpTable("mytable", "DaysSinceEpoch", "daysSinceEpoch", KafkaStarterUtils.DEFAULT_ZK_STR, KAFKA_TOPIC,
            schemaFile, avroFiles.get(0));

    // Wait until the Pinot event count matches with the number of events in the Avro files
    long timeInFiveMinutes = System.currentTimeMillis() + 5 * 60 * 1000L;
    Statement statement = _connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    statement.execute("select count(*) from mytable");
    ResultSet rs = statement.getResultSet();
    rs.first();
    int h2RecordCount = rs.getInt(1);
    rs.close();

    waitForRecordCountToStabilizeToExpectedCount(h2RecordCount, timeInFiveMinutes);
}

From source file:biz.fstechnology.micro.common.jms.JmsServiceConnection.java

/**
 * @see biz.fstechnology.micro.common.DefaultServiceConnection#callAsync(biz.fstechnology.micro.common.Request,
 * java.util.function.Consumer)/*from  w w  w  . j  ava 2s.com*/
 */
@Override
public <T, U> void callAsync(Request<T> request, Consumer<Result<U>> callback) {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Result<U>> rawResponse = executor.submit(() -> call(request));
    executor.submit(() -> {
        try {
            callback.accept(rawResponse.get());
        } catch (Exception e) {
            e.printStackTrace();
            Result<U> result = new Result<>(e);
            callback.accept(result);
        }
    });
    executor.shutdown();
}

From source file:com.linkedin.pinot.integration.tests.UploadRefreshDeleteIntegrationTest.java

protected void generateAndUploadRandomSegment(String segmentName, int rowCount) throws Exception {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    Schema schema = new Schema.Parser().parse(
            new File(TestUtils.getFileFromResourceUrl(getClass().getClassLoader().getResource("dummy.avsc"))));
    GenericRecord record = new GenericData.Record(schema);
    GenericDatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(schema);
    DataFileWriter<GenericRecord> fileWriter = new DataFileWriter<GenericRecord>(datumWriter);
    File avroFile = new File(_tmpDir, segmentName + ".avro");
    fileWriter.create(schema, avroFile);

    for (int i = 0; i < rowCount; i++) {
        record.put(0, random.nextInt());
        fileWriter.append(record);/*from   w  ww  .j  a v a2 s  .  com*/
    }

    fileWriter.close();

    int segmentIndex = Integer.parseInt(segmentName.split("_")[1]);

    File segmentTarDir = new File(_tarsDir, segmentName);
    ensureDirectoryExistsAndIsEmpty(segmentTarDir);
    ExecutorService executor = MoreExecutors.sameThreadExecutor();
    buildSegmentsFromAvro(Collections.singletonList(avroFile), executor, segmentIndex,
            new File(_segmentsDir, segmentName), segmentTarDir, "mytable", false, null);
    executor.shutdown();
    executor.awaitTermination(1L, TimeUnit.MINUTES);

    for (String segmentFileName : segmentTarDir.list()) {
        File file = new File(segmentTarDir, segmentFileName);
        FileUploadUtils.sendFile("localhost", "8998", "segments", segmentFileName, new FileInputStream(file),
                file.length(), FileUploadUtils.SendFileMethod.POST);
    }

    avroFile.delete();
    FileUtils.deleteQuietly(segmentTarDir);
}

From source file:com.microsoft.azure.servicebus.samples.scheduledmessages.ScheduledMessages.java

public void run(String connectionString) throws Exception {

    QueueClient sendClient;//from www. j  ava 2  s  . c o  m
    QueueClient receiveClient;

    // Create a QueueClient instance 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
    receiveClient = new QueueClient(new ConnectionStringBuilder(connectionString, "BasicQueue"),
            ReceiveMode.PEEKLOCK);
    // We are using single thread executor as we are only processing one message at a time
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    this.initializeReceiver(receiveClient, executorService);

    sendClient = new QueueClient(new ConnectionStringBuilder(connectionString, "BasicQueue"),
            ReceiveMode.PEEKLOCK);
    this.sendMessagesAsync(sendClient).thenRunAsync(() -> sendClient.closeAsync());

    waitForEnter(150);

    receiveClient.close();
    executorService.shutdown();

}

From source file:com.jolbox.benchmark.BenchmarkTests.java

/**
 * Helper function./*from   w w w .j ava2 s .c om*/
 *
 * @param threads
 * @param cpds
 * @param workDelay
 * @param doPreparedStatement 
 * @return time taken
 * @throws InterruptedException
 */
public static long startThreadTest(int threads, DataSource cpds, int workDelay, boolean doPreparedStatement)
        throws InterruptedException {
    CountDownLatch startSignal = new CountDownLatch(1);
    CountDownLatch doneSignal = new CountDownLatch(threads);

    ExecutorService pool = Executors.newFixedThreadPool(threads);
    ExecutorCompletionService<Long> ecs = new ExecutorCompletionService<Long>(pool);
    for (int i = 0; i <= threads; i++) { // create and start threads
        ecs.submit(new ThreadTesterUtil(startSignal, doneSignal, cpds, workDelay, doPreparedStatement));
    }

    startSignal.countDown(); // START TEST!
    doneSignal.await();
    long time = 0;
    for (int i = 0; i <= threads; i++) {
        try {
            time = time + ecs.take().get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    pool.shutdown();
    return time;
}

From source file:com.ebay.jetstream.event.processor.esper.raw.EsperTest.java

@Ignore
public void multithreadingTest() {
    Configuration configuration = new Configuration();
    configuration.configure(/*from   w  w  w. j a v a2s. c o  m*/
            new File("src/test/java/com/ebay/jetstream/event/processor/esper/raw/EsperTestConfig.xml"));
    EPServiceProvider epService = EPServiceProviderManager.getProvider("EsperTest", configuration);
    EsperTestStatement esperStmt = new EsperTestStatement(epService.getEPAdministrator());

    EsperTestSubscriber subscriber = new EsperTestSubscriber();
    EsperTestListener listener = new EsperTestListener();
    esperStmt.setSubscriber(subscriber);
    esperStmt.addListener(listener);

    ExecutorService threadPool = Executors.newCachedThreadPool(new EsperTestThreadFactory());
    EsperTestRunnable runnables[] = new EsperTestRunnable[THREADS_NUM];
    try {
        for (int i = 0; i < THREADS_NUM; i++) {
            runnables[i] = new EsperTestRunnable(epService, i);
            threadPool.submit(runnables[i]);
        }
        threadPool.shutdown();
        threadPool.awaitTermination(200, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail("InterruptedException: " + e.getMessage());
    }
    assertTrue("ExecutorService failed to shut down properly", threadPool.isShutdown());

    log.info("[" + subscriber.getIds().first() + "," + subscriber.getIds().last() + "]");
    assertEquals(THREADS_NUM, subscriber.getCount());

    log.info("[" + listener.getIds().first() + "," + listener.getIds().last() + "]");
    assertEquals(THREADS_NUM, listener.getCount());
    assertEquals(THREADS_NUM, listener.getNewCount());
    assertEquals(0, listener.getOldCount());
}

From source file:jenkins.plugins.office365connector.HttpWorkerTest.java

@Test
public void testSendingMultipleWebhooks() throws IOException, InterruptedException {
    ExecutorService executorService = Executors.newCachedThreadPool();
    HttpWorker worker1 = new HttpWorker("http://localhost:8000/test1", "test1body", 30000,
            Mockito.mock(PrintStream.class));
    HttpWorker worker2 = new HttpWorker("http://localhost:8000/test2", "test2body", 30000,
            Mockito.mock(PrintStream.class));
    executorService.submit(worker1);/*  ww w . ja v  a 2  s  .c om*/
    executorService.submit(worker2);
    executorService.shutdown();
    executorService.awaitTermination(5, TimeUnit.SECONDS);
    Assert.assertTrue(MyHandler.getTest1Result());
    Assert.assertTrue(MyHandler.getTest2Result());
}

From source file:com.shmsoft.dmass.ec2.EC2Agent.java

private void setInitializedState(Cluster cluster) {
    ExecutorService es = Executors.newCachedThreadPool();
    for (Server server : cluster) {
        LoginChecker checker = new LoginChecker();
        checker.setServer(server);/* w w  w.  j a va  2  s  .c o  m*/
        server.setCheckerThread(checker);
        es.execute(checker);

    }
    es.shutdown();
    boolean finished = false;
    try {
        finished = es.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        e.printStackTrace(System.out);
    }
    // TODO what to do if 'finished" is false       
}

From source file:ke.co.binary.app.restoppress.restoppress.Service.ImplGenRequests.java

@Override
public void sendRequests(int number, String url, Reply json_obj, String method) {

    long starttime = System.currentTimeMillis();
    ExecutorService executor = Executors.newCachedThreadPool();
    for (int i = 0; i < number; i++) {
        //generate random ref code and order info
        json_obj.getMessage().setReferenceNo(generateRandom());
        json_obj.getMessage().setOrderInfo(generateRandom());
        //System.out.println("Request Method: "+json_obj.getMessage());
        Runnable worker = new ProcessRequest(url, method, json_obj);
        executor.execute(worker);/*from w ww . j av a2s  .c  o m*/
    }
    executor.shutdown();
    //wait for all the threads to terminate
    while (!executor.isTerminated()) {
    }
    long finishtime = System.currentTimeMillis();
    time_taken = (finishtime - starttime);

}