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: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.  j a va  2 s .  com*/
 * @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:io.druid.query.lookup.LookupReferencesManager.java

/**
 * @return a map with successful lookups
 *///w  w  w.  j  a  v  a 2s.c  o  m
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.m2x.rssreader.service.FetcherService.java

private int refreshFeeds() {
    ContentResolver cr = getContentResolver();
    final Cursor cursor = cr.query(FeedColumns.CONTENT_URI, FeedColumns.PROJECTION_ID, null, null, null);
    int nbFeed = cursor.getCount();

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

    CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(executor);
    while (cursor.moveToNext()) {
        final String feedId = cursor.getString(0);
        completionService.submit(new Callable<Integer>() {
            @Override
            public Integer call() {
                int result = 0;
                try {
                    result = refreshFeed(feedId);
                } catch (Exception ignored) {
                }
                return result;
            }
        });
    }
    cursor.close();

    int globalResult = 0;
    for (int i = 0; i < nbFeed; i++) {
        try {
            Future<Integer> f = completionService.take();
            globalResult += f.get();
        } catch (Exception ignored) {
        }
    }

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

    return globalResult;
}

From source file:org.codice.ddf.catalog.sourcepoller.Poller.java

/**
 * For each of the {@code itemsToPoll}, uses the {@code completionService} to kick off the process
 * to load the new value. If unable to start the process, an exception is added to {@code
 * gatheredExceptions}.//from  w  w  w  .  j  a  v  a 2  s  . c o  m
 *
 * @return the number of processes started
 */
private int startLoads(final long timeout, final TimeUnit timeoutTimeUnit,
        final ImmutableMap<K, Callable<V>> itemsToPoll,
        final CompletionService<Pair<K, Commitable>> completionService,
        final Map<K, Throwable> gatheredExceptions) {
    int startedLoadsCount = 0;
    for (final Entry<K, Callable<V>> entry : itemsToPoll.entrySet()) {
        final K key1 = entry.getKey();
        final Callable<V> loader = entry.getValue();
        try {
            completionService
                    .submit(() -> new ImmutablePair<>(key1, load(key1, loader, timeout, timeoutTimeUnit)));
            startedLoadsCount++;
        } catch (final RuntimeException e) {
            LOGGER.debug("Unable to start the load task for {}", key1, e);
            gatheredExceptions.put(key1, e);
        }
    }

    return startedLoadsCount;
}

From source file:org.apache.drill.sql.client.ref.DrillRefImpl.java

/**
 * Runs the plan as a background task./*from  w ww.  j  a  v  a  2  s .c  o m*/
 */
