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:ubic.gemma.core.loader.genome.gene.ncbi.NcbiGeneConverter.java

public void convert(final BlockingQueue<NcbiGeneData> geneInfoQueue, final BlockingQueue<Gene> geneQueue) {
    // start up thread to convert a member of geneInfoQueue to a gene/geneproduct/databaseentry
    // then push the gene onto the geneQueue for loading

    Thread convertThread = new Thread(new Runnable() {
        @Override/*from  w  w  w  .  j av a2s  . c o m*/
        @SuppressWarnings("synthetic-access")
        public void run() {
            while (!(sourceDone.get() && geneInfoQueue.isEmpty())) {
                try {
                    NcbiGeneData data = geneInfoQueue.poll();
                    if (data == null) {
                        continue;
                    }
                    Gene converted = NcbiGeneConverter.this.convert(data);

                    if (converted.getProducts().isEmpty()) {
                        log.info("Gene with no products skipped: " + converted);
                        continue;
                    }

                    geneQueue.put(converted);

                } catch (InterruptedException e) {
                    NcbiGeneConverter.log.warn("Interrupted");
                    break;
                } catch (Exception e) {
                    NcbiGeneConverter.log.error(e, e);
                    break;
                }
            }
            producerDone.set(true);
        }
    }, "Converter");

    convertThread.start();
}

From source file:ubic.gemma.loader.genome.gene.ncbi.NcbiGeneConverter.java

public void convert(final BlockingQueue<NcbiGeneData> geneInfoQueue, final BlockingQueue<Gene> geneQueue) {
    // start up thread to convert a member of geneInfoQueue to a gene/geneproduct/databaseentry
    // then push the gene onto the geneQueue for loading

    if (!retainProteinInformation) {
        log.info("Note that protein information will be ignored; set " + RETAIN_PROTEIN_INFO_PARAM
                + " to true to change");
    }/*from   www. ja  v a2  s.  c o m*/

    Thread convertThread = new Thread(new Runnable() {
        @Override
        @SuppressWarnings("synthetic-access")
        public void run() {
            while (!(sourceDone.get() && geneInfoQueue.isEmpty())) {
                try {
                    NcbiGeneData data = geneInfoQueue.poll();
                    if (data == null) {
                        continue;
                    }
                    Gene converted = convert(data);

                    geneQueue.put(converted);

                } catch (InterruptedException e) {
                    log.warn("Interrupted");
                    break;
                } catch (Exception e) {
                    log.error(e, e);
                    break;
                }
            }
            producerDone.set(true);
        }
    }, "Converter");

    convertThread.start();
}

From source file:gdsc.core.clustering.optics.ProjectedMoleculeSpace.java

/**
 * Put.//from   w ww.  j av  a 2 s.  co  m
 *
 * @param <T>
 *            the generic type
 * @param jobs
 *            the jobs
 * @param job
 *            the job
 */
private <T> void put(BlockingQueue<T> jobs, T job) {
    try {
        jobs.put(job);
    } catch (InterruptedException e) {
        throw new RuntimeException("Unexpected interruption", e);
    }
}

From source file:org.apache.camel.component.routebox.seda.RouteboxSedaConsumer.java

private void dispatchToInnerRoute(BlockingQueue<Exchange> queue, final Exchange exchange)
        throws InterruptedException {
    Exchange result;//from w ww . java  2s.  co m

    if (exchange != null) {
        if (isRunAllowed()) {
            try {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Dispatching to inner route: " + exchange);
                }
                RouteboxDispatcher dispatcher = new RouteboxDispatcher(producer);
                result = dispatcher.dispatchAsync(getRouteboxEndpoint(), exchange);
                AsyncProcessorHelper.process(processor, result, new AsyncCallback() {
                    public void done(boolean doneSync) {
                        // noop
                    }
                });
            } catch (Exception e) {
                getExceptionHandler().handleException("Error processing exchange", exchange, e);
            }
        } else {
            if (LOG.isWarnEnabled()) {
                LOG.warn(
                        "This consumer is stopped during polling an exchange, so putting it back on the seda queue: "
                                + exchange);
            }
            queue.put(exchange);
        }
    }
}

From source file:ubic.gemma.loader.protein.StringProteinProteinInteractionConverter.java

/**
 * Threaded conversion of domain objects to Gemma objects.
 *///  w w  w. ja  va  2  s  .c om
