Example usage for java.util.concurrent BlockingQueue put

List of usage examples for java.util.concurrent BlockingQueue put

Introduction

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

Prototype

void put(E e) throws InterruptedException;

Source Link

Document

Inserts the specified element into this queue, waiting if necessary for space to become available.

Usage

From source file:org.mitre.mpf.mst.TestSystemStress.java

@Test(timeout = 180 * MINUTES)
public void runFaceOcvDetectImageManyJobs() throws Exception {
    testCtr++;//w ww .j  a v  a  2s.com
    log.info("Beginning test #{} runFaceOcvDetectImageManyJobs()", testCtr);
    IOFileFilter fileFilter = FileFilterUtils.and(FileFilterUtils.fileFileFilter(),
            FileFilterUtils.suffixFileFilter(".jpg"));

    int numExtractors = 6; // number of extractors on Jenkins (* number of nodes, now 1)
    //        int numExtractors = 2;  // number of extractors on local VM * 1 node

    // for testing on local VM only
    //        Collection<File> files = FileUtils.listFiles(new File(getClass().getClassLoader().getResource("samples/face").getFile()),
    //            fileFilter, null);

    // for testing on Jenkins
    // 10,000 jpgs
    Collection<File> files = FileUtils.listFiles(new File("/mpfdata/datasets/mugshots_10000"), fileFilter,
            null);

    BlockingQueue<File> fQueue = new ArrayBlockingQueue<File>(files.size());
    for (File file : files) {
        fQueue.put(file);
    }
    ExecutorService executor = Executors.newFixedThreadPool(numExtractors);
    JobRunner[] jobRunners = new JobRunner[numExtractors];
    for (int i = 0; i < numExtractors; i++) {
        jobRunners[i] = new JobRunner(fQueue);
        executor.submit(jobRunners[i]);
    }
    executor.shutdown();
    executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);

    Assert.assertEquals("Number of files to process doesn't match actual number of jobs run (one job/file):",
            files.size(), manyJobsNumFilesProcessed);
    log.info("Successfully ran {} jobs for {} files, one file per job.", manyJobsNumFilesProcessed,
            files.size());
    log.info("Finished test runFaceOcvDetectImageManyJobs()");
}

From source file:io.fabric8.che.starter.openshift.CheDeploymentConfig.java

private void waitUntilDeploymentConfigIsAvailable(final OpenShiftClient client, String namespace) {
    final BlockingQueue<Object> queue = new ArrayBlockingQueue<Object>(1);

    final Runnable readinessPoller = new Runnable() {
        public void run() {
            try {
                if (isDeploymentAvailable(client, namespace)) {
                    queue.put(true);
                    return;
                } else {
                    queue.put(false);//from www.j a va  2 s .  c  om
                    return;
                }
            } catch (Throwable t) {
                try {
                    if (queue.isEmpty()) {
                        queue.put(false);
                    }
                    return;
                } catch (InterruptedException e) {
                }
            }
        }
    };

    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    ScheduledFuture<?> poller = executor.scheduleWithFixedDelay(readinessPoller, 0, 500, TimeUnit.MILLISECONDS);
    executor.schedule(new Runnable() {

        @Override
        public void run() {
            poller.cancel(true);
        }
    }, Integer.valueOf(startTimeout), TimeUnit.SECONDS);
    try {
        while (!waitUntilReady(queue)) {
        }
    } finally {
        if (!poller.isDone()) {
            poller.cancel(true);
        }
        executor.shutdown();
    }
}

From source file:ubic.gemma.core.apps.ExpressionExperimentDataFileGeneratorCli.java

@Override
protected Exception doWork(String[] args) {

    Exception exp = this.processCommandLine(args);
    if (exp != null) {
        return exp;
    }/*from   w w  w .  j av a  2  s  .co m*/

    BlockingQueue<BioAssaySet> queue = new ArrayBlockingQueue<>(expressionExperiments.size());

    // Add the Experiments to the queue for processing
    for (BioAssaySet ee : expressionExperiments) {
        if (ee instanceof ExpressionExperiment) {
            try {
                queue.put(ee);
            } catch (InterruptedException ie) {
                AbstractCLI.log.info(ie);
            }
        } else {
            throw new UnsupportedOperationException("Can't handle non-EE BioAssaySets yet");
        }

    }

    // Inner class for processing the experiments
    class Worker extends Thread {
        private SecurityContext context;
        private BlockingQueue<BioAssaySet> q;

        private Worker(BlockingQueue<BioAssaySet> q, SecurityContext context) {
            this.context = context;
            this.q = q;
        }

        @Override
        public void run() {

            SecurityContextHolder.setContext(this.context);

            while (true) {
                BioAssaySet ee = q.poll();
                if (ee == null) {
                    break;
                }
                AbstractCLI.log.info("Processing Experiment: " + ee.getName());
                ExpressionExperimentDataFileGeneratorCli.this.processExperiment((ExpressionExperiment) ee);

            }

        }
    }

    final SecurityContext context = SecurityContextHolder.getContext();

    Collection<Thread> threads = new ArrayList<>();

    for (int i = 1; i <= this.numThreads; i++) {
        Worker worker = new Worker(queue, context);
        threads.add(worker);
        AbstractCLI.log.info("Starting thread " + i);
        worker.start();
    }

    this.waitForThreadPoolCompletion(threads);

    this.summarizeProcessing();

    return null;

}