Future<Collection<RunOutcome>> runRefInterpreterPlan(CompletionService<Collection<RunOutcome>> service) {
    LogicalPlan parsedPlan = LogicalPlan.parse(DrillConfig.create(), plan);
    IteratorRegistry ir = new IteratorRegistry();
    DrillConfig config = DrillConfig.create();
    config.setSinkQueues(0, queue);
    final ReferenceInterpreter i = new ReferenceInterpreter(parsedPlan, ir, new BasicEvaluatorFactory(ir),
            new RSERegistry(config));
    try {
        i.setup();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return service.submit(new Callable<Collection<RunOutcome>>() {
        @Override
        public Collection<RunOutcome> call() throws Exception {
            Collection<RunOutcome> outcomes = i.run();

            for (RunOutcome outcome : outcomes) {
                System.out.println("============");
                System.out.println(outcome);
                if (outcome.outcome == RunOutcome.OutcomeType.FAILED && outcome.exception != null) {
                    outcome.exception.printStackTrace();
                }
            }
            return outcomes;
        }
    });
}

From source file:com.elixsr.portforwarder.forwarding.ForwardingService.java

/**
 * Starts forwarding based on rules found in database.
 *
 * Acquires an instance of the Forwarding Manager to turn forwarding flag on.
 *
 * Creates a list off callbacks for each forward thread, and handle exceptions as they come.
 *
 * If an exception is thrown, the service immediately stops, and the #onDestroy method is
 * called./*w ww. j av  a  2  s .c  om*/
 *
 * @param intent
 */
@Override
protected void onHandleIntent(Intent intent) {

    // Gets data from the incoming Intent
    //        String dataString = intent.getDataString();

    Log.i(TAG, "Ran the service");

    ForwardingManager.getInstance().enableForwarding();

    runService = true;

    /*
     * Creates a new Intent containing a Uri object
     * BROADCAST_ACTION is a custom Intent action
     */
    Intent localIntent = new Intent(BROADCAST_ACTION)
            // Puts the status into the Intent
            .putExtra(PORT_FORWARD_SERVICE_STATE, ForwardingManager.getInstance().isEnabled());
    // Broadcasts the Intent to receivers in this app.
    LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);

    showForwardingEnabledNotification();

    //load the rules from the datastore
    //TODO: inject the rules as extras
    RuleDao ruleDao = new RuleDao(new RuleDbHelper(this));
    List<RuleModel> ruleModels = ruleDao.getAllRuleModels();

    InetSocketAddress from;

    Forwarder forwarder = null;

    /*
     Sourced from: http://stackoverflow.com/questions/19348248/waiting-on-a-list-of-future
     */
    CompletionService<Void> completionService = new ExecutorCompletionService<>(executorService);

    // how many futures there are to check
    int remainingFutures = 0;

    for (RuleModel ruleModel : ruleModels) {

        try {
            from = generateFromIpUsingInterface(ruleModel.getFromInterfaceName(), ruleModel.getFromPort());

            if (ruleModel.isTcp()) {
                completionService.submit(new TcpForwarder(from, ruleModel.getTarget(), ruleModel.getName()));
                remainingFutures++;
            }

            if (ruleModel.isUdp()) {
                completionService.submit(new UdpForwarder(from, ruleModel.getTarget(), ruleModel.getName()));
                remainingFutures++;
            }

        } catch (SocketException | ObjectNotFoundException e) {
            Log.e(TAG, "Error generating IP Address for FROM interface with rule '" + ruleModel.getName() + "'",
                    e);

            // graceful UI Exception handling - broadcast this to ui - it will deal with display something to the user e.g. a Toast
            localIntent = new Intent(BROADCAST_ACTION)
                    // Puts the status into the Intent
                    .putExtra(PORT_FORWARD_SERVICE_ERROR_MESSAGE,
                            "Error while trying to start rule '" + ruleModel.getName() + "'");
            // Broadcasts the Intent to receivers in this app.
            LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
        }
    }

    // Build and send an Event.
    tracker.send(new HitBuilders.EventBuilder().setCategory(CATEGORY_FORWARDING)
            .setAction(ACTION_START_FORWARDING).setLabel(ruleModels.size() + " rules").build());

    Future<?> completedFuture;

    // loop through each callback, and handle an exception
    while (remainingFutures > 0) {

        // block until a callable completes
        try {
            completedFuture = completionService.take();
            remainingFutures--;

            completedFuture.get();
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();

            Log.e(TAG, "Error when forwarding port.", e);
            localIntent = new Intent(BROADCAST_ACTION)
                    // Puts the status into the Intent
                    .putExtra(PORT_FORWARD_SERVICE_ERROR_MESSAGE, e.getCause().getMessage());
            // Broadcasts the Intent to receivers in this app.
            LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);

            break;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.nickelproject.nickel.blobStore.S3BlobStore.java

private void putMultiPartByteArray(final BlobRef blobRef, final byte[] pBytes) {
    final CompletionService<PartETag> completionService = new ExecutorCompletionService<PartETag>(executor);
    final String key = blobRef.toString();
    final PartTransferInterface partGetter = RetryProxy.newInstance(PartTransferInterface.class,
            new PartTransfer());
    final String uploadId = partGetter.initiateUpload(key);
    int partNumber = 1;
    for (int start = 0; start < pBytes.length; start += uploadPartSize) {
        final long length = Math.min(uploadPartSize, pBytes.length - start);
        completionService.submit(new PutPartCallable(start, (int) length, pBytes, partNumber++, key, uploadId,
                start + uploadPartSize >= pBytes.length));
    }/*from w  w  w  .  j  ava  2s . c  o  m*/
    final List<PartETag> partETags = Lists.newArrayList();
    for (int i = 1; i < partNumber; i++) {
        try {
            partETags.add(completionService.take().get());
        } catch (final InterruptedException e1) {
            Thread.currentThread().interrupt();
            throw RethrownException.rethrow(e1);
        } catch (final ExecutionException e2) {
            throw RethrownException.rethrow(e2);
        }
    }
    partGetter.completeUpload(key, uploadId, partETags);
}

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.
 *///from   w w  w  .  ja  v a 2s.c  om
@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.apache.hadoop.hbase.wal.LogRecoveredEditsOutputSink.java

/**
 * @param completionService threadPool to execute the closing tasks
 * @param thrown store the exceptions//from   w w w  .ja  v  a 2s  .  c  om
 * @param paths arrayList to store the paths written
 * @return if close tasks executed successful
 */
boolean executeCloseTask(CompletionService<Void> completionService, List<IOException> thrown, List<Path> paths)
        throws InterruptedException, ExecutionException {
    for (final Map.Entry<String, WALSplitter.SinkWriter> writersEntry : writers.entrySet()) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Submitting close of " + ((WALSplitter.WriterAndPath) writersEntry.getValue()).path);
        }
        completionService.submit(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                WALSplitter.WriterAndPath wap = (WALSplitter.WriterAndPath) writersEntry.getValue();
                Path dst = closeWriter(writersEntry.getKey(), wap, thrown);
                paths.add(dst);
                return null;
            }
        });
    }
    boolean progress_failed = false;
    for (int i = 0, n = this.writers.size(); i < n; i++) {
        Future<Void> future = completionService.take();
        future.get();
        if (!progress_failed && reporter != null && !reporter.progress()) {
            progress_failed = true;
        }
    }
    return progress_failed;
}

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

public void testPingOnTaskExecFailure() {

    LOG.info("Testing ping failure case - 2");
    conf.setInt(TEST_POINT, 2);//from w  w  w .j a v a2s  .  co  m
    CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(
            this.testBSPTaskService);
    TestBSPProcessRunner runner = new TestBSPProcessRunner(2, 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();
}