public void convert(final BlockingQueue<Gene2GeneProteinAssociation> gene2GeneProteinAssociationQueue,
        final Collection<StringProteinProteinInteraction> stringProteinProteinInteractions) {
    // start up thread to convert a member of geneInfoQueue to a gene/geneproduct/databaseentry
    // then push the gene onto the geneQueue for loading
    Thread convertThread = new Thread(new Runnable() {
        @Override
        @SuppressWarnings("synthetic-access")
        public void run() {

            try {
                for (StringProteinProteinInteraction stringProteinProteinInteraction : stringProteinProteinInteractions) {
                    if (stringProteinProteinInteraction == null) {
                        continue;
                    }
                    // converter
                    Collection<Gene2GeneProteinAssociation> dataColl = convert(stringProteinProteinInteraction);
                    // this returns a collection so split out and put on queue
                    for (Gene2GeneProteinAssociation gene2GeneProteinAssociation : dataColl) {
                        gene2GeneProteinAssociationQueue.put(gene2GeneProteinAssociation);
                    }
                }
            } catch (InterruptedException e) {
                log.info("Interrupted.");
            }
            producerDone.set(true);
        }

    }, "Converter");

    convertThread.start();
}

From source file:ubic.gemma.core.loader.protein.StringProteinProteinInteractionConverter.java

/**
 * Threaded conversion of domain objects to Gemma objects.
 *
 * @param gene2GeneProteinAssociationQueue queue
 * @param stringProteinProteinInteractions interactions
 *///from   w  ww. j a  va 2s .  c  om
public void convert(final BlockingQueue<Gene2GeneProteinAssociation> gene2GeneProteinAssociationQueue,
        final Collection<StringProteinProteinInteraction> stringProteinProteinInteractions) {
    // start up thread to convert a member of geneInfoQueue to a gene/geneproduct/databaseentry
    // then push the gene onto the geneQueue for loading
    Thread convertThread = new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                for (StringProteinProteinInteraction stringProteinProteinInteraction : stringProteinProteinInteractions) {
                    if (stringProteinProteinInteraction == null) {
                        continue;
                    }
                    // converter
                    Collection<Gene2GeneProteinAssociation> dataColl = StringProteinProteinInteractionConverter.this
                            .convert(stringProteinProteinInteraction);
                    // this returns a collection so split out and put on queue
                    for (Gene2GeneProteinAssociation gene2GeneProteinAssociation : dataColl) {
                        gene2GeneProteinAssociationQueue.put(gene2GeneProteinAssociation);
                    }
                }
            } catch (InterruptedException e) {
                StringProteinProteinInteractionConverter.log.info("Interrupted.");
            }
            producerDone.set(true);
        }

    }, "Converter");

    convertThread.start();
}

From source file:org.apache.usergrid.tools.ImportAdmins.java

@SuppressWarnings("unchecked")
private void importMetadata(String fileName, int writeThreads) throws Exception {

    EntityManager em = emf.getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);

    File metadataFile = new File(importDir, fileName);

    logger.info("----- Loading metadata file: " + metadataFile.getAbsolutePath());

    JsonParser jp = getJsonParserForFile(metadataFile);

    JsonToken jsonToken = null; // jp.nextToken();// START_OBJECT this is the outer hashmap

    int depth = 1;

    BlockingQueue<ImportMetadataTask> workQueue = new LinkedBlockingQueue<ImportMetadataTask>();
    startMetadataWorkers(workQueue, writeThreads);

    while (depth > 0) {

        jsonToken = jp.nextToken();// www  .  j av a2 s  .  c  o m

        if (jsonToken == null) {
            logger.info("token is null, breaking");
            break;
        }

        if (jsonToken.equals(JsonToken.START_OBJECT)) {
            depth++;
        } else if (jsonToken.equals(JsonToken.END_OBJECT)) {
            depth--;
        }

        if (jsonToken.equals(JsonToken.FIELD_NAME) && depth == 2) {

            jp.nextToken();
            String entityOwnerId = jp.getCurrentName();

            try {
                EntityRef entityRef = new SimpleEntityRef("user", UUID.fromString(entityOwnerId));
                Map<String, Object> metadata = (Map<String, Object>) jp.readValueAs(Map.class);

                workQueue.put(new ImportMetadataTask(entityRef, metadata));
                logger.debug("Put user {} in metadata queue", entityRef.getUuid());

            } catch (Exception e) {
                logger.debug("Error with user {}, not putting in metadata queue", entityOwnerId);
            }
        }
    }

    waitForQueueAndMeasure(workQueue, metadataEmptyCount, metadataWorkerThreadMap, "Metadata Load");

    logger.info("----- End of metadata -----");
    jp.close();
}

From source file:com.nridge.connector.fs.con_fs.core.RunTransformFS.java

