Example usage for java.util Queue size

List of usage examples for java.util Queue size

Introduction

In this page you can find the example usage for java.util Queue size.

Prototype

int size();

Source Link

Document

Returns the number of elements in this collection.

Usage

From source file:org.apache.camel.component.ibatis.IBatisPollingConsumer.java

public int processBatch(Queue<Object> exchanges) throws Exception {
    final IBatisEndpoint endpoint = getEndpoint();

    int total = exchanges.size();

    // limit if needed
    if (maxMessagesPerPoll > 0 && total > maxMessagesPerPoll) {
        LOG.debug("Limiting to maximum messages to poll " + maxMessagesPerPoll + " as there was " + total
                + " messages in this poll.");
        total = maxMessagesPerPoll;//from www  .j  a  va 2s  .  com
    }

    for (int index = 0; index < total && isBatchAllowed(); index++) {
        // only loop if we are started (allowed to run)
        DataHolder holder = ObjectHelper.cast(DataHolder.class, exchanges.poll());
        Exchange exchange = holder.exchange;
        Object data = holder.data;

        // add current index and total as properties
        exchange.setProperty(Exchange.BATCH_INDEX, index);
        exchange.setProperty(Exchange.BATCH_SIZE, total);
        exchange.setProperty(Exchange.BATCH_COMPLETE, index == total - 1);

        // update pending number of exchanges
        pendingExchanges = total - index - 1;

        // process the current exchange
        if (LOG.isDebugEnabled()) {
            LOG.debug("Processing exchange: " + exchange);
        }
        getProcessor().process(exchange);

        try {
            if (onConsume != null) {
                endpoint.getProcessingStrategy().commit(endpoint, exchange, data, onConsume);
            }
        } catch (Exception e) {
            handleException(e);
        }
    }

    return total;
}

From source file:org.apache.predictionio.examples.java.recommendations.tutorial1.Algorithm.java

private void setTopItemSimilarity(Map<Integer, Queue<IndexAndScore>> topItemSimilarity, Integer itemID1,
        Integer index2, double score, int capacity, Comparator<IndexAndScore> comparator) {
    Queue<IndexAndScore> queue = topItemSimilarity.get(itemID1);
    if (queue == null) {
        queue = new PriorityQueue<IndexAndScore>(capacity, comparator);
        topItemSimilarity.put(itemID1, queue);
    }/*w  w w . jav  a2 s  .  co  m*/
    IndexAndScore entry = new IndexAndScore(index2, score);
    if (queue.size() < capacity)
        queue.add(entry);
    else if (comparator.compare(queue.peek(), entry) < 0) {
        queue.poll();
        queue.add(entry);
    }
}

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

private void useOffer(SchedulerDriver schedulerDriver, Offer offer) {
    log.debug("Received offer From: " + offer.getHostname());

    String host = offer.getHostname();

    ResourceOffer ro = new ResourceOffer(offer);
    int maxSlots = ro.getMaxSlots();
    if (maxSlots == 0) {
        schedulerDriver.declineOffer(offer.getId());
        return;/*from  w w  w .j a va 2s .  c om*/
    }

    java.util.Queue<TaskInProgress> tasks = new LinkedList<TaskInProgress>();

    while (tasks.size() < maxSlots) {
        TaskInProgress tip = null;
        if (tasksToRunByGroom.get(host) != null) {
            tip = tasksToRunByGroom.get(host).poll();
        }

        if (tip == null) {
            tip = tasksToRunByGroom.get(anyGroomServer).poll();
            if (tip == null) {
                if (tasks.isEmpty()) {
                    schedulerDriver.declineOffer(offer.getId());
                }

                break;
            }
        }
        if (executingTasks.contains(tip)) {
            continue;
        }

        executingTasks.add(tip);
        tasksToRun.remove(tip);

        tasks.add(tip);

        log.debug("Found offer for: " + tip.getTaskId());
    }

    if (!tasks.isEmpty()) {
        launchTasks(schedulerDriver, tasks, ro);
    }
}

From source file:it.geosolutions.geobatch.geotiff.overview.GeotiffOverviewsEmbedder.java

