Example usage for java.util.concurrent CompletionService submit

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

Introduction

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

Prototype

Future<V> submit(Callable<V> task);

Source Link

Document

Submits a value-returning task for execution and returns a Future representing the pending results of the task.

Usage

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

/**
 * @throws Exception//from  w w w. j a  va2  s  .  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();
}

From source file:com.flipkart.bifrost.ListenTest.java

@Test
public void testSendReceive() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

    Connection connection = new Connection(Lists.newArrayList("localhost"), "guest", "guest");
    connection.start();//w  w w . j ava  2 s. c om

    BifrostExecutor<Void> executor = BifrostExecutor.<Void>builder(TestAction.class).connection(connection)
            .objectMapper(mapper).requestQueue("bifrost-send").responseQueue("bifrost-recv").concurrency(20)
            .executorService(Executors.newFixedThreadPool(20)).build();

    BifrostRemoteCallExecutionServer<Void> executionServer = BifrostRemoteCallExecutionServer
            .<Void>builder(TestAction.class).objectMapper(mapper).connection(connection).concurrency(20)
            .requestQueue("bifrost-send").build();
    executionServer.start();

    long startTime = System.currentTimeMillis();
    AtomicInteger counter = new AtomicInteger(0);
    int requestCount = 1000000;
    CompletionService<Void> ecs = new ExecutorCompletionService<>(Executors.newFixedThreadPool(50));
    List<Future<Void>> futures = Lists.newArrayListWithCapacity(requestCount);
    for (int i = 0; i < requestCount; i++) {
        futures.add(ecs.submit(new ServiceCaller(executor, counter)));
    }
    for (int i = 0; i < requestCount; i++) {
        try {
            ecs.take().get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    while (counter.get() != requestCount)
        ;
    System.out.println(
            String.format("Completed: %d in %d ms", counter.get(), (System.currentTimeMillis() - startTime)));
    executor.shutdown();
    executionServer.stop();
    connection.stop();

    Assert.assertEquals(requestCount, counter.get());
}

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

/**
 * @throws Exception/*from w  ww  .ja v a2 s.c  om*/
 * @throws InterruptedException
 * @throws ExecutionException
 */
private void doTest() throws Exception, InterruptedException, ExecutionException {
    final IbatisPagingItemReader<Foo> reader = getItemReader();
    reader.setDataSource(dataSource);
    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); // try to make it fairer
                    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();
}

From source file:org.springframework.integration.store.MessageGroupQueueTests.java

private void doTestConcurrentAccess(int concurrency, final int maxPerTask, final Set<String> set)
        throws Exception {

    SimpleMessageStore messageGroupStore = new SimpleMessageStore();
    final MessageGroupQueue queue = new MessageGroupQueue(messageGroupStore, "FOO");
    ExecutorService executorService = Executors.newCachedThreadPool();
    CompletionService<Boolean> completionService = new ExecutorCompletionService<Boolean>(executorService);

    for (int i = 0; i < concurrency; i++) {

        final int big = i;

        completionService.submit(new Callable<Boolean>() {
            public Boolean call() throws Exception {
                boolean result = true;
                for (int j = 0; j < maxPerTask; j++) {
                    result &= queue.add(new GenericMessage<String>("count=" + big + ":" + j));
                    if (!result) {
                        logger.warn("Failed to add");
                    }//from   w  ww  .  j ava2 s . com
                }
                return result;
            }
        });

        completionService.submit(new Callable<Boolean>() {
            public Boolean call() throws Exception {
                boolean result = true;
                for (int j = 0; j < maxPerTask; j++) {
                    @SuppressWarnings("unchecked")
                    Message<String> item = (Message<String>) queue.poll(10, TimeUnit.SECONDS);
                    result &= item != null;
                    if (!result) {
                        logger.warn("Failed to poll");
                    } else if (set != null) {
                        synchronized (set) {
                            set.add(item.getPayload());
                        }
                    }
                }
                return result;
            }
        });

        messageGroupStore.expireMessageGroups(-10000);

    }

    for (int j = 0; j < 2 * concurrency; j++) {
        assertTrue(completionService.take().get());
    }

    if (set != null) {
        // Ensure all items polled are unique
        assertEquals(concurrency * maxPerTask, set.size());
    }

    assertEquals(0, queue.size());
    messageGroupStore.expireMessageGroups(-10000);
    assertEquals(Integer.MAX_VALUE, queue.remainingCapacity());

    executorService.shutdown();

}

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

/**
 * @throws Exception//from   w w  w .jav a2  s.  c om
 * @throws InterruptedException
 * @throws ExecutionException
 */
private void doTest() throws Exception, InterruptedException, ExecutionException {
    final ItemReader<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());
}

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   w w w.  j  a va  2s  .c  om*/
                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.integration.groovy.GroovyExpressionTests.java

@Test
public void testScriptFactoryCustomizerThreadSafetyWithNewScript() throws Exception {
    final Customizer customizer = new Customizer(Collections.singletonMap("name", (Object) "foo"));
    final GroovyScriptFactory factory = new GroovyScriptFactory("Groovy Script", customizer);
    CompletionService<String> completionService = new ExecutorCompletionService<String>(
            Executors.newFixedThreadPool(5));
    for (int i = 0; i < 100; i++) {
        final String name = "Bar" + i;
        completionService.submit(new Callable<String>() {
            public String call() throws Exception {
                Object scriptedObject;
                synchronized (customizer) {
                    customizer.setMap(Collections.singletonMap("name", (Object) name));
                    ResourceScriptSource scriptSource = new ResourceScriptSource(
                            new NamedByteArrayResource("\"name=${name}\"".getBytes(), "InlineScript" + name));
                    scriptedObject = factory.getScriptedObject(scriptSource, null);
                }/*w  ww .  j ava 2 s. c  o m*/
                String result = scriptedObject.toString();
                logger.debug("Result=" + result + " with name=" + name);
                if (!("name=" + name).equals(result)) {
                    throw new IllegalStateException("Wrong value (" + result + ") for: " + name);
                }
                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.integration.groovy.GroovyExpressionTests.java

@Test
public void testScriptFactoryCustomizerThreadSafety() 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;
                synchronized (customizer) {
                    customizer.setMap(Collections.singletonMap("name", (Object) name));
                    scriptedObject = factory.getScriptedObject(scriptSource, null);
                }//from  ww  w.j a v a  2 s.c  o  m
                String result = scriptedObject.toString();
                logger.debug("Result=" + result + " with name=" + name);
                if (!("name=" + name).equals(result)) {
                    throw new IllegalStateException("Wrong value (" + result + ") for: " + name);
                }
                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:io.scigraph.owlapi.loader.BatchOwlLoader.java

public void loadOntology() throws InterruptedException, ExecutionException {
    CompletionService<Long> completionService = new ExecutorCompletionService<Long>(exec);
    Set<Future<?>> futures = new HashSet<>();
    if (!ontologies.isEmpty()) {
        for (int i = 0; i < numConsumers; i++) {
            futures.add(completionService.submit(consumerProvider.get()));
        }//from   ww w.j av a  2 s  .  co m
        for (int i = 0; i < numProducers; i++) {
            futures.add(completionService.submit(producerProvider.get()));
        }
        for (OntologySetup ontology : ontologies) {
            urlQueue.offer(ontology);
        }
        for (int i = 0; i < numProducers; i++) {
            urlQueue.offer(POISON_STR);
        }
    }

    while (futures.size() > 0) {
        Future<?> completedFuture = completionService.take();
        futures.remove(completedFuture);
        try {
            completedFuture.get();
        } catch (ExecutionException e) {
            logger.log(Level.SEVERE, "Stopping batchLoading due to: " + e.getMessage(), e);
            e.printStackTrace();
            exec.shutdownNow();
            throw new InterruptedException(e.getCause().getMessage());
        }
    }

    exec.shutdown();
    exec.awaitTermination(10, TimeUnit.DAYS);
    graph.shutdown();
    logger.info("Postprocessing...");
    postprocessorProvider.get().postprocess();

    if (cliqueConfiguration.isPresent()) {
        postprocessorProvider.runCliquePostprocessor(cliqueConfiguration.get());
    }

    postprocessorProvider.shutdown();

}

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;
    }// w w  w. j av 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);
            }
        }
    };
}