Example usage for java.util.concurrent CompletionService take

List of usage examples for java.util.concurrent CompletionService take

Introduction

In this page you can find the example usage for java.util.concurrent CompletionService take.

Prototype

Future<V> take() throws InterruptedException;

Source Link

Document

Retrieves and removes the Future representing the next completed task, waiting if none are yet present.

Usage

From source file:com.appdynamics.monitors.hadoop.communicator.AmbariCommunicator.java

/**
 * Parses a JSON Reader object as service metrics and collect service state plus service
 * component metrics. Prefixes metric name with <code>hierarchy</code>.
 * @see #getComponentMetrics(java.io.Reader, String)
 *
 * @param response//from w w w  .  ja  va  2s.co  m
 * @param hierarchy
 */
private void getServiceMetrics(Reader response, String hierarchy) {
    try {
        Map<String, Object> json = (Map<String, Object>) parser.parse(response, simpleContainer);
        try {
            Map serviceInfo = (Map) json.get("ServiceInfo");
            String serviceName = (String) serviceInfo.get("service_name");
            String serviceState = (String) serviceInfo.get("state");

            List<String> states = new ArrayList<String>();
            states.add("INIT");
            states.add("INSTALLING");
            states.add("INSTALL_FAILED");
            states.add("INSTALLED");
            states.add("STARTING");
            states.add("STARTED");
            states.add("STOPPING");
            states.add("UNINSTALLING");
            states.add("UNINSTALLED");
            states.add("WIPING_OUT");
            states.add("UPGRADING");
            states.add("MAINTENANCE");
            states.add("UNKNOWN");
            metrics.put(hierarchy + "|" + serviceName + "|state", states.indexOf(serviceState));

            List<Map> components = (ArrayList<Map>) json.get("components");

            CompletionService<Reader> threadPool = new ExecutorCompletionService<Reader>(executor);
            int count = 0;
            for (Map component : components) {
                if (xmlParser.isIncludeServiceComponent(serviceName,
                        (String) ((Map) component.get("ServiceComponentInfo")).get("component_name"))) {
                    threadPool.submit(new Response(component.get("href") + COMPONENT_FIELDS));
                    count++;
                }
            }
            for (; count > 0; count--) {
                getComponentMetrics(threadPool.take().get(), hierarchy + "|" + serviceName);
            }
        } catch (Exception e) {
            logger.error("Failed to parse service metrics: " + stackTraceToString(e));
        }
    } catch (Exception e) {
        logger.error("Failed to get response for service metrics: " + stackTraceToString(e));
    }
}

From source file:uniol.apt.analysis.synthesize.FindWords.java

static private void generateList(final PNProperties properties, SortedSet<Character> alphabet,
        final boolean quickFail, WordCallback wordCallback, LengthDoneCallback lengthDoneCallback,
        ForkJoinPool executor) throws PreconditionFailedException {
    if (properties.isPlain() && properties.isKMarking())
        throw new PreconditionFailedException("The combination of plain and k-marking is not supported"
                + ", because 'minimal unsolvable' cannot be defined");

    CompletionService<Pair<String, SynthesizePN>> completion = new ExecutorCompletionService<>(executor);
    List<String> currentLevel = Collections.singletonList("");
    while (!currentLevel.isEmpty()) {

        // Lazily create new Callables to avoid OOM errors
        Iterator<Callable<Pair<String, SynthesizePN>>> jobGenerator = IteratorUtils.transformedIterator(
                new NextWordsIterator(properties, alphabet, currentLevel),
                new Transformer<String, Callable<Pair<String, SynthesizePN>>>() {
                    @Override/*from   w w w.  j ava  2s.c om*/
                    public Callable<Pair<String, SynthesizePN>> transform(final String word) {
                        return new Callable<Pair<String, SynthesizePN>>() {
                            @Override
                            public Pair<String, SynthesizePN> call() {
                                List<Character> wordList = toList(word);
                                SynthesizePN synthesize = solveWord(wordList, properties, quickFail);
                                return new Pair<>(word, synthesize);
                            }
                        };
                    }
                });

        // Wait for and handle results
        List<String> nextLevel = new ArrayList<>();
        int tasksSubmitted = submitTasks(executor, completion, jobGenerator);
        int tasksFinished = 0;
        while (tasksSubmitted != tasksFinished) {
            String word;
            SynthesizePN synthesize;
            try {
                Pair<String, SynthesizePN> pair = completion.take().get();
                word = pair.getFirst();
                synthesize = pair.getSecond();
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return;
            }

            List<Character> wordList = toList(word);
            wordCallback.call(wordList, word, synthesize);
            if (synthesize.wasSuccessfullySeparated()) {
                nextLevel.add(word);
            }
            tasksFinished++;

            tasksSubmitted += submitTasks(executor, completion, jobGenerator);
        }

        int currentLength = currentLevel.iterator().next().length() + 1;
        lengthDoneCallback.call(currentLength);
        currentLevel = nextLevel;
        Collections.sort(currentLevel);
    }
}