public Queue<FileSystemEvent> execute(Queue<FileSystemEvent> events) throws ActionException {

    try {// ww  w .j  a  va2 s.c  o  m
        // looking for file
        if (events.size() == 0)
            throw new IllegalArgumentException(
                    "GeotiffOverviewsEmbedder::execute(): Wrong number of elements for this action: "
                            + events.size());

        listenerForwarder.setTask("config");
        listenerForwarder.started();

        // //
        //
        // data flow configuration and dataStore name must not be null.
        //
        // //
        if (configuration == null) {
            final String message = "GeotiffOverviewsEmbedder::execute(): DataFlowConfig is null.";
            if (LOGGER.isErrorEnabled())
                LOGGER.error(message);
            throw new IllegalStateException(message);
        }

        // //
        //
        // check the configuration and prepare the overviews embedder
        //
        // //
        final int downsampleStep = configuration.getDownsampleStep();
        if (downsampleStep <= 0)
            throw new IllegalArgumentException(
                    "GeotiffOverviewsEmbedder::execute(): Illegal downsampleStep: " + downsampleStep);

        int numberOfSteps = configuration.getNumSteps();
        if (numberOfSteps <= 0)
            throw new IllegalArgumentException("Illegal numberOfSteps: " + numberOfSteps);

        final OverviewsEmbedder oe = new OverviewsEmbedder();
        oe.setDownsampleStep(downsampleStep);
        oe.setNumSteps(configuration.getNumSteps());
        // SG: this way we are sure we use the standard tile cache
        oe.setTileCache(JAI.getDefaultInstance().getTileCache());

        String scaleAlgorithm = configuration.getScaleAlgorithm();
        if (scaleAlgorithm == null) {
            LOGGER.warn("No scaleAlgorithm defined. Using " + SubsampleAlgorithm.Nearest + " as default");
            scaleAlgorithm = SubsampleAlgorithm.Nearest.name();
        } else {
            final SubsampleAlgorithm algorithm = SubsampleAlgorithm.valueOf(scaleAlgorithm);
            if (algorithm == null) {
                throw new IllegalStateException("Bad scaleAlgorithm defined [" + scaleAlgorithm + "]");
            }
        }
        oe.setScaleAlgorithm(scaleAlgorithm);
        oe.setTileHeight(configuration.getTileH());
        oe.setTileWidth(configuration.getTileW());

        /*
         * TODO check this is formally wrong! this should be done into the
         * configuration.
         */
        // add logger/listener
        if (configuration.isLogNotification())
            oe.addProcessingEventListener(new ProcessingEventListener() {

                public void exceptionOccurred(ExceptionEvent event) {
                    if (LOGGER.isInfoEnabled())
                        LOGGER.info("GeotiffOverviewsEmbedder::execute(): " + event.getMessage(),
                                event.getException());
                }

                public void getNotification(ProcessingEvent event) {
                    if (LOGGER.isInfoEnabled())
                        LOGGER.info("GeotiffOverviewsEmbedder::execute(): " + event.getMessage());
                    listenerForwarder.progressing((float) event.getPercentage(), event.getMessage());
                }
            });

        // The return
        Queue<FileSystemEvent> ret = new LinkedList<FileSystemEvent>();

        while (events.size() > 0) {

            // run
            listenerForwarder.progressing(0, "Embedding overviews");

            final FileSystemEvent event = events.remove();

            final File eventFile = event.getSource();
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Processing file " + eventFile);

            if (eventFile.exists() && eventFile.canRead() && eventFile.canWrite()) {
                /*
                 * If here: we can start retiler actions on the incoming
                 * file event
                 */

                if (eventFile.isDirectory()) {

                    final FileFilter filter = new RegexFileFilter(".+\\.[tT][iI][fF]([fF]?)");
                    final Collector collector = new Collector(filter);
                    final List<File> fileList = collector.collect(eventFile);
                    int size = fileList.size();
                    for (int progress = 0; progress < size; progress++) {

                        final File inFile = fileList.get(progress);

                        try {
                            oe.setSourcePath(inFile.getAbsolutePath());
                            oe.run();
                        } catch (UnsupportedOperationException uoe) {
                            listenerForwarder.failed(uoe);
                            if (LOGGER.isWarnEnabled())
                                LOGGER.warn("GeotiffOverviewsEmbedder::execute(): " + uoe.getLocalizedMessage(),
                                        uoe);
                        } catch (IllegalArgumentException iae) {
                            listenerForwarder.failed(iae);
                            if (LOGGER.isWarnEnabled())
                                LOGGER.warn("GeotiffOverviewsEmbedder::execute(): " + iae.getLocalizedMessage(),
                                        iae);
                        } finally {
                            listenerForwarder.setProgress((progress * 100) / ((size != 0) ? size : 1));
                            listenerForwarder.progressing();
                        }
                    }
                } else {
                    // file is not a directory
                    try {
                        oe.setSourcePath(eventFile.getAbsolutePath());
                        oe.run();
                    } catch (UnsupportedOperationException uoe) {
                        listenerForwarder.failed(uoe);
                        if (LOGGER.isWarnEnabled())
                            LOGGER.warn("GeotiffOverviewsEmbedder::execute(): " + uoe.getLocalizedMessage(),
                                    uoe);
                    } catch (IllegalArgumentException iae) {
                        listenerForwarder.failed(iae);
                        if (LOGGER.isWarnEnabled())
                            LOGGER.warn("GeotiffOverviewsEmbedder::execute(): " + iae.getLocalizedMessage(),
                                    iae);
                    } finally {
                        listenerForwarder.setProgress(100 / ((events.size() != 0) ? events.size() : 1));
                    }
                }

                // add the directory to the return
                ret.add(event);
            } else {
                final String message = "GeotiffOverviewsEmbedder::execute(): The passed file event refers to a not existent "
                        + "or not readable/writeable file! File: " + eventFile.getAbsolutePath();
                if (LOGGER.isWarnEnabled())
                    LOGGER.warn(message);

                throw new ActionException(this, message);

            }
        } // endwile
        listenerForwarder.completed();

        // return
        if (ret.size() > 0) {
            events.clear();
            return ret;
        } else {
            /*
             * If here: we got an error no file are set to be returned the
             * input queue is returned
             */
            return events;
        }
    } catch (Exception t) {
        final String message = "GeotiffOverviewsEmbedder::execute(): " + t.getLocalizedMessage();
        if (LOGGER.isErrorEnabled())
            LOGGER.error(message, t);
        final ActionException exc = new ActionException(this, message, t);
        listenerForwarder.failed(exc);
        throw exc;
    }
}

