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.dumontierlab.pdb2rdf.Pdb2Rdf.java

private static void load(CommandLine cmd, final Map<String, Double> stats) {
    String username = "dba";
    String password = "dba";
    String host = "localhost";
    int port = 1111;
    DetailLevel detailLevel = null;/*from  www  .ja  v a  2s  . co m*/
    if (cmd.hasOption("detailLevel")) {
        try {
            detailLevel = Enum.valueOf(DetailLevel.class, cmd.getOptionValue("detailLevel"));
        } catch (IllegalArgumentException e) {
            LOG.fatal("Invalid argument value for detailLevel option", e);
            System.exit(1);
        }
    }
    final DetailLevel f_detailLevel = detailLevel;

    if (cmd.hasOption("username")) {
        username = cmd.getOptionValue("username");
    }
    if (cmd.hasOption("password")) {
        password = cmd.getOptionValue("password");
    }
    if (cmd.hasOption("host")) {
        host = cmd.getOptionValue("host");
    }
    if (cmd.hasOption("port")) {
        try {
            port = Integer.parseInt(cmd.getOptionValue("port"));
        } catch (NumberFormatException e) {
            LOG.fatal("Invalid port number: " + cmd.getOptionValue("port"));
            System.exit(1);
        }
    }

    final VirtuosoDaoFactory factory = new VirtuosoDaoFactory(host, port, username, password);
    ExecutorService pool = getThreadPool(cmd);

    final ProgressMonitor monitor = getProgressMonitor();
    final Pdb2RdfInputIterator i = processInput(cmd);
    final int inputSize = i.size();
    final AtomicInteger progressCount = new AtomicInteger();

    if (monitor != null) {
        monitor.setProgress(0, inputSize);
    }

    while (i.hasNext()) {
        final InputSource input = i.next();
        pool.execute(new Runnable() {
            public void run() {
                PdbXmlParser parser = new PdbXmlParser();
                UriBuilder uriBuilder = new UriBuilder();
                PdbRdfModel model = null;
                try {
                    model = new VirtPdbRdfModel(factory, Bio2RdfPdbUriPattern.PDB_GRAPH, uriBuilder,
                            factory.getTripleStoreDao());
                    if (f_detailLevel != null) {
                        parser.parse(input, model, f_detailLevel);
                    } else {
                        parser.parse(input, model);
                    }
                    if (stats != null) {
                        updateStats(stats, model);
                    }
                    if (monitor != null) {
                        monitor.setProgress(progressCount.incrementAndGet(), inputSize);
                    }

                } catch (Exception e) {
                    LOG.error("Uanble to parse input for pdb=" + (model != null ? model.getPdbId() : "null"),
                            e);
                }
            }
        });
    }
    pool.shutdown();
    while (!pool.isTerminated()) {
        try {
            pool.awaitTermination(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            break;
        }
    }
}

From source file:info.pancancer.arch3.coordinator.Coordinator.java

public void doWork() throws InterruptedException, ExecutionException {
    ExecutorService pool = Executors.newFixedThreadPool(DEFAULT_THREADS);
    CoordinatorOrders coordinatorOrders = new CoordinatorOrders(this.configFile,
            this.options.has(this.endlessSpec));
    CleanupJobs cleanupJobs = new CleanupJobs(this.configFile, this.options.has(this.endlessSpec));
    FlagJobs flagJobs = new FlagJobs(this.configFile, this.options.has(this.endlessSpec));
    List<Future<?>> futures = new ArrayList<>();
    futures.add(pool.submit(coordinatorOrders));
    futures.add(pool.submit(cleanupJobs));
    futures.add(pool.submit(flagJobs));/*from   w  ww  .  j  ava2  s. co m*/
    try {
        for (Future<?> future : futures) {
            future.get();
        }
    } catch (InterruptedException | ExecutionException ex) {
        log.error(ex.toString());
        throw new RuntimeException(ex);
    } finally {
        pool.shutdown();
    }
}

From source file:co.pugo.convert.ConvertServlet.java

/**
 * download imageData and encode it base64
 * @param imageLinks set of image links extracted with extractImageLinks()
 * @return map, key = imageLink, value = base64 encoded image
 *///from   w w w .ja  v a 2 s. c o m
private HashMap<String, String> downloadImageData(Set<String> imageLinks) {
    HashMap<String, String> imageData = new HashMap<>();
    ExecutorService service = Executors.newCachedThreadPool();
    for (final String imageLink : imageLinks) {
        RunnableFuture<byte[]> future = new FutureTask<>(new Callable<byte[]>() {
            @Override
            public byte[] call() {
                try {
                    URL srcUrl = new URL(imageLink);
                    URLConnection urlConnection = srcUrl.openConnection();
                    return IOUtils.toByteArray(urlConnection.getInputStream());
                } catch (IOException e) {
                    LOG.severe(e.getMessage());
                    return null;
                }
            }
        });
        service.execute(future);
        try {
            imageData.put(imageLink, Base64.encodeBase64String(future.get()));
        } catch (InterruptedException | ExecutionException e) {
            LOG.severe(e.getMessage());
        }
    }
    service.shutdown();
    try {
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException e) {
        LOG.severe(e.getMessage());
    }
    return imageData;
}

From source file:org.ngrinder.perftest.service.AgentManager.java

/**
 * Assign the agents on the given console.
 *
 * @param user              user/*from   w  w  w  . j av  a 2 s .com*/
 * @param singleConsole     {@link SingleConsole} to which agents will be assigned
 * @param grinderProperties {@link GrinderProperties} to be distributed.
 * @param agentCount        the count of agents.
 */
public synchronized void runAgent(User user, final SingleConsole singleConsole,
        final GrinderProperties grinderProperties, final Integer agentCount) {
    final Set<AgentIdentity> allFreeAgents = getAllFreeApprovedAgentsForUser(user);
    final Set<AgentIdentity> necessaryAgents = selectAgent(user, allFreeAgents, agentCount);
    LOGGER.info("{} agents are starting for user {}", agentCount, user.getUserId());
    for (AgentIdentity each : necessaryAgents) {
        LOGGER.info("- Agent {}", each.getName());
    }
    ExecutorService execService = null;
    try {
        // Make the agents connect to console.
        grinderProperties.setInt(GrinderProperties.CONSOLE_PORT, singleConsole.getConsolePort());
        execService = ExecutorFactory.createThreadPool("agentStarter", NUMBER_OF_THREAD);
        for (final AgentIdentity eachAgentIdentity : necessaryAgents) {
            execService.submit(new Runnable() {
                @Override
                public void run() {
                    agentControllerServerDaemon.startAgent(grinderProperties, eachAgentIdentity);
                }
            });
        }
    } finally {
        if (execService != null) {
            execService.shutdown();
        }
    }
}

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

@Test
public void testRetryOnConflict() throws InterruptedException {
    final String index = "JresUpdateDocumentScriptTest.testRetryOnConflict".toLowerCase();
    final String type = "test";
    final String id = "warzone";

    final AtomicInteger total = new AtomicInteger();
    final AtomicReference<String> error = new AtomicReference<String>();
    final Random random = new Random(System.currentTimeMillis());

    final int numThreads = 16, numIterations = 100;

    ExecutorService x = Executors.newFixedThreadPool(numThreads);
    for (int i = 0; i < numThreads; i++) {
        x.submit(new Runnable() {
            @Override//from w  w w .  j  a v a2s . c  o  m
            public void run() {
                try {
                    for (int j = 0; j < numIterations; j++) {
                        int increment = random.nextInt(5);
                        total.addAndGet(increment);
                        JresUpdateDocumentScript req = new JresUpdateDocumentScript(index, type, id,
                                "ctx._source.value += increment", ImmutableMap.of("increment", increment),
                                ImmutableMap.of("value", increment), null);
                        req.setRetryOnConflict(numIterations * 10);
                        jres.quest(req);
                    }
                } catch (Exception e) {
                    error.set(e.getMessage());
                }
            }
        });
    }
    x.shutdown();
    x.awaitTermination(1, TimeUnit.MINUTES);

    Assert.assertNull("With so many retries, all of these should have gotten through without conflict error",
            error.get());
    jres.quest(new JresRefresh(index));
    JresGetDocumentReply getReply = jres.quest(new JresGetDocument(index, type, id));
    Map<String, Integer> doc = getReply.getSourceAsType(new TypeReference<Map<String, Integer>>() {
    });
    Assert.assertEquals("All increments should have gotten committed", (Object) total.get(), doc.get("value"));
    Assert.assertEquals("Should have been numThreads * numIterations versions committed",
            (Object) (numThreads * numIterations), getReply.getVersion());
}

From source file:com.laudandjolynn.mytv.Main.java

/**
 * ?// w w  w  .  j a va2s  .  c  o  m
 * 
 * @param data
 */
private static void runCrawlTask(final MyTvData data, final TvService tvService) {
    CrawlEventListener listener = null;
    final String today = DateUtils.today();
    final ExecutorService executorService = Executors.newFixedThreadPool(Constant.CPU_PROCESSOR_NUM,
            new BasicThreadFactory.Builder().namingPattern("Mytv_Crawl_Program_Table_%d").build());
    if (!data.isProgramCrawlerInited()) {
        listener = new CrawlEventListenerAdapter() {
            @Override
            public void itemFound(CrawlEvent event) {
                if (event instanceof TvStationFoundEvent) {
                    final TvStation item = (TvStation) ((TvStationFoundEvent) event).getItem();
                    if (!tvService.isInMyTv(item) || CrawlAction.getIntance().isInQuerying(item, today)) {
                        return;
                    }
                    executorService.execute(new Runnable() {

                        @Override
                        public void run() {
                            CrawlAction.getIntance().queryProgramTable(item, today);
                        }
                    });
                }
            }
        };
    }

    // ???
    logger.info("It is trying to find some proxyies.");
    MyTvProxyManager.getInstance().prepareProxies(new ConfigProxy());
    logger.info("found " + MyTvProxyManager.getInstance().getProxySize() + " proxies.");

    if (!data.isStationCrawlerInited()) {
        // ?
        tvService.crawlAllTvStation(listener);

        // ??
        String[] weeks = DateUtils.getWeek(new Date(), "yyyy-MM-dd");
        List<TvStation> stationList = tvService.getDisplayedTvStation();
        for (String date : weeks) {
            if (date.compareTo(today) >= 1) {
                crawlAllProgramTable(stationList, executorService, date, tvService);
            }
        }
        executorService.shutdown();
        data.writeData(null, Constant.XML_TAG_STATION, "true");
        data.writeData(null, Constant.XML_TAG_PROGRAM, "true");
    }
}

From source file:com.twitter.distributedlog.auditor.DLAuditor.java

public long calculateLedgerSpaceUsage(URI uri) throws IOException {
    List<URI> uris = Lists.newArrayList(uri);
    String zkServers = validateAndGetZKServers(uris);
    RetryPolicy retryPolicy = new BoundExponentialBackoffRetryPolicy(conf.getZKRetryBackoffStartMillis(),
            conf.getZKRetryBackoffMaxMillis(), Integer.MAX_VALUE);
    ZooKeeperClient zkc = ZooKeeperClientBuilder.newBuilder().name("DLAuditor-ZK").zkServers(zkServers)
            .sessionTimeoutMs(conf.getZKSessionTimeoutMilliseconds()).retryPolicy(retryPolicy)
            .zkAclId(conf.getZkAclId()).build();
    ExecutorService executorService = Executors.newCachedThreadPool();
    try {//from   w  w  w  .  ja  va  2 s.c o  m
        BKDLConfig bkdlConfig = resolveBKDLConfig(zkc, uris);
        logger.info("Resolved bookkeeper config : {}", bkdlConfig);

        BookKeeperClient bkc = BookKeeperClientBuilder.newBuilder().name("DLAuditor-BK").dlConfig(conf)
                .zkServers(bkdlConfig.getBkZkServersForWriter()).ledgersPath(bkdlConfig.getBkLedgersPath())
                .build();
        try {
            return calculateLedgerSpaceUsage(bkc, executorService);
        } finally {
            bkc.close();
        }
    } finally {
        zkc.close();
        executorService.shutdown();
    }
}

From source file:gobblin.configuration.StateTest.java

/**
 * This test checks that state object is thread safe. We run 2 threads, one of them continuously adds and removes key/values
 * to the state and other thread calls getProperties.
 *//*  w  ww  .  jav  a  2 s .c o  m*/
@Test
public void testGetPropertiesThreadSafety() {
    try {
        final State state = new State();
        for (int i = 0; i < 1000; i++) {
            state.setProp(Integer.toString(i), Integer.toString(i));
        }
        ExecutorService executorService = Executors.newFixedThreadPool(2);

        executorService.submit(new Runnable() {
            @Override
            public void run() {
                for (int j = 0; j < 1000; j++) {
                    for (int i = 0; i < 1000; i++) {
                        try {
                            state.removeProp(Integer.toString(i));
                            state.setProp(Integer.toString(i), Integer.toString(i));
                        } catch (Throwable t) {
                            exceptions.add(t);
                        }
                    }
                }
            }
        });

        executorService.submit(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    try {
                        state.getProperties().get(Integer.toString(i));
                    } catch (Throwable t) {
                        exceptions.add(t);
                    }
                }
            }
        });

        executorService.shutdown();
        if (!executorService.awaitTermination(100, TimeUnit.SECONDS)) {
            throw new RuntimeException("Executor service still running");
        }
    } catch (Throwable t) {
        Assert.fail("Concurrency test failed", t);
    }

    if (!this.exceptions.isEmpty()) {
        Assert.fail("Concurrency test failed with first exception: "
                + ExceptionUtils.getFullStackTrace(this.exceptions.poll()));
    }
}

From source file:com.tyndalehouse.step.tools.modules.ConvertXmlToOSISModule.java

private void convert() throws Exception {
    final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(1024);
    final ExecutorService executorService = new ThreadPoolExecutor(3, 3, 1, TimeUnit.DAYS, queue);

    final File[] files = SOURCE_DIRECTORY.listFiles();
    for (final File f : files) {
        if (f.isDirectory()) {
            final File[] unzippedFiles = f.listFiles();
            for (final File unzipped : unzippedFiles) {
                if (unzipped.getName().endsWith(".xml")) {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                convertToXml(f.getName(), unzipped);
                                LOGGER.debug("Finished [{}], [{}] remaining", f.getName(), queue.size());
                            } catch (Exception e) {
                                LOGGER.error("Failed to convert [{}]", f.getName(), e);
                            }//from   w  ww. j  ava2  s .  co m
                        }
                    });
                    break;
                }
            }
            //                break;
        }
    }
    executorService.shutdown();
}