Example usage for java.util.concurrent ExecutorCompletionService ExecutorCompletionService

List of usage examples for java.util.concurrent ExecutorCompletionService ExecutorCompletionService

Introduction

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

Prototype

public ExecutorCompletionService(Executor executor) 

Source Link

Document

Creates an ExecutorCompletionService using the supplied executor for base task execution and a LinkedBlockingQueue as a completion queue.

Usage

From source file:org.mule.module.db.performance.LoadGenerator.java

public void generateLoad(final LoadTask loadTask) throws InterruptedException, ExecutionException {
    Collection<Callable<Integer>> solvers = new ArrayList<Callable<Integer>>(getThreadCount());
    for (int i = 1; i <= getThreadCount(); i++) {
        solvers.add(new Callable<Integer>() {
            public Integer call() throws Exception {

                for (int message = 1; message <= getMessagesPerThread(); message++) {
                    try {

                        loadTask.execute(message);
                    } catch (Exception e) {
                        // Ignore and continue
                        logger.error("Error sending message: " + e.getMessage());
                    }/* w w  w  .ja v a  2 s .  c om*/
                    Thread.sleep(getMessageDelay());
                }

                return getMessagesPerThread();
            }
        });
    }
    ExecutorService exec = Executors.newFixedThreadPool(getThreadCount());

    CompletionService<Integer> executorCompletionService = new ExecutorCompletionService<Integer>(exec);
    for (Callable<Integer> s : solvers) {
        executorCompletionService.submit(s);
    }

    Integer count = 0;

    for (int i = 0; i < getThreadCount(); ++i) {
        count = count + executorCompletionService.take().get();
        logger.info("Current row processed count: " + count);
    }

    logger.info("Load generation completed");
}

From source file:com.ironiacorp.http.impl.httpclient3.HttpJobRunnerHttpClient3.java

public void run() {
    ExecutorService executor = Executors.newFixedThreadPool(maxThreadsCount);
    ExecutorCompletionService<HttpJob> queue = new ExecutorCompletionService<HttpJob>(executor);
    List<Future<?>> workers = new ArrayList<Future<?>>();

    for (HttpJob job : jobs) {
        if (HttpMethod.GET == job.getMethod()) {
            GetRequest request = new GetRequest(httpClient, job);
            Future<HttpJob> jobStatus = queue.submit(request);
            workers.add(jobStatus);/* ww w.j a  va2  s .c o  m*/
            continue;
        }
        if (HttpMethod.POST == job.getMethod()) {
            PostRequest request = new PostRequest(httpClient, job);
            Future<HttpJob> jobStatus = queue.submit(request);
            workers.add(jobStatus);
            continue;
        }
        // TODO: job cannot be handled, what to do?
    }

    while (!workers.isEmpty()) {
        Iterator<Future<?>> i = workers.iterator();
        while (i.hasNext()) {
            try {
                Future<?> future = i.next();
                // future.get(timeout, TimeUnit.MILLISECONDS);
                future.get();
                i.remove();
                // } catch (TimeoutException e) {
            } catch (InterruptedException ie) {
                System.out.println(ie.getMessage());
            } catch (ExecutionException ee) {
                System.out.println(ee.getMessage());
                i.remove();
            }
        }
    }

    executor.shutdown();
}

From source file:it.geosolutions.tools.io.CopyTreeTest.java

@Before
public void setUp() throws Exception {

    destMount = new File(TestData.file(this, "."), "test-data2");
    if (!destMount.exists()) {
        new File(destMount, "collector").mkdirs();
    }/*from w  w  w .  ja  v  a2 s  . c o  m*/
    Assert.assertTrue(destMount.exists());

    testFile = TestData.file(this, "collector/569_PRO/RS1_geo8.xml");

    ex = Executors.newFixedThreadPool(2);

    if (ex == null || ex.isTerminated()) {
        throw new IllegalArgumentException(
                "Unable to run asynchronously using a terminated or null ThreadPoolExecutor");
    }

    cs = new ExecutorCompletionService<File>(ex);
}

From source file:nl.b3p.imagetool.ImageManager.java

public ImageManager(List<CombineImageUrl> urls, int maxResponseTime, String uname, String pw) {
    if (urls == null || urls.size() <= 0) {
        throw new IllegalArgumentException();
    }// w w  w. j  a v  a 2  s. com
    log.info(String.format("%s initialized with %d urls, max response time %dms", this.getClass().getName(),
            urls.size(), maxResponseTime));

    threadPool = Executors.newFixedThreadPool(MAX_TREADS);
    pool = new ExecutorCompletionService<ImageCollector>(threadPool);

    B3PCredentials credentials = new B3PCredentials();
    credentials.setUserName(uname);
    credentials.setPassword(pw);
    //preemptive not possible, varying hosts possible
    credentials.setPreemptive(false);

    hcc = new HttpClientConfigured(credentials, maxResponseTime);

    for (CombineImageUrl ciu : urls) {
        ImageCollector ic = null;
        if (ciu instanceof CombineWmsUrl) {
            ic = new ImageCollector(this, ciu, hcc);
        } else if (ciu instanceof CombineArcIMSUrl) {
            ic = new ArcImsImageCollector(this, ciu, hcc);
        } else if (ciu instanceof CombineArcServerUrl) {
            ic = new ArcServerImageCollector(this, ciu, hcc);
        } else {
            ic = new ImageCollector(this, ciu, hcc);
        }
        ics.add(ic);
    }
}

