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.apache.hama.bsp.TestBSPTaskFaults.java

public void testPing() {
    conf.setInt(TEST_POINT, 0);/*w ww.j  a  va  2  s. com*/

    CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(
            this.testBSPTaskService);
    TestBSPProcessRunner runner = new TestBSPProcessRunner(0, workerServer.getListenerAddress().getPort());

    Future<Integer> future = completionService.submit(runner);

    try {
        future.get(20000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e1) {
        LOG.error("Interrupted Exception.", e1);
    } catch (ExecutionException e1) {
        LOG.error("ExecutionException Exception.", e1);
    } catch (TimeoutException e) {
        LOG.error("TimeoutException Exception.", e);
    }

    checkIfPingTestPassed();
    groom.setPingCount(0);
    this.testBSPTaskService.shutdownNow();
    runner.destroyProcess();
}

From source file:org.apache.hama.bsp.TestBSPTaskFaults.java

public void testPingOnTaskSetupFailure() {

    LOG.info("Testing ping failure case - 1");
    conf.setInt(TEST_POINT, 1);/*from   w  w  w .  j  a  v a 2 s .c  o m*/

    CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(
            this.testBSPTaskService);
    TestBSPProcessRunner runner = new TestBSPProcessRunner(1, workerServer.getListenerAddress().getPort());
    Future<Integer> future = completionService.submit(runner);

    try {
        future.get(20000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e1) {
        LOG.error("Interrupted Exception.", e1);
    } catch (ExecutionException e1) {
        LOG.error("ExecutionException Exception.", e1);
    } catch (TimeoutException e) {
        LOG.error("TimeoutException Exception.", e);
    }

    checkIfPingTestPassed();
    groom.setPingCount(0);
    this.testBSPTaskService.shutdownNow();
    runner.destroyProcess();
}

From source file:org.apache.hama.bsp.TestBSPTaskFaults.java

public void testPingOnTaskCleanupFailure() {

    LOG.info("Testing ping failure case - 3");

    conf.setInt(TEST_POINT, 3);/*www .  j ava 2  s  . c  o  m*/
    CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(
            this.testBSPTaskService);
    TestBSPProcessRunner runner = new TestBSPProcessRunner(3, workerServer.getListenerAddress().getPort());

    Future<Integer> future = completionService.submit(runner);

    try {
        future.get(20000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e1) {
        LOG.error("Interrupted Exception.", e1);
    } catch (ExecutionException e1) {
        LOG.error("ExecutionException Exception.", e1);
    } catch (TimeoutException e) {
        LOG.error("TimeoutException Exception.", e);
    }

    checkIfPingTestPassed();
    groom.setPingCount(0);
    this.testBSPTaskService.shutdownNow();
    runner.destroyProcess();
}

From source file:org.apache.hama.bsp.TestBSPTaskFaults.java

public void testBSPTaskSelfDestroy() {
    LOG.info("Testing self kill on lost contact.");

    CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(
            this.testBSPTaskService);
    TestBSPProcessRunner runner = new TestBSPProcessRunner(0, workerServer.getListenerAddress().getPort());

    Future<Integer> future = completionService.submit(runner);

    try {/*from  w ww .j av  a2 s .c  o m*/
        while (groom.pingCount == 0) {
            Thread.sleep(100);
        }
    } catch (Exception e) {
        LOG.error("Interrupted the timer for 1 sec.", e);
    }

    workerServer.stop();
    umbilical = null;
    workerServer = null;
    Integer exitValue = -1;
    try {
        exitValue = future.get(20000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e1) {
        LOG.error("Interrupted Exception.", e1);
    } catch (ExecutionException e1) {
        LOG.error("ExecutionException Exception.", e1);
    } catch (TimeoutException e) {
        LOG.error("TimeoutException Exception.", e);
    }

    assertEquals(69, exitValue.intValue());
    runner.destroyProcess();
}

From source file:nl.privacybarometer.privacyvandaag.service.FetcherService.java

private int refreshFeeds(final long keepDateBorderTime) {
    ContentResolver cr = getContentResolver();
    final Cursor cursor = cr.query(FeedColumns.CONTENT_URI, FeedColumns.PROJECTION_ID, null, null, null);
    int nbFeed = (cursor != null) ? cursor.getCount() : 0;

    ExecutorService executor = Executors.newFixedThreadPool(THREAD_NUMBER, new ThreadFactory() {
        @Override//from   w  ww. jav  a 2 s  . co  m
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setPriority(Thread.MIN_PRIORITY);
            return t;
        }
    });

    CompletionService<Integer> completionService = new ExecutorCompletionService<>(executor);
    while (cursor != null && cursor.moveToNext()) {
        final String feedId = cursor.getString(0);
        completionService.submit(new Callable<Integer>() {
            @Override
            public Integer call() {
                int result = 0;
                try {
                    result = refreshFeed(feedId, keepDateBorderTime);
                } catch (Exception e) {
                    Log.e(TAG, "Error refreshing feed " + e.getMessage());
                }
                return result;
            }
        });
    }
    if (cursor != null)
        cursor.close();

    int globalResult = 0;
    for (int i = 0; i < nbFeed; i++) {
        try {
            Future<Integer> f = completionService.take(); // ModPrivacyVandaag: the count of new articles after a feed is refreshed
            globalResult += f.get();
        } catch (Exception e) {
            Log.e(TAG, "Error counting new articles " + e.getMessage());
        }
    }

    executor.shutdownNow(); // To purge all threads

    return globalResult; // ModPrivacyVandaag: As far as I can see: this contains the number of new articles from a refresh of the feeds.
}

From source file:com.palantir.atlasdb.transaction.impl.SnapshotTransactionTest.java

@Test
public void testConcurrentWriteWriteConflicts() throws InterruptedException, ExecutionException {
    CompletionService<Void> executor = new ExecutorCompletionService<Void>(PTExecutors.newFixedThreadPool(8));
    final Cell cell = Cell.create("row1".getBytes(), "column1".getBytes());
    Transaction t1 = txManager.createNewTransaction();
    t1.put(TABLE, ImmutableMap.of(cell, EncodingUtils.encodeVarLong(0L)));
    t1.commit();/*from   w w w .j a  v  a 2  s .  com*/
    for (int i = 0; i < 1000; i++) {
        executor.submit(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                txManager.runTaskWithRetry(new TxTask() {
                    @Override
                    public Void execute(Transaction t) throws RuntimeException {
                        long prev = EncodingUtils
                                .decodeVarLong(t.get(TABLE, ImmutableSet.of(cell)).values().iterator().next());
                        t.put(TABLE, ImmutableMap.of(cell, EncodingUtils.encodeVarLong(prev + 1)));
                        return null;
                    }
                });
                return null;
            }
        });
    }
    for (int i = 0; i < 1000; i++) {
        Future<Void> future = executor.take();
        future.get();
    }
    t1 = txManager.createNewTransaction();
    long val = EncodingUtils.decodeVarLong(t1.get(TABLE, ImmutableSet.of(cell)).values().iterator().next());
    assertEquals(1000, val);
}

From source file:com.palantir.atlasdb.transaction.impl.SnapshotTransactionTest.java

@Test
public void testConcurrentWriteChangedConflicts() throws InterruptedException, ExecutionException {
    conflictDetectionManager.setConflictDetectionMode(TABLE, ConflictHandler.RETRY_ON_VALUE_CHANGED);
    CompletionService<Void> executor = new ExecutorCompletionService<Void>(PTExecutors.newFixedThreadPool(8));
    final Cell cell = Cell.create("row1".getBytes(), "column1".getBytes());
    Transaction t1 = txManager.createNewTransaction();
    t1.put(TABLE, ImmutableMap.of(cell, EncodingUtils.encodeVarLong(0L)));
    t1.commit();/* w  ww.  j a va  2 s.  c o m*/
    for (int i = 0; i < 1000; i++) {
        executor.submit(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                txManager.runTaskWithRetry(new TxTask() {
                    @Override
                    public Void execute(Transaction t) throws RuntimeException {
                        long prev = EncodingUtils
                                .decodeVarLong(t.get(TABLE, ImmutableSet.of(cell)).values().iterator().next());
                        t.put(TABLE, ImmutableMap.of(cell, EncodingUtils.encodeVarLong(prev + 1)));
                        return null;
                    }
                });
                return null;
            }
        });
    }
    for (int i = 0; i < 1000; i++) {
        Future<Void> future = executor.take();
        future.get();
    }
    t1 = txManager.createNewTransaction();
    long val = EncodingUtils.decodeVarLong(t1.get(TABLE, ImmutableSet.of(cell)).values().iterator().next());
    assertEquals(1000, val);
}