From source file:it.geosolutions.geobatch.geotiff.overview.GeotiffOverviewsEmbedderAction.java

@Override
public Queue<FileSystemEvent> execute(Queue<FileSystemEvent> events) throws ActionException {

    try {//from www.j  a va  2  s . c  om
        // looking for file
        if (events.size() == 0)
            throw new IllegalArgumentException(
                    "GeotiffOverviewsEmbedder::execute(): Wrong number of elements for this action: "
                            + events.size());

        listenerForwarder.setTask("config");
        listenerForwarder.started();

        // //
        //
        // data flow configuration and dataStore name must not be null.
        //
        // //
        if (configuration == null) {
            final String message = "GeotiffOverviewsEmbedder::execute(): DataFlowConfig is null.";
            if (LOGGER.isErrorEnabled())
                LOGGER.error(message);
            throw new IllegalStateException(message);
        }

        // //
        //
        // check the configuration and prepare the overviews embedder
        //
        // //
        final int downsampleStep = configuration.getDownsampleStep();
        if (downsampleStep <= 0)
            throw new IllegalArgumentException(
                    "GeotiffOverviewsEmbedder::execute(): Illegal downsampleStep: " + downsampleStep);

        int numberOfSteps = configuration.getNumSteps();
        if (numberOfSteps <= 0)
            throw new IllegalArgumentException("Illegal numberOfSteps: " + numberOfSteps);

        final OverviewsEmbedder oe = new OverviewsEmbedder();
        oe.setDownsampleStep(downsampleStep);
        oe.setNumSteps(configuration.getNumSteps());
        // SG: this way we are sure we use the standard tile cache
        oe.setTileCache(JAI.getDefaultInstance().getTileCache());

        String scaleAlgorithm = configuration.getScaleAlgorithm();
        if (scaleAlgorithm == null) {
            LOGGER.warn("No scaleAlgorithm defined. Using " + SubsampleAlgorithm.Nearest + " as default");
            scaleAlgorithm = SubsampleAlgorithm.Nearest.name();
        } else {
            final SubsampleAlgorithm algorithm = SubsampleAlgorithm.valueOf(scaleAlgorithm);
            if (algorithm == null) {
                throw new IllegalStateException("Bad scaleAlgorithm defined [" + scaleAlgorithm + "]");
            }
        }
        oe.setScaleAlgorithm(scaleAlgorithm);
        oe.setTileHeight(configuration.getTileH());
        oe.setTileWidth(configuration.getTileW());

        /*
         * TODO check this is formally wrong! this should be done into the
         * configuration.
         */
        // add logger/listener
        if (configuration.isLogNotification())
            oe.addProcessingEventListener(new ProcessingEventListener() {

                public void exceptionOccurred(ExceptionEvent event) {
                    if (LOGGER.isInfoEnabled())
                        LOGGER.info("GeotiffOverviewsEmbedder::execute(): " + event.getMessage(),
                                event.getException());
                }

                public void getNotification(ProcessingEvent event) {
                    if (LOGGER.isInfoEnabled())
                        LOGGER.info("GeotiffOverviewsEmbedder::execute(): " + event.getMessage());
                    listenerForwarder.progressing((float) event.getPercentage(), event.getMessage());
                }
            });

        // The return
        Queue<FileSystemEvent> ret = new LinkedList<FileSystemEvent>();

        while (events.size() > 0) {

            // run
            listenerForwarder.progressing(0, "Embedding overviews");

            final FileSystemEvent event = events.remove();

            final File eventFile = event.getSource();
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Processing file " + eventFile);

            if (eventFile.exists() && eventFile.canRead() && eventFile.canWrite()) {
                /*
                 * If here: we can start retiler actions on the incoming
                 * file event
                 */

                if (eventFile.isDirectory()) {

                    final FileFilter filter = new RegexFileFilter(".+\\.[tT][iI][fF]([fF]?)");
                    final Collector collector = new Collector(filter);
                    final List<File> fileList = collector.collect(eventFile);
                    int size = fileList.size();
                    for (int progress = 0; progress < size; progress++) {

                        final File inFile = fileList.get(progress);

                        try {
                            oe.setSourcePath(inFile.getAbsolutePath());
                            oe.run();
                        } catch (UnsupportedOperationException uoe) {
                            listenerForwarder.failed(uoe);
                            if (LOGGER.isWarnEnabled())
                                LOGGER.warn("GeotiffOverviewsEmbedder::execute(): " + uoe.getLocalizedMessage(),
                                        uoe);
                        } catch (IllegalArgumentException iae) {
                            listenerForwarder.failed(iae);
                            if (LOGGER.isWarnEnabled())
                                LOGGER.warn("GeotiffOverviewsEmbedder::execute(): " + iae.getLocalizedMessage(),
                                        iae);
                        } finally {
                            listenerForwarder.setProgress((progress * 100) / ((size != 0) ? size : 1));
                            listenerForwarder.progressing();
                        }
                    }
                } else {
                    // file is not a directory
                    try {
                        oe.setSourcePath(eventFile.getAbsolutePath());
                        oe.run();
                    } catch (UnsupportedOperationException uoe) {
                        listenerForwarder.failed(uoe);
                        if (LOGGER.isWarnEnabled())
                            LOGGER.warn("GeotiffOverviewsEmbedder::execute(): " + uoe.getLocalizedMessage(),
                                    uoe);
                    } catch (IllegalArgumentException iae) {
                        listenerForwarder.failed(iae);
                        if (LOGGER.isWarnEnabled())
                            LOGGER.warn("GeotiffOverviewsEmbedder::execute(): " + iae.getLocalizedMessage(),
                                    iae);
                    } finally {
                        listenerForwarder.setProgress(100 / ((events.size() != 0) ? events.size() : 1));
                    }
                }

                // add the directory to the return
                ret.add(event);
            } else {
                final String message = "GeotiffOverviewsEmbedder::execute(): The passed file event refers to a not existent "
                        + "or not readable/writeable file! File: " + eventFile.getAbsolutePath();
                if (LOGGER.isWarnEnabled())
                    LOGGER.warn(message);

                throw new ActionException(this, message);

            }
        } // endwile
        listenerForwarder.completed();

        // return
        if (ret.size() > 0) {
            events.clear();
            return ret;
        } else {
            /*
             * If here: we got an error no file are set to be returned the
             * input queue is returned
             */
            return events;
        }
    } catch (Exception t) {
        final String message = "GeotiffOverviewsEmbedder::execute(): " + t.getLocalizedMessage();
        if (LOGGER.isErrorEnabled())
            LOGGER.error(message, t);
        final ActionException exc = new ActionException(this, message, t);
        listenerForwarder.failed(exc);
        throw exc;
    }
}