From source file:com.tascape.qa.th.SuiteRunner.java

public int startExecution() throws IOException, InterruptedException, SQLException, XMLStreamException {
    File dir = SYS_CONFIG.getLogPath().resolve(execId).toFile();
    LOG.info("Create suite execution log directory {}", dir);
    if (!dir.exists() && !dir.mkdirs()) {
        throw new IOException("Cannot create directory " + dir);
    }/*w w  w. ja  va 2s . c om*/
    this.logAppProperties(dir);

    int threadCount = SYS_CONFIG.getExecutionThreadCount();
    LOG.info("Start execution engine with {} thread(s)", threadCount);
    int len = (threadCount + "").length();
    ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("t%0" + len + "d").build();
    ExecutorService executorService = Executors.newFixedThreadPool(threadCount, namedThreadFactory);
    CompletionService<TestResult> completionService = new ExecutorCompletionService<>(executorService);

    LOG.info("Start to acquire test cases to execute");
    int numberOfFailures = 0;
    try {
        List<TestResult> tcrs = this.filter(this.db.getQueuedTestCaseResults(this.execId, 100));
        while (!tcrs.isEmpty()) {
            List<Future<TestResult>> futures = new ArrayList<>();

            for (TestResult tcr : tcrs) {
                LOG.info("Submit test case {}", tcr.getTestCase().format());
                futures.add(completionService.submit(new TestRunnerJUnit4(db, tcr)));
            }
            LOG.debug("Total {} test cases submitted", futures.size());

            for (Future<TestResult> f : futures) {
                try {
                    Future<TestResult> future = completionService.take();
                    TestResult tcr = future.get();
                    if (tcr == null) {
                        continue;
                    }
                    String result = tcr.getResult().result();
                    LOG.info("Get result of test case {} - {}", tcr.getTestCase().format(), result);
                    if (!ExecutionResult.PASS.name().equals(result) && !result.endsWith("/0")) {
                        numberOfFailures++;
                    }
                } catch (Throwable ex) {
                    LOG.error("Error executing test thread", ex);
                    numberOfFailures++;
                }
            }

            tcrs = this.filter(this.db.getQueuedTestCaseResults(this.execId, 100));
        }
    } finally {
        AbstractSuite.getSuites().stream().forEach((suite) -> {
            try {
                suite.tearDown();
            } catch (Exception ex) {
                LOG.warn("Error tearing down suite {} -  {}", suite.getClass(), ex.getMessage());
            }
        });
    }
    executorService.shutdown();

    LOG.info("No more test case to run on this host, updating suite execution result");
    this.db.updateSuiteExecutionResult(this.execId);
    this.db.saveJunitXml(this.execId);
    return numberOfFailures;
}

From source file:org.apache.phoenix.execute.UpsertSelectOverlappingBatchesIT.java

@Test
public void testUpsertSelectSameBatchConcurrently() throws Exception {
    try (Connection conn = driver.connect(url, props)) {
        int numUpsertSelectRunners = 5;
        ExecutorService exec = Executors.newFixedThreadPool(numUpsertSelectRunners);
        CompletionService<Boolean> completionService = new ExecutorCompletionService<Boolean>(exec);
        List<Future<Boolean>> futures = Lists.newArrayListWithExpectedSize(numUpsertSelectRunners);
        // run one UPSERT SELECT for 100 rows (that locks the rows for a long time)
        futures.add(completionService.submit(new UpsertSelectRunner(dataTable, 0, 105, 1)));
        // run four UPSERT SELECTS for 5 rows (that overlap with slow running UPSERT SELECT)
        for (int i = 0; i < 100; i += 25) {
            futures.add(completionService.submit(new UpsertSelectRunner(dataTable, i, i + 25, 5)));
        }/*from w  ww  .j  a v  a 2  s  .c o  m*/
        int received = 0;
        while (received < futures.size()) {
            Future<Boolean> resultFuture = completionService.take();
            Boolean result = resultFuture.get();
            received++;
            assertTrue(result);
        }
        exec.shutdownNow();
    }
}