From source file:ubic.gemma.apps.ExpressionExperimentDataFileGeneratorCli.java

@Override
protected Exception doWork(String[] args) {

    Exception exp = processCommandLine(DESCRIPTION, args);
    if (exp != null) {
        return exp;
    }/*from w w  w. j  av  a2 s  .  com*/

    BlockingQueue<BioAssaySet> queue = new ArrayBlockingQueue<BioAssaySet>(expressionExperiments.size());

    // Add the Experiments to the queue for processing
    for (BioAssaySet ee : expressionExperiments) {
        if (ee instanceof ExpressionExperiment) {
            try {
                queue.put(ee);
            } catch (InterruptedException ie) {
                log.info(ie);
            }
        } else {
            throw new UnsupportedOperationException("Can't handle non-EE BioAssaySets yet");
        }

    }

    // Inner class for processing the experiments
    class Worker extends Thread {
        BlockingQueue<BioAssaySet> q;
        SecurityContext context;

        Worker(BlockingQueue<BioAssaySet> q, SecurityContext context) {
            this.context = context;
            this.q = q;
        }

        @Override
        public void run() {

            SecurityContextHolder.setContext(this.context);

            while (true) {
                BioAssaySet ee = q.poll();
                if (ee == null) {
                    break;
                }
                log.info("Processing Experiment: " + ee.getName());
                processExperiment((ExpressionExperiment) ee);

            }

        }
    }

    final SecurityContext context = SecurityContextHolder.getContext();

    Collection<Thread> threads = new ArrayList<Thread>();

    for (int i = 1; i <= this.numThreads; i++) {
        Worker worker = new Worker(queue, context);
        threads.add(worker);
        log.info("Starting thread " + i);
        worker.start();
    }

    waitForThreadPoolCompletion(threads);

    summarizeProcessing();

    return null;

}

From source file:com.netflix.curator.framework.recipes.queue.TestDistributedPriorityQueue.java