From source file:com.nearinfinity.hbase.dsl.HBase.java

protected void savePut(byte[] tableName, Put put) {
    Queue<Put> puts = getPuts(tableName);
    if (puts.size() >= MAX_QUEUE_SIZE) {
        flushPuts(tableName, puts);//w  ww . ja v  a 2 s . com
    }
    puts.add(put);
}

From source file:com.nearinfinity.hbase.dsl.HBase.java

protected void saveDelete(byte[] tableName, Delete delete) {
    Queue<Delete> deletes = getDeletes(tableName);
    if (deletes.size() >= MAX_QUEUE_SIZE) {
        flushDeletes(tableName, deletes);
    }//from  ww  w.ja v  a 2 s . c  o  m
    deletes.add(delete);
}

From source file:org.finra.datagenerator.engine.negscxml.NegSCXMLFrontier.java

private void dfs(Queue<Map<String, String>> queue, AtomicBoolean flag, NegPossibleState state) {
    if (flag.get()) {
        return;//from   w w  w  .  j a v a2 s  . co  m
    }

    //reached end of chart, valid assignment found only if a negative value is set
    if (state.nextState.getId().equalsIgnoreCase("end")) {
        if (state.negVariable.size() == negative) {
            queue.add(state.variables);

            if (queue.size() > 10000) {
                log.info("Queue size " + queue.size() + ", waiting");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    log.info("Interrupted ", ex);
                }
            }

            return;
        }
    }

    List<NegPossibleState> expand = new LinkedList<>();

    expandPositive(state, expand);
    if (state.negVariable.size() < negative) {
        expandNegative(state, expand, negative - state.negVariable.size());
    }

    for (NegPossibleState e : expand) {
        dfs(queue, flag, e);
    }
}