From source file:org.geowebcache.sqlite.MbtilesBlobStore.java

/**
 * Helper method that delete the provided files.
 *//*  w  w  w .j  av  a  2 s  .co m*/
private boolean deleteFiles(List<File> files) throws StorageException {
    if (files.isEmpty()) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("No files to delete.");
        }
        return false;
    }
    // asking the connection manager to remove the database files
    CompletionService completionService = new ExecutorCompletionService(executorService);
    int tasks = 0;
    for (File file : files) {
        completionService.submit(() -> connectionManager.delete(file), true);
        tasks++;
    }
    // let's wait for the tasks to finish
    for (int i = 0; i < tasks; i++) {
        try {
            completionService.take().get();
        } catch (Exception exception) {
            throw Utils.exception(exception, "Something bad happen when deleting files.");
        }
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Files deleted.");
    }
    return true;
}

From source file:io.druid.query.lookup.LookupReferencesManager.java

/**
 * @return a map with successful lookups
 *//*w  w w  .ja  v a2s .c om*/
private Map<String, LookupExtractorFactoryContainer> startLookups(List<LookupBean> lookupBeans,
        CompletionService<Map.Entry<String, LookupExtractorFactoryContainer>> completionService)
        throws InterruptedException {
    for (LookupBean lookupBean : lookupBeans) {
        completionService.submit(() -> startLookup(lookupBean));
    }
    Map<String, LookupExtractorFactoryContainer> successfulLookups = new HashMap<>();
    for (int i = 0; i < lookupBeans.size(); i++) {
        Future<Map.Entry<String, LookupExtractorFactoryContainer>> completedFuture = completionService.take();
        try {
            Map.Entry<String, LookupExtractorFactoryContainer> lookupResult = completedFuture.get();
            if (lookupResult != null) {
                successfulLookups.put(lookupResult.getKey(), lookupResult.getValue());
            }
        } catch (ExecutionException e) {
            LOG.error(e.getCause(), "Exception while starting a lookup");
            // not adding to successfulLookups
        }
    }
    return successfulLookups;
}

From source file:org.jtheque.modules.impl.ModuleLoader.java

/**
 * Load all the modules from the given files in parallel (using one thread per processor).
 *
 * @param files The files to load the modules from.
 *
 * @return A Collection containing all the loaded modules.
 *///  w  w w.j  ava 2 s  .co m
@SuppressWarnings({ "ForLoopReplaceableByForEach" })
private Collection<Module> loadInParallel(File[] files) {
    ExecutorService loadersPool = Executors.newFixedThreadPool(2 * ThreadUtils.processors());

    CompletionService<Module> completionService = new ExecutorCompletionService<Module>(loadersPool);

    for (File file : files) {
        completionService.submit(new ModuleLoaderTask(file));
    }

    List<Module> modules = CollectionUtils.newList(files.length);

    try {
        for (int i = 0; i < files.length; i++) {
            modules.add(completionService.take().get());
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    } catch (ExecutionException e) {
        throw new RuntimeException(e);
    }

    loadersPool.shutdown();

    return modules;
}

From source file:org.springframework.amqp.rabbit.test.RepeatProcessor.java

public Statement apply(final Statement base, FrameworkMethod method, final Object target) {

    Repeat repeat = AnnotationUtils.findAnnotation(method.getMethod(), Repeat.class);
    if (repeat == null) {
        return base;
    }//from w  w w . jav  a  2  s  .c o  m

    final int repeats = repeat.value();
    if (repeats <= 1) {
        return base;
    }

    initializeIfNecessary(target);

    if (concurrency <= 0) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                try {
                    for (int i = 0; i < repeats; i++) {
                        try {
                            base.evaluate();
                        } catch (Throwable t) {
                            throw new IllegalStateException(
                                    "Failed on iteration: " + i + " of " + repeats + " (started at 0)", t);
                        }
                    }
                } finally {
                    finalizeIfNecessary(target);
                }
            }
        };
    }
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            List<Future<Boolean>> results = new ArrayList<Future<Boolean>>();
            ExecutorService executor = Executors.newFixedThreadPool(concurrency);
            CompletionService<Boolean> completionService = new ExecutorCompletionService<Boolean>(executor);
            try {
                for (int i = 0; i < repeats; i++) {
                    final int count = i;
                    results.add(completionService.submit(new Callable<Boolean>() {
                        public Boolean call() {
                            try {
                                base.evaluate();
                            } catch (Throwable t) {
                                throw new IllegalStateException("Failed on iteration: " + count, t);
                            }
                            return true;
                        }
                    }));
                }
                for (int i = 0; i < repeats; i++) {
                    Future<Boolean> future = completionService.take();
                    assertTrue("Null result from completer", future.get());
                }
            } finally {
                executor.shutdownNow();
                finalizeIfNecessary(target);
            }
        }
    };
}