From source file:org.gitana.platform.load.AbstractLoadTest.java

protected List<RunnerResult<V>> execute() throws Exception {
    ExecutorService executorService = createExecutorService();

    CompletionService<V> cs = new ExecutorCompletionService<V>(executorService);

    for (int i = 0; i < getIterationCount(); i++) {
        // create the runner
        Runner<V> runner = createRunner("runner-" + i);
        runner.init();/*from  www.j  a v a  2s .co  m*/

        cs.submit(runner);
    }

    // wait for everything to finish
    List<RunnerResult<V>> results = new ArrayList<RunnerResult<V>>();
    for (int i = 0; i < getIterationCount(); i++) {
        RunnerResult<V> result = null;

        try {
            V v = cs.take().get();

            result = new RunnerResult<V>(v);
        } catch (Exception ex) {
            ex.printStackTrace();

            result = new RunnerResult<V>();
            result.setException(ex);
        }

        results.add(result);
    }

    return results;
}

From source file:org.apache.hadoop.hdfs.server.datanode.TestBatchIbr.java

static ExecutorService createExecutor() throws Exception {
    final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    final ExecutorCompletionService<Path> completion = new ExecutorCompletionService<>(executor);

    // initialize all threads and buffers
    for (int i = 0; i < NUM_THREADS; i++) {
        completion.submit(new Callable<Path>() {
            @Override/* w w  w  .  jav a 2s. co m*/
            public Path call() throws Exception {
                IO_BUF.get();
                VERIFY_BUF.get();
                return null;
            }
        });
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        completion.take().get();
    }
    return executor;
}

From source file:com.amazon.s3.S3ParserTest.java

@Test
void testAmazonParseListAllMyBucketsParallelResponseTime() throws InterruptedException, ExecutionException {
    CompletionService<Boolean> completer = new ExecutorCompletionService<Boolean>(exec);

    for (int i = 0; i < LOOP_COUNT; i++)
        completer.submit(new Callable<Boolean>() {
            public Boolean call() throws IOException {
                runAmazonParseListAllMyBuckets();
                return true;
            }//from w  w w .j  a v a2 s.  c  o  m
        });
    for (int i = 0; i < LOOP_COUNT; i++)
        assert completer.take().get();
}

From source file:it.drwolf.ridire.session.async.IndexerService.java

@Create
public void init() {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    this.completionService = new ExecutorCompletionService<IndexingResult>(executorService);
}

From source file:org.v2020.service.ie.VnaImport.java

/**
 * (non-Javadoc)/*from   w ww .j a v a  2  s  . com*/
 * @see org.v2020.service.ie.IVnaImport#importVna(byte[])
 */
@Override
public void importVna(byte[] vnaFileData) {
    taskExecutor = createExecutor();
    objectImportCompletionService = new ExecutorCompletionService<ObjectImportContext>(taskExecutor);
    linkImportCompletionService = new ExecutorCompletionService<LinkImportContext>(taskExecutor);
    importContext = new ImportContext();
    try {
        Vna vna = new Vna(vnaFileData);
        List<SyncObject> syncObjectList = getSyncObjectList(vna);
        List<MapObjectType> mapObjectTypeList = getMapObjectTypeList(vna);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Starting import of objects...");
        }
        importObjectList(null, syncObjectList, mapObjectTypeList);
        if (LOG.isDebugEnabled()) {
            LOG.debug(number + " objects imported.");
        }
        List<SyncLink> syncLinkList = getSyncLinkList(vna);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Number of links: " + syncLinkList.size() + ", starting import...");
        }
        importLinkList(syncLinkList);
    } catch (Exception e) {
        LOG.error("Error while importing VNA.", e);
    } finally {
        shutdownAndAwaitTermination(taskExecutor);
    }
}

From source file:rk.java.compute.cep.ComputeService.java

public ComputeService(final int numberOfTickSources, final int threadPoolSize, IPriceEventSink eventBus) {
    this(new LinkedBlockingQueue<IPriceTick>(), numberOfTickSources, eventBus);
    executorService = Executors.newFixedThreadPool(threadPoolSize);
    ecs = new ExecutorCompletionService<StopWatch>(executorService);
}