/**
 * When an object implementing interface <code>Runnable</code> is used
 * to create a thread, starting the thread causes the object's
 * <code>run</code> method to be called in that separately executing
 * thread.//from  w w w .  j  av  a2 s. c om
 * 
 * The general contract of the method <code>run</code> is that it may
 * take any action whatsoever.
 *
 * @see Thread#run()
 */
@Override
public void run() {
    DocumentXML documentXML;
    Document srcDoc, dstDoc;
    String docId, queueItem, srcPathFileName;
    Logger appLogger = mAppMgr.getLogger(this, "run");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    Pipeline pipeline = new Pipeline(mAppMgr, Constants.CFG_PROPERTY_PREFIX);

    BlockingQueue extractQueue = (BlockingQueue) mAppMgr.getProperty(Connector.QUEUE_EXTRACT_NAME);
    BlockingQueue transformQueue = (BlockingQueue) mAppMgr.getProperty(Connector.QUEUE_TRANSFORM_NAME);

    long queueWaitTimeout = mAppMgr.getLong(Constants.CFG_PROPERTY_PREFIX + ".queue.wait_timeout",
            Constants.QUEUE_POLL_TIMEOUT_DEFAULT);
    do {
        try {
            queueItem = (String) extractQueue.poll(queueWaitTimeout, TimeUnit.SECONDS);
            if (mCrawlQueue.isQueueItemDocument(queueItem)) {
                StopWatch stopWatch = new StopWatch();
                stopWatch.start();

                docId = Connector.docIdFromQueueItem(queueItem);

                appLogger.debug(String.format("Extract Queue Item: %s", docId));
                srcPathFileName = mCrawlQueue.docPathFileName(Connector.QUEUE_EXTRACT_NAME, docId);
                try {
                    documentXML = new DocumentXML();
                    documentXML.load(srcPathFileName);
                    srcDoc = documentXML.getDocument();

                    dstDoc = pipeline.execute(srcDoc);

                    mCrawlQueue.transition(Connector.QUEUE_EXTRACT_NAME, Connector.QUEUE_TRANSFORM_NAME, dstDoc,
                            docId);
                    stopWatch.stop();
                    queueItem = Connector.queueItemIdPhaseTime(queueItem, Connector.PHASE_TRANSFORM,
                            stopWatch.getTime());
                    try {
                        // If queue is full, this thread may block.
                        transformQueue.put(queueItem);
                    } catch (InterruptedException e) {
                        // Restore the interrupted status so parent can handle (if it wants to).
                        Thread.currentThread().interrupt();
                    }
                } catch (Exception e) {
                    String msgStr = String.format("%s: %s", docId, e.getMessage());
                    appLogger.error(msgStr, e);
                    MailManager mailManager = (MailManager) mAppMgr.getProperty(Connector.PROPERTY_MAIL_NAME);
                    mailManager.addMessage(Connector.PHASE_TRANSFORM, Connector.STATUS_MAIL_ERROR, msgStr,
                            Constants.MAIL_DETAIL_MESSAGE);
                }
            }
        } catch (InterruptedException e) {
            queueItem = StringUtils.EMPTY;
        }
    } while (!mCrawlQueue.isPhaseComplete(Connector.PHASE_EXTRACT, queueItem));

    // Forward the marker queue item to the next queue.

    if (mCrawlQueue.isQueueItemMarker(queueItem)) {
        try {
            // If queue is full, this thread may block.
            transformQueue.put(queueItem);
        } catch (InterruptedException e) {
            // Restore the interrupted status so parent can handle (if it wants to).
            Thread.currentThread().interrupt();
        }
    }

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:coral.service.ExpServable.java

private void sendRes(BlockingQueue<Message> outQueue, String... res) {
    for (String line : res) {
        try {/*from  www  .  java2 s . co  m*/
            File file = new File(basepath, line);

            long version = file.lastModified();

            logger.info("sync file " + line + " version:" + version + " path " + file.getCanonicalPath());

            try {
                /*
                 * outQueue.put(new Message("ressync:" + file.getName() +
                 * "?show=NO&version=" + version + "&synccmd=" +
                 * URLEncoder.encode("get://host/" +
                 * file.getCanonicalPath(), "utf-8"), new byte[] {}));
                 */

                // InputStream is;
                // byte[] bytes = null;

                String mime = GetServable.getMime(file);

                BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));

                byte[] buffer = new byte[8096]; // == 1024

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int read;
                while ((read = reader.read(buffer)) >= 0) {
                    baos.write(buffer, 0, read);
                }
                byte[] readbytes = baos.toByteArray();

                outQueue.put(new Message("vset", file.getName(), mime, "NO&version=" + version, readbytes));

                reader.close();

            } catch (InterruptedException e) {
                logger.error("i error", e);
            }
        } catch (FileNotFoundException e) {
            logger.error("file not found", e);
        } catch (IOException e) {
            logger.error("I/O", e);
        }
    }
}