From source file:org.springframework.integration.groovy.GroovyExpressionTests.java

@Test
public void testScriptFactoryCustomizerStatic() throws Exception {
    final Customizer customizer = new Customizer(Collections.singletonMap("name", (Object) "foo"));
    final GroovyScriptFactory factory = new GroovyScriptFactory("Groovy Script", customizer);
    final ResourceScriptSource scriptSource = new ResourceScriptSource(
            new NamedByteArrayResource("\"name=${name}\"".getBytes(), "InlineScript"));
    Object scriptedObject = factory.getScriptedObject(scriptSource, null);
    assertEquals("name=foo", scriptedObject.toString());
    CompletionService<String> completionService = new ExecutorCompletionService<String>(
            Executors.newFixedThreadPool(10));
    for (int i = 0; i < 100; i++) {
        final String name = "bar" + i;
        completionService.submit(new Callable<String>() {
            public String call() throws Exception {
                Object scriptedObject = factory.getScriptedObject(scriptSource, null);
                String result = scriptedObject.toString();
                logger.debug("Result=" + result + " with name=" + name);
                if (!("name=foo").equals(result)) {
                    throw new IllegalStateException("Wrong value (" + result + ") for: " + name);
                }//from  www.  j a  va  2s .c  o  m
                return name;
            }
        });
    }
    Set<String> set = new HashSet<String>();
    for (int i = 0; i < 100; i++) {
        set.add(completionService.take().get());
    }
    assertEquals(100, set.size());
}

From source file:org.springframework.batch.item.database.JpaPagingItemReaderAsyncTests.java

/**
 * @throws Exception//from   w  ww.  j  a va 2s .  c o m
 * @throws InterruptedException
 * @throws ExecutionException
 */
private void doTest() throws Exception, InterruptedException, ExecutionException {
    final JpaPagingItemReader<Foo> reader = getItemReader();
    CompletionService<List<Foo>> completionService = new ExecutorCompletionService<List<Foo>>(
            Executors.newFixedThreadPool(THREAD_COUNT));
    for (int i = 0; i < THREAD_COUNT; i++) {
        completionService.submit(new Callable<List<Foo>>() {
            @Override
            public List<Foo> call() throws Exception {
                List<Foo> list = new ArrayList<Foo>();
                Foo next = null;
                do {
                    next = reader.read();
                    Thread.sleep(10L);
                    logger.debug("Reading item: " + next);
                    if (next != null) {
                        list.add(next);
                    }
                } while (next != null);
                return list;
            }
        });
    }
    int count = 0;
    Set<Foo> results = new HashSet<Foo>();
    for (int i = 0; i < THREAD_COUNT; i++) {
        List<Foo> items = completionService.take().get();
        count += items.size();
        logger.debug("Finished items count: " + items.size());
        logger.debug("Finished items: " + items);
        assertNotNull(items);
        results.addAll(items);
    }
    assertEquals(ITEM_COUNT, count);
    assertEquals(ITEM_COUNT, results.size());
    reader.close();
}