Example usage for java.util Queue poll

List of usage examples for java.util Queue poll

Introduction

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

Prototype

E poll();

Source Link

Document

Retrieves and removes the head of this queue, or returns null if this queue is empty.

Usage

From source file:org.apache.hadoop.corona.SchedulerForType.java

/**
 * Try match one request to one node//from   ww w  . j av  a  2 s .c  om
 *
 * @param nodeWait Time to wait for node locality
 * @param rackWait Time to wait for rack locality
 * @return The pair contains a session id and a granted resource
 *         or null when no task can be scheduled
 */
private ScheduledPair scheduleOneTask(long nodeWait, long rackWait) {
    if (!nodeManager.existRunnableNodes(type)) {
        return null;
    }

    Queue<PoolGroupSchedulable> poolGroupQueue = poolGroupManager.getScheduleQueue();
    while (!poolGroupQueue.isEmpty()) {
        PoolGroupSchedulable poolGroup = poolGroupQueue.poll();
        if (poolGroup.reachedMaximum()) {
            continue;
        }
        // Get the appropriate pool from the pool group to schedule, then
        // schedule the best session
        Queue<PoolSchedulable> poolQueue = poolGroup.getScheduleQueue();
        while (!poolQueue.isEmpty()) {
            PoolSchedulable pool = poolQueue.poll();
            if (pool.reachedMaximum()) {
                continue;
            }
            Queue<SessionSchedulable> sessionQueue = pool.getScheduleQueue();
            while (!sessionQueue.isEmpty()) {
                SessionSchedulable schedulable = sessionQueue.poll();
                Session session = schedulable.getSession();
                long now = ClusterManager.clock.getTime();
                MatchedPair pair = doMatch(schedulable, now, nodeWait, rackWait);
                synchronized (session) {
                    if (session.isDeleted()) {
                        continue;
                    }
                    if (pair != null) {
                        ResourceGrant grant = commitMatchedResource(session, pair);
                        if (grant != null) {
                            poolGroup.incGranted(1);
                            pool.incGranted(1);
                            schedulable.incGranted(1);
                            // Put back to the queue only if we scheduled successfully
                            poolGroupQueue.add(poolGroup);
                            poolQueue.add(pool);
                            sessionQueue.add(schedulable);
                            return new ScheduledPair(session.getSessionId().toString(), grant);
                        }
                    }
                }
            }
        }
    }
    return null;
}

From source file:com.navercorp.pinpoint.web.calltree.span.CallTreeIteratorTest.java

private void assertCallTree(CallTree callTree, List<StackEvent> stackEvents, boolean check) {

    Queue<StackEvent> stackEventQueue = new LinkedList<>(stackEvents);
    if (check && CollectionUtils.isNotEmpty(stackEventQueue)) {
        int index = 0;
        for (CallTreeNode callTreeNode : callTree) {
            Align align = callTreeNode.getAlign();
            final StackEvent stackEvent = stackEventQueue.poll();
            assertEquals("depth " + index, stackEvent.getDepth(), align.getDepth());
            assertEquals("gap " + index, stackEvent.getGap(), align.getGap());
            assertEquals("exec " + index, stackEvent.getExec(), align.getExecutionMilliseconds());
            index++;/*from w ww.  j a v  a2s .c  o  m*/
        }
    }
    // TODO Check CI log
    log(callTree);
}

From source file:org.rhq.core.plugin.testutil.AbstractAgentPluginTest.java

/**
 * Test that loads a resource configuration and then immediately updates the resource
 * with the exact same loaded settings.//from  w  w w. jav a  2s  .c  o  m
 *
 * Notes:
 * 1) load/update is not executed on the root resource provided.
 * 2) if a resource is ignored then all of subresource of that resources are ignored
 *
 * @param rootResource root resource
 * @param ignoredResources resources to be ignored
 * @return number of errors
 * @throws InterruptedException
 * @throws PluginContainerException
 */
protected int loadUpdateConfigChildResources(Resource rootResource, List<String> ignoredResources)
        throws InterruptedException, PluginContainerException {

    ignoredResources = (ignoredResources == null) ? new ArrayList<String>() : ignoredResources;

    ConfigurationManager configManager = this.pluginContainer.getConfigurationManager();
    Thread.sleep(10 * 1000L);

    Queue<Resource> unparsedResources = new LinkedList<Resource>();
    addCommitedChildrenToCollection(unparsedResources, rootResource, ignoredResources);

    int errorCount = 0;

    while (!unparsedResources.isEmpty()) {
        Resource resourceUnderTest = unparsedResources.poll();

        addCommitedChildrenToCollection(unparsedResources, resourceUnderTest, ignoredResources);

        if (resourceUnderTest.getResourceType().getResourceConfigurationDefinition() != null) {
            Configuration configUnderTest = configManager.loadResourceConfiguration(resourceUnderTest.getId());

            ConfigurationUpdateRequest updateRequest = new ConfigurationUpdateRequest(1, configUnderTest,
                    resourceUnderTest.getId());
            ConfigurationUpdateResponse updateResponse = configManager
                    .executeUpdateResourceConfigurationImmediately(updateRequest);

            if (updateResponse == null) {
                errorCount++;
                log.error("------------------------------");
                log.error(resourceUnderTest);
                log.error("Update Response is NULL!!!!");
                log.error("------------------------------\n");
            }
            if (updateResponse.getErrorMessage() != null) {
                errorCount++;
                log.error("------------------------------");
                log.error(resourceUnderTest);
                log.error(updateResponse.getErrorMessage());
                log.error("------------------------------\n");
            }
        }
    }

    return errorCount;
}

From source file:ch.mlutz.plugins.t4e.tapestry.TapestryModule.java

/**
 * Scans app specification for module metadata (mainly page and component
 * class packages); then scans this module (html and java files) and updates
 * the central tapestryIndex with file relations; finally scans
 * hivemodule.xml and collects and stores services in this module
 * @param monitor/* ww  w . j  a  v a2s .co m*/
 *
 * @throws TapestryException
 */
public void scanAndUpdateIndex(IProgressMonitor monitor) throws TapestryException {
    try {
        // validateAppSpecificationFile(appSpecification.getAppSpecificationFile());
        if (!getAppSpecification().update()) {
            return;
        }

        Queue<IResource> resourceQueue = new LinkedList<IResource>();
        resourceQueue.add(webappFolder);

        IResource current = resourceQueue.poll();
        while (current != null) {
            if (current.getType() == IResource.FILE) {
                if ("html".equals(FilenameUtils.getExtension(current.getName()))) {
                    try {
                        findRelatedUnit((IFile) current);
                    } catch (CoreException e) {
                        log.warn("Couldn't find related unit for " + current.getName(), e);
                    }
                }
            } else if (current.getType() == IResource.FOLDER) {
                // add all members in this folder to queue
                IResource[] members;
                try {
                    members = ((IFolder) current).members();
                    for (IResource member : members) {
                        resourceQueue.add(member);
                    }
                } catch (CoreException e) {
                    log.warn("Couldn't get members of folder " + current.getName(), e);
                }
            }
            current = resourceQueue.poll();
        }
    } finally {
        if (monitor != null) {
            monitor.worked(getScanAndUpdateWork());
        }
    }
}

From source file:org.apache.camel.component.jpa.JpaConsumer.java

public int processBatch(Queue<Object> exchanges) throws Exception {
    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   ww w.  jav a2s . c o m
    }

    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());
        EntityManager entityManager = holder.manager;
        Exchange exchange = holder.exchange;
        Object result = holder.result;

        // 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;

        if (lockEntity(result, entityManager)) {
            // process the current exchange
            if (LOG.isDebugEnabled()) {
                LOG.debug("Processing exchange: " + exchange);
            }
            try {
                getProcessor().process(exchange);
            } catch (Exception e) {
                throw new PersistenceException(e);
            }
            getDeleteHandler().deleteObject(entityManager, result);
        }
    }

    return total;
}

From source file:org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhaseBase.java

/**
 * {@inheritDoc}/*from  w w w  .  ja  v  a2 s  .com*/
 */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    Queue<ViewLifecyclePhase> toPrint = new LinkedList<ViewLifecyclePhase>();
    toPrint.offer(this);
    while (!toPrint.isEmpty()) {
        ViewLifecyclePhase tp = toPrint.poll();

        if (tp.getElement() == null) {
            sb.append("\n      ");
            sb.append(tp.getClass().getSimpleName());
            sb.append(" (recycled)");
            continue;
        }

        String indent;
        if (tp == this) {
            sb.append("\nProcessed? ");
            sb.append(processed);
            indent = "\n";
        } else {
            indent = "\n    ";
        }
        sb.append(indent);

        sb.append(tp.getClass().getSimpleName());
        sb.append(" ");
        sb.append(System.identityHashCode(tp));
        sb.append(" ");
        sb.append(tp.getViewPath());
        sb.append(" ");
        sb.append(tp.getElement().getClass().getSimpleName());
        sb.append(" ");
        sb.append(tp.getElement().getId());
        sb.append(" ");
        sb.append(pendingSuccessors);

        if (tp == this) {
            sb.append("\nPredecessor Phases:");
        }

        ViewLifecyclePhase tpredecessor = tp.getPredecessor();
        if (tpredecessor != null) {
            toPrint.add(tpredecessor);
        }
    }
    return sb.toString();
}

From source file:org.apache.gobblin.example.wikipedia.WikipediaExtractor.java

private long createLowWatermarkForBootstrap(WorkUnitState state) throws IOException {
    String bootstrapPeriodString = state.getProp(BOOTSTRAP_PERIOD, DEFAULT_BOOTSTRAP_PERIOD);
    Period period = Period.parse(bootstrapPeriodString);
    DateTime startTime = DateTime.now().minus(period);

    try {/*w ww .  ja va  2  s  .c o m*/
        Queue<JsonElement> firstRevision = retrievePageRevisions(ImmutableMap.<String, String>builder()
                .putAll(this.baseQuery).put("rvprop", "ids").put("titles", this.requestedTitle)
                .put("rvlimit", "1").put("rvstart", WIKIPEDIA_TIMESTAMP_FORMAT.print(startTime))
                .put("rvdir", "newer").build());
        if (firstRevision.isEmpty()) {
            throw new IOException("Could not retrieve oldest revision, returned empty revisions list.");
        }
        return parseRevision(firstRevision.poll());
    } catch (URISyntaxException use) {
        throw new IOException(use);
    }

}

From source file:eu.stratosphere.runtime.io.channels.InputChannel.java

@Override
public void destroy() {
    final Queue<Buffer> buffersToRecycle = new ArrayDeque<Buffer>();

    synchronized (this.queuedEnvelopes) {
        this.destroyCalled = true;

        while (!this.queuedEnvelopes.isEmpty()) {
            final Envelope envelope = this.queuedEnvelopes.poll();
            if (envelope.getBuffer() != null) {
                buffersToRecycle.add(envelope.getBuffer());
            }//from  w  w w.  ja  va2s  .  c  o m
        }
    }

    while (!buffersToRecycle.isEmpty()) {
        buffersToRecycle.poll().recycleBuffer();
    }
}

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  ww w  .j  a v  a  2 s .  c  o  m*/
    }

    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.onebusaway.uk.network_rail.gtfs_realtime.graph.PositionBerthToStanoxGraphMain.java

private void interpolateBerthLocations() {
    int index = 0;
    for (RawBerthNode rootNode : _berthNodesToLocations.keySet()) {
        if (index % 100 == 0) {
            _log.info("node=" + index + "/" + _berthNodesToLocations.keySet().size());
        }//from   ww w. j  a v  a  2s. c  o  m
        index++;
        Location fromLocation = _berthNodesToLocations.get(rootNode);
        Queue<OrderedRawBerthNode> queue = new PriorityQueue<OrderedRawBerthNode>();
        queue.add(new OrderedRawBerthNode(rootNode, null, 0.0));

        Map<RawBerthNode, RawBerthNode> parents = new HashMap<RawBerthNode, RawBerthNode>();
        Set<RawBerthNode> visited = new HashSet<RawBerthNode>();

        while (!queue.isEmpty()) {
            OrderedRawBerthNode currentNode = queue.poll();
            RawBerthNode node = currentNode.getNode();
            if (!visited.add(node)) {
                continue;
            }

            parents.put(node, currentNode.getParent());
            Location toLocation = _berthNodesToLocations.get(node);
            if (currentNode.getParent() != null && toLocation != null) {

                List<RawBerthNode> path = new ArrayList<RawBerthNode>();
                RawBerthNode last = node;
                while (last != null) {
                    path.add(last);
                    last = parents.get(last);
                }

                if (path.size() <= 2) {
                    break;
                }
                Collections.reverse(path);
                BerthPath berthPath = new BerthPath(path, currentNode.getDistance());
                double d = fromLocation.getDistance(toLocation);
                if (d > 30000) {
                    continue;
                }
                RailwayPath railwayPath = _railwayShapeService.getPath(fromLocation.getPoint(),
                        toLocation.getPoint());
                if (railwayPath != null) {
                    snapBerthsToRailwayPath(berthPath, railwayPath);
                }
                break;
            } else {
                for (Map.Entry<RawBerthNode, List<Integer>> entry : node.getOutgoing().entrySet()) {
                    RawBerthNode outgoing = entry.getKey();
                    int avgDuration = RawNode.average(entry.getValue());
                    queue.add(new OrderedRawBerthNode(outgoing, node, currentNode.getDistance() + avgDuration));
                }
            }
        }
    }
}