@Test
public void testSortingWhileTaking() throws Exception {
    Timing timing = new Timing();
    DistributedPriorityQueue<Integer> queue = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(),
            timing.connection(), new RetryOneTime(1));
    client.start();//from w  ww  . jav a  2 s  .c o m
    try {
        final BlockingQueue<Integer> blockingQueue = new SynchronousQueue<Integer>();
        QueueConsumer<Integer> consumer = new QueueConsumer<Integer>() {
            @Override
            public void consumeMessage(Integer message) throws Exception {
                blockingQueue.put(message);
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        queue = QueueBuilder.builder(client, consumer, new IntSerializer(), "/test").buildPriorityQueue(0);
        queue.start();

        for (int i = 0; i < 10; ++i) {
            queue.put(i, 10);
        }

        Assert.assertEquals(blockingQueue.poll(timing.seconds(), TimeUnit.SECONDS), new Integer(0));
        timing.sleepABit();
        queue.put(1000, 1); // lower priority
        timing.sleepABit();
        Assert.assertEquals(blockingQueue.poll(timing.seconds(), TimeUnit.SECONDS), new Integer(1)); // is in consumer already
        Assert.assertEquals(blockingQueue.poll(timing.seconds(), TimeUnit.SECONDS), new Integer(1000));
    } finally {
        IOUtils.closeQuietly(queue);
        IOUtils.closeQuietly(client);
    }
}

From source file:net.yacy.cora.federate.opensearch.SRURSSConnector.java

public static Thread searchSRURSS(final BlockingQueue<RSSMessage> queue, final String urlBase,
        final String query, final long timeoutInit, final int maximumRecordsInit, final CacheStrategy verify,
        final boolean global, final ClientIdentification.Agent agent) {
    final Thread job = new Thread("searchSRURSS:" + urlBase) {
        @Override//from   w  w  w . j  a  v a 2 s .  c om
        public void run() {
            int startRecord = 0;
            RSSMessage message;
            int maximumRecords = maximumRecordsInit;
            long timeout = timeoutInit;
            mainloop: while (timeout > 0 && maximumRecords > 0) {
                final long st = System.currentTimeMillis();
                RSSFeed feed;
                try {
                    feed = loadSRURSS(urlBase, query, startRecord, recordsPerSession, verify, global, agent);
                } catch (final IOException e1) {
                    break mainloop;
                }
                if (feed == null || feed.isEmpty())
                    break mainloop;
                maximumRecords -= feed.size();
                innerloop: while (!feed.isEmpty()) {
                    message = feed.pollMessage();
                    if (message == null)
                        break innerloop;
                    try {
                        queue.put(message);
                    } catch (final InterruptedException e) {
                        break innerloop;
                    }
                }
                startRecord += recordsPerSession;
                timeout -= System.currentTimeMillis() - st;
            }
            try {
                queue.put(RSSMessage.POISON);
            } catch (final InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    job.start();
    return job;
}

From source file:org.apache.s4.core.classloader.TestClassLoader.java

@Test
public void testInitialDeploymentFromFileSystem() throws Exception {

    File producerS4R = new File(producerS4rDir, "testApp-0.0.0-SNAPSHOT.s4r");
    String uriProducer = producerS4R.toURI().toString();

    initializeS4Node();/*from  w  ww  .ja va  2  s  .  c o  m*/

    final BlockingQueue<Object> queue = new ArrayBlockingQueue<Object>(1);
    zkClient.subscribeDataChanges("/s4/classLoader", new IZkDataListener() {

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
        }

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
            queue.put(data);
        }
    });
    DeploymentUtils.initAppConfig(new AppConfig.Builder().appURI(uriProducer).build(), PRODUCER_CLUSTER, true,
            "localhost:2181");

    Object classLoaderRecord = queue.poll(20, TimeUnit.SECONDS);
    assertTrue("Stored record has unexpected type", classLoaderRecord instanceof ZNRecord);
    ZNRecord record = (ZNRecord) classLoaderRecord;

    assertEquals("Unexpected classloader runs the app init()", S4RLoader.class.getName(), record.getId());
}

From source file:com.opengamma.bbg.replay.BloombergTickWriter.java

/**
 * @param ticks// w w  w  .  ja v a2 s .c om
 */
private void buildSecurityMapQueue(List<FudgeMsg> ticks) {
    for (FudgeMsg fudgeMsg : ticks) {
        String securityDes = fudgeMsg.getString(SECURITY_KEY);
        if (_securityMapQueue.containsKey(securityDes)) {
            BlockingQueue<FudgeMsg> queue = _securityMapQueue.get(securityDes);
            try {
                queue.put(fudgeMsg);
            } catch (InterruptedException e) {
                Thread.interrupted();
                s_logger.warn("interrupted from putting message on queue");
            }
        } else {
            LinkedBlockingQueue<FudgeMsg> queue = new LinkedBlockingQueue<FudgeMsg>();
            try {
                queue.put(fudgeMsg);
            } catch (InterruptedException e) {
                Thread.interrupted();
                s_logger.warn("interrupted from putting message on queue");
            }
            _securityMapQueue.put(securityDes, queue);
        }
    }
}

From source file:it.infn.ct.futuregateway.apiserver.inframanager.state.Running.java

@Override
public final void action(final ExecutorService anExecutorService, final BlockingQueue<Task> aBlockingQueue,
        final Storage aStorage) {

    try {//from   w  w w .ja v a2s. co m
        final Job job = CustomJobFactory.createJob(this.task, aStorage);
        final State state = job.getState();

        this.task.updateCheckTime();
        switch (state) {
        case DONE:
            this.task.setState(Task.STATE.DONE);
            break;
        case RUNNING:
            try {
                aBlockingQueue.put(this.task);
            } catch (InterruptedException ex) {
                this.log.error(ex.getMessage());
                this.task.setState(Task.STATE.ABORTED);
            }
            break;
        case CANCELED:
            this.task.setState(Task.STATE.CANCELLED);
            break;
        case FAILED:
        case NEW:
        case SUSPENDED:
            this.task.setState(Task.STATE.ABORTED);
            break;
        default:
            this.log.error("Task: " + this.task.getId() + " is in a invalid state: " + state);
            this.task.setState(Task.STATE.ABORTED);
            break;
        }
    } catch (InfrastructureException | BadParameterException | DoesNotExistException | NotImplementedException
            | TimeoutException | NoSuccessException ex) {
        final String msg = ex.getMessage();
        this.log.error("Error checking job status: " + msg);
    }

}

From source file:com.tesobe.obp.transport.spi.ConnectorNov2016Test.java

@Before
public void connector() {
    Transport.Factory factory = Transport.factory(Transport.Version.Nov2016, Transport.Encoding.json)
            .map(Function.identity()).orElseThrow(IllegalArgumentException::new);
    Receiver receiver = new ReceiverNov2016(new MockResponder(), factory.codecs());
    final BlockingQueue<String> in = new SynchronousQueue<>();
    final BlockingQueue<Message> out = new SynchronousQueue<>();
    final Sender sender = request -> {
        out.put(request);

        return in.take();
    };//  w  w  w  . j  av a  2s  . c  om

    // north: sender
    connector = factory.connector(sender);

    // south: receiver in a background thread
    service.submit(new Callable<Void>() {
        @Override
        @SuppressWarnings({ "InfiniteLoopStatement" })
        public Void call() throws InterruptedException {
            for (;;) {
                in.put(receiver.respond(out.take()));
            }
        }
    });
}