From source file:org.apache.camel.component.file.GenericFileConsumer.java

@SuppressWarnings("unchecked")
public int processBatch(Queue<Object> exchanges) {
    int total = exchanges.size();

    // limit if needed
    if (maxMessagesPerPoll > 0 && total > maxMessagesPerPoll) {
        if (log.isDebugEnabled()) {
            log.debug("Limiting to maximum messages to poll " + maxMessagesPerPoll + " as there was " + total
                    + " messages in this poll.");
        }/*from   w  w w.ja  v  a  2 s .  c  o m*/
        total = maxMessagesPerPoll;
    }

    for (int index = 0; index < total && isBatchAllowed(); index++) {
        // only loop if we are started (allowed to run)
        // use poll to remove the head so it does not consume memory even after we have processed it
        Exchange exchange = (Exchange) exchanges.poll();
        // add current index and total as properties
        exchange.setProperty(Exchange.BATCH_INDEX, index);
        exchange.setProperty(Exchange.BATCH_SIZE, total);
        exchange.setProperty(Exchange.BATCH_COMPLETE, index == total - 1);

        // update pending number of exchanges
        pendingExchanges = total - index - 1;

        // process the current exchange
        processExchange(exchange);
    }

    // remove the file from the in progress list in case the batch was limited by max messages per poll
    while (exchanges.size() > 0) {
        Exchange exchange = (Exchange) exchanges.poll();
        GenericFile<T> file = (GenericFile<T>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
        String key = file.getAbsoluteFilePath();
        endpoint.getInProgressRepository().remove(key);
    }

    return total;
}

From source file:com.android.volley.RequestQueue.java

/**
 * Called from {@link Request#finish(String)}, indicating that processing of the given request
 * has finished./*w  w w .j  a v  a2s.com*/
 *
 * <p>Releases waiting requests for <code>request.getCacheKey()</code> if
 *      <code>request.shouldCache()</code>.</p>
 */
<T> void finish(Request<T> request) {
    // Remove from the set of requests currently being processed.
    synchronized (mCurrentRequests) {
        mCurrentRequests.remove(request);
    }
    synchronized (mFinishedListeners) {
        for (RequestFinishedListener<T> listener : mFinishedListeners) {
            listener.onRequestFinished(request);
        }
    }

    if (request.shouldCache()) {
        synchronized (mWaitingRequests) {
            String cacheKey = request.getCacheKey();
            Queue<Request<?>> waitingRequests = mWaitingRequests.remove(cacheKey);
            if (waitingRequests != null) {
                if (VolleyLog.DEBUG) {
                    VolleyLog.v("Releasing %d waiting requests for cacheKey=%s.", waitingRequests.size(),
                            cacheKey);
                }
                // Process all queued up requests. They won't be considered as in flight, but
                // that's not a problem as the cache has been primed by 'request'.
                mCacheQueue.addAll(waitingRequests);
            }
        }
    }
}