From source file:com.nridge.connector.fs.con_fs.core.RunPublishFS.java

/**
 * When an object implementing interface <code>Runnable</code> is used
 * to create a thread, starting the thread causes the object's
 * <code>run</code> method to be called in that separately executing
 * thread.//  w w w . j  av a  2  s.c  o  m
 * 
 * The general contract of the method <code>run</code> is that it may
 * take any action whatsoever.
 *
 * @see Thread#run()
 */
@Override
public void run() {
    Document conDoc;
    DocumentXML documentXML;
    String docId, queueItem, srcPathFileName;
    Logger appLogger = mAppMgr.getLogger(this, "run");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    boolean isPublisherInitialized = true;
    DataBag schemaBag = (DataBag) mAppMgr.getProperty(Connector.PROPERTY_SCHEMA_NAME);
    Publishers indexPublishers = new Publishers(mAppMgr, mCrawlQueue, Constants.CFG_PROPERTY_PREFIX);
    try {
        indexPublishers.initialize(schemaBag);
    } catch (NSException e) {
        isPublisherInitialized = false;
        appLogger.error("Publisher initialization: " + e.getMessage());
    }

    if (isPublisherInitialized) {
        BlockingQueue transformQueue = (BlockingQueue) mAppMgr.getProperty(Connector.QUEUE_TRANSFORM_NAME);
        BlockingQueue publishQueue = (BlockingQueue) mAppMgr.getProperty(Connector.QUEUE_PUBLISH_NAME);

        long queueWaitTimeout = mAppMgr.getLong(Constants.CFG_PROPERTY_PREFIX + ".queue.wait_timeout",
                Constants.QUEUE_POLL_TIMEOUT_DEFAULT);
        do {
            try {
                queueItem = (String) transformQueue.poll(queueWaitTimeout, TimeUnit.SECONDS);
                if (mCrawlQueue.isQueueItemDocument(queueItem)) {
                    StopWatch stopWatch = new StopWatch();
                    stopWatch.start();

                    docId = Connector.docIdFromQueueItem(queueItem);

                    appLogger.debug(String.format("Transform Queue Item: %s", docId));
                    srcPathFileName = mCrawlQueue.docPathFileName(Connector.QUEUE_TRANSFORM_NAME, docId);
                    try {
                        documentXML = new DocumentXML();
                        documentXML.load(srcPathFileName);
                        conDoc = documentXML.getDocument();

                        indexPublishers.send(conDoc);

                        File srcFile = new File(srcPathFileName);
                        if (!srcFile.delete())
                            appLogger.warn(String.format("%s: Unable to delete.", srcPathFileName));

                        stopWatch.stop();
                        queueItem = Connector.queueItemIdPhaseTime(queueItem, Connector.PHASE_PUBLISH,
                                stopWatch.getTime());
                        try {
                            // If queue is full, this thread may block.
                            publishQueue.put(queueItem);
                        } catch (InterruptedException e) {
                            // Restore the interrupted status so parent can handle (if it wants to).
                            Thread.currentThread().interrupt();
                        }
                    } catch (Exception e) {
                        String msgStr = String.format("%s: %s", docId, e.getMessage());
                        appLogger.error(msgStr, e);
                        MailManager mailManager = (MailManager) mAppMgr
                                .getProperty(Connector.PROPERTY_MAIL_NAME);
                        mailManager.addMessage(Connector.PHASE_PUBLISH, Connector.STATUS_MAIL_ERROR, msgStr,
                                Constants.MAIL_DETAIL_MESSAGE);
                    }
                }
            } catch (InterruptedException e) {
                queueItem = StringUtils.EMPTY;
            }
        } while (!mCrawlQueue.isPhaseComplete(Connector.PHASE_TRANSFORM, queueItem));

        // Forward the marker queue item to the next queue.

        if (mCrawlQueue.isQueueItemMarker(queueItem)) {
            try {
                // If queue is full, this thread may block.
                publishQueue.put(queueItem);
            } catch (InterruptedException e) {
                // Restore the interrupted status so parent can handle (if it wants to).
                Thread.currentThread().interrupt();
            }
        }

        // Now we can shutdown our search indexer publisher.

        try {
            indexPublishers.shutdown();
        } catch (NSException e) {
            appLogger.error("Publisher shutdown: " + e.getMessage());
        }
    }

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}