From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.localizer.ContainerLocalizer.java

protected void localizeFiles(LocalizationProtocol nodemanager, CompletionService<Path> cs,
        UserGroupInformation ugi) throws IOException, YarnException {
    while (true) {
        try {/*from   w  ww. j  ava  2 s . com*/
            LocalizerStatus status = createStatus();
            LocalizerHeartbeatResponse response = nodemanager.heartbeat(status);
            switch (response.getLocalizerAction()) {
            case LIVE:
                List<ResourceLocalizationSpec> newRsrcs = response.getResourceSpecs();
                for (ResourceLocalizationSpec newRsrc : newRsrcs) {
                    if (!pendingResources.containsKey(newRsrc.getResource())) {
                        pendingResources.put(newRsrc.getResource(),
                                cs.submit(download(new Path(newRsrc.getDestinationDirectory().getFile()),
                                        newRsrc.getResource(), ugi)));
                    }
                }
                break;
            case DIE:
                // killall running localizations
                for (Future<Path> pending : pendingResources.values()) {
                    pending.cancel(true);
                }
                status = createStatus();
                // ignore response while dying.
                try {
                    nodemanager.heartbeat(status);
                } catch (YarnException e) {
                    // Cannot do anything about this during death stage, let's just log
                    // it.
                    e.printStackTrace(System.out);
                    LOG.error("Heartbeat failed while dying: ", e);
                }
                return;
            }
            cs.poll(1000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            return;
        } catch (YarnException e) {
            // TODO cleanup
            throw e;
        }
    }
}

From source file:org.codice.ddf.spatial.ogc.csw.catalog.transformer.CswQueryResponseTransformer.java

private String multiThreadedMarshal(List<Result> results, String recordSchema,
        final Map<String, Serializable> arguments) throws CatalogTransformerException {

    CompletionService<BinaryContent> completionService = new ExecutorCompletionService<>(queryExecutor);

    try {/*  w ww .j  ava  2s.c  om*/
        for (Result result : results) {
            final Metacard mc = result.getMetacard();

            final MetacardTransformer transformer = metacardTransformerManager
                    .getTransformerBySchema(recordSchema);

            if (transformer == null) {
                throw new CatalogTransformerException("Cannot find transformer for schema: " + recordSchema);
            }

            // the "current" thread will run submitted task when queueSize exceeded; effectively
            // blocking enqueue of more tasks.
            completionService.submit(new Callable<BinaryContent>() {
                @Override
                public BinaryContent call() throws Exception {
                    BinaryContent content = transformer.transform(mc, arguments);
                    return content;
                }
            });
        }

        int metacardCount = results.size();
        CharArrayWriter accum = new CharArrayWriter(ACCUM_INITIAL_SIZE);
        for (int i = 0; i < metacardCount; i++) {
            Future<BinaryContent> binaryContentFuture = completionService.take(); // blocks
            BinaryContent binaryContent = binaryContentFuture.get();
            IOUtils.copy(binaryContent.getInputStream(), accum);
        }

        return accum.toString();

    } catch (IOException | InterruptedException | ExecutionException xe) {
        throw new CatalogTransformerException(xe);
    }

}

From source file:com.rapid7.diskstorage.dynamodb.DynamoDBDelegate.java

public void parallelMutate(List<MutateWorker> workers) throws BackendException {
    CompletionService<Void> completion = new ExecutorCompletionService<>(clientThreadPool);
    List<Future<Void>> futures = Lists.newLinkedList();
    for (MutateWorker worker : workers) {
        futures.add(completion.submit(worker));
    }/*  w w w  .  j  ava  2 s.c om*/

    //block on the futures all getting or throwing instead of using a latch as i need to check future status anyway
    boolean interrupted = false;
    try {
        for (int i = 0; i < workers.size(); i++) {
            try {
                completion.take().get(); //Void
            } catch (InterruptedException e) {
                interrupted = true;
                // fail out because titan does not poll this thread for interrupted anywhere
                throw new BackendRuntimeException("was interrupted during parallelMutate");
            } catch (ExecutionException e) {
                throw unwrapExecutionException(e, MUTATE_ITEM);
            }
        }
    } finally {
        for (Future<Void> future : futures) {
            if (!future.isDone()) {
                future.cancel(interrupted /* mayInterruptIfRunning */);
            }
        }
        if (interrupted) {
            // set interrupted on this thread
            Thread.currentThread().interrupt();
        }
    }
}