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.hyperic.hq.product.JDBCMeasurementPlugin.java

protected void returnCachedConnection(String url, String user, String pass, Connection conn) {
    String cacheKey = calculateKey(url, user, pass);
    Queue<Connection> pool = connectionPools.get(cacheKey);
    if (pool != null) {
        pool.add(conn);// w ww  .j a v  a2 s .c om
        log.debug("[retCC] Connection for '" + cacheKey + "' returned (pool.size=" + pool.size() + ")");
    } else {
        DBUtil.closeJDBCObjects(log, conn, null, null);
        log.debug("[retCC] Pool for '" + cacheKey + "' not found, closing connection");
    }
}

From source file:org.glassfish.jersey.examples.sseitemstore.jersey.JerseyItemStoreResourceTest.java

/**
 * Test the {@link EventSource} reconnect feature.
 *
 * @throws Exception in case of a test failure.
 *///from   ww  w .ja v a  2s .c o  m
@Test
public void testEventSourceReconnect() throws Exception {
    final WebTarget itemsTarget = target("items");
    final CountDownLatch latch = new CountDownLatch(MAX_ITEMS * MAX_LISTENERS * 2); // countdown only on new item events
    final List<Queue<String>> receivedQueues = new ArrayList<>(MAX_LISTENERS);
    final EventSource[] sources = new EventSource[MAX_LISTENERS];

    for (int i = 0; i < MAX_LISTENERS; i++) {
        final int id = i;
        final EventSource es = EventSource.target(itemsTarget.path("events")).named("SOURCE " + id).build();
        sources[id] = es;

        final Queue<String> received = new ConcurrentLinkedQueue<>();
        receivedQueues.add(received);

        es.register(inboundEvent -> {
            try {
                if (inboundEvent.getName() == null) {
                    latch.countDown();
                    final String data = inboundEvent.readData();
                    LOGGER.info("[-i-] SOURCE " + id + ": Received event id=" + inboundEvent.getId() + " data="
                            + data);
                    received.add(data);
                }
            } catch (Exception ex) {
                LOGGER.log(Level.SEVERE, "[-x-] SOURCE " + id + ": Error getting event data.", ex);
                received.add("[data processing error]");
            }
        });
    }

    final String[] postedItems = new String[MAX_ITEMS * 2];
    try {
        open(sources);

        for (int i = 0; i < MAX_ITEMS; i++) {
            final String item = String.format("round-1-%02d", i);
            postItem(itemsTarget, item);
            postedItems[i] = item;
            sendCommand(itemsTarget, "disconnect");
            Thread.sleep(100);
        }

        final int reconnectDelay = 1;
        sendCommand(itemsTarget, "reconnect " + reconnectDelay);
        sendCommand(itemsTarget, "disconnect");

        Thread.sleep(reconnectDelay * 1000);

        for (int i = 0; i < MAX_ITEMS; i++) {
            final String item = String.format("round-2-%02d", i);
            postedItems[i + MAX_ITEMS] = item;
            postItem(itemsTarget, item);
        }

        sendCommand(itemsTarget, "reconnect now");

        assertTrue("Waiting to receive all events has timed out.",
                latch.await(
                        (1 + MAX_LISTENERS * (MAX_ITEMS + 1) * reconnectDelay) * getAsyncTimeoutMultiplier(),
                        TimeUnit.SECONDS));

        // need to force disconnect on server in order for EventSource.close(...) to succeed with HttpUrlConnection
        sendCommand(itemsTarget, "disconnect");
    } finally {
        close(sources);
    }

    final String storedItems = itemsTarget.request().get(String.class);
    for (String item : postedItems) {
        assertThat("Posted item '" + item + "' stored on server", storedItems, containsString(item));
    }

    int sourceId = 0;
    for (Queue<String> queue : receivedQueues) {
        assertThat("Received events in source " + sourceId, queue, describedAs("Collection containing %0",
                hasItems(postedItems), Arrays.asList(postedItems).toString()));
        assertThat("Size of received queue for source " + sourceId, queue.size(), equalTo(postedItems.length));
        sourceId++;
    }
}

From source file:org.glassfish.jersey.examples.sseitemstore.ItemStoreResourceTest.java

/**
 * Test the {@link EventSource} reconnect feature.
 *
 * @throws Exception in case of a test failure.
 *///  w w  w .j  a v a2  s  .  c o  m
@Test
public void testEventSourceReconnect() throws Exception {
    final WebTarget itemsTarget = target("items");
    final CountDownLatch latch = new CountDownLatch(MAX_ITEMS * MAX_LISTENERS * 2); // countdown only on new item events
    final List<Queue<String>> receivedQueues = new ArrayList<Queue<String>>(MAX_LISTENERS);
    final EventSource[] sources = new EventSource[MAX_LISTENERS];

    for (int i = 0; i < MAX_LISTENERS; i++) {
        final int id = i;
        final EventSource es = EventSource.target(itemsTarget.path("events")).named("SOURCE " + id).build();
        sources[id] = es;

        final Queue<String> received = new ConcurrentLinkedQueue<String>();
        receivedQueues.add(received);

        es.register(new EventListener() {
            @Override
            public void onEvent(InboundEvent inboundEvent) {
                try {
                    if (inboundEvent.getName() == null) {
                        latch.countDown();
                        final String data = inboundEvent.readData();
                        LOGGER.info("[-i-] SOURCE " + id + ": Received event id=" + inboundEvent.getId()
                                + " data=" + data);
                        received.add(data);
                    }
                } catch (Exception ex) {
                    LOGGER.log(Level.SEVERE, "[-x-] SOURCE " + id + ": Error getting event data.", ex);
                    received.add("[data processing error]");
                }
            }
        });
    }

    final String[] postedItems = new String[MAX_ITEMS * 2];
    try {
        open(sources);

        for (int i = 0; i < MAX_ITEMS; i++) {
            final String item = String.format("round-1-%02d", i);
            postItem(itemsTarget, item);
            postedItems[i] = item;
            sendCommand(itemsTarget, "disconnect");
            Thread.sleep(100);
        }

        final int reconnectDelay = 1;
        sendCommand(itemsTarget, "reconnect " + reconnectDelay);
        sendCommand(itemsTarget, "disconnect");

        Thread.sleep(reconnectDelay * 1000);

        for (int i = 0; i < MAX_ITEMS; i++) {
            final String item = String.format("round-2-%02d", i);
            postedItems[i + MAX_ITEMS] = item;
            postItem(itemsTarget, item);
        }

        sendCommand(itemsTarget, "reconnect now");

        assertTrue("Waiting to receive all events has timed out.",
                latch.await(
                        (1 + MAX_LISTENERS * (MAX_ITEMS + 1) * reconnectDelay) * getAsyncTimeoutMultiplier(),
                        TimeUnit.SECONDS));

        // need to force disconnect on server in order for EventSource.close(...) to succeed with HttpUrlConnection
        sendCommand(itemsTarget, "disconnect");
    } finally {
        close(sources);
    }

    final String storedItems = itemsTarget.request().get(String.class);
    for (String item : postedItems) {
        assertThat("Posted item '" + item + "' stored on server", storedItems, containsString(item));
    }

    int sourceId = 0;
    for (Queue<String> queue : receivedQueues) {
        assertThat("Received events in source " + sourceId, queue, describedAs("Collection containing %0",
                hasItems(postedItems), Arrays.asList(postedItems).toString()));
        assertThat("Size of received queue for source " + sourceId, queue.size(), equalTo(postedItems.length));
        sourceId++;
    }
}

From source file:it.geosolutions.geobatch.actions.ds2ds.Ds2dsAction.java

/**
* Imports data from the source DataStore to the output one
* transforming the data as configured./*from   w  w w .j a va  2 s .  c  o  m*/
 */
@Override
public Queue<EventObject> execute(Queue<EventObject> events) throws ActionException {

    // return object
    final Queue<EventObject> outputEvents = new LinkedList<EventObject>();

    while (events.size() > 0) {
        final EventObject ev;
        try {
            if ((ev = events.remove()) != null) {
                listenerForwarder.started();

                updateTask("Working on incoming event: " + ev.getSource());

                Queue<FileSystemEvent> acceptableFiles = acceptableFiles(unpackCompressedFiles(ev));
                if (ev instanceof FileSystemEvent
                        && ((FileSystemEvent) ev).getEventType().equals(FileSystemEventType.POLLING_EVENT)) {
                    String fileType = getFileType((FileSystemEvent) ev);
                    EventObject output = null;
                    if ("feature".equalsIgnoreCase(fileType)) {
                        configuration.getOutputFeature().setTypeName(
                                FilenameUtils.getBaseName(((FileSystemEvent) ev).getSource().getName()));
                        output = buildOutputEvent();
                        updateImportProgress(1, 1, "Completed");
                    } else {
                        output = importFile((FileSystemEvent) ev);
                    }

                    outputEvents.add(output);
                } else {
                    if (acceptableFiles.size() == 0) {
                        failAction("No file to process");
                    } else {
                        List<ActionException> exceptions = new ArrayList<ActionException>();
                        for (FileSystemEvent fileEvent : acceptableFiles) {
                            try {
                                String fileType = getFileType(fileEvent);
                                EventObject output = null;
                                if ("feature".equalsIgnoreCase(fileType)) {
                                    configuration.getOutputFeature().setTypeName(FilenameUtils
                                            .getBaseName(((FileSystemEvent) ev).getSource().getName()));
                                    output = buildOutputEvent();
                                    updateImportProgress(1, 1, "Completed");
                                } else {
                                    output = importFile(fileEvent);
                                }
                                if (output != null) {
                                    // add the event to the return
                                    outputEvents.add(output);
                                } else {
                                    if (LOGGER.isWarnEnabled()) {
                                        LOGGER.warn("No output produced");
                                    }
                                }
                            } catch (ActionException e) {
                                exceptions.add(e);
                            }

                        }
                        if (acceptableFiles.size() == exceptions.size()) {
                            throw new ActionException(this, exceptions.get(0).getMessage());
                        } else if (exceptions.size() > 0) {
                            if (LOGGER.isWarnEnabled()) {
                                for (ActionException ex : exceptions) {
                                    LOGGER.warn("Error in action: " + ex.getMessage());
                                }
                            }
                        }
                    }
                }

            } else {
                if (LOGGER.isErrorEnabled()) {
                    LOGGER.error("Encountered a NULL event: SKIPPING...");
                }
                continue;
            }
        } catch (ActionException ioe) {
            failAction("Unable to produce the output, " + ioe.getLocalizedMessage(), ioe);
        } catch (Exception ioe) {
            failAction("Unable to produce the output: " + ioe.getLocalizedMessage(), ioe);
        }
    }
    return outputEvents;
}

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

private void launchTasks(SchedulerDriver schedulerDriver, java.util.Queue<TaskInProgress> tips,
        ResourceOffer offer) {/*from  w w  w. j  a  v  a 2 s .c om*/
    TaskID taskId = TaskID.newBuilder().setValue("Task_" + launchedTasks++).build();

    List<Long> ports = claimPorts(offer.ports, 2);

    double taskCpus = 1 * tips.size() + groomCpus;
    double taskMem = slotMemory * tips.size() + groomMem;
    double taskDisk = 10 + groomDisk;

    String uri = conf.get("hama.mesos.executor.uri");
    if (uri == null) {
        throw new RuntimeException("Expecting configuration property 'mapred.mesos.executor.uri'");
    }

    String directory = conf.get("hama.mesos.executor.directory");
    if (directory == null || directory.equals("")) {
        log.info("URI: " + uri + ", name: " + new File(uri).getName());

        directory = new File(uri).getName().split("\\.")[0] + "*";
    }
    log.debug("Directory: " + directory);
    String command = conf.get("hama.mesos.executor.command");
    if (command == null || command.equals("")) {
        command = "env ; bash -x ./bin/hama org.apache.hama.bsp.MesosExecutor";
    }

    // Set up the environment for running the TaskTracker.
    Protos.Environment.Builder envBuilder = Protos.Environment.newBuilder();

    // Set java specific environment, appropriately.
    Map<String, String> env = System.getenv();
    if (env.containsKey("JAVA_HOME")) {
        envBuilder.addVariables(
                Protos.Environment.Variable.newBuilder().setName("JAVA_HOME").setValue(env.get("JAVA_HOME")));
    }

    envBuilder.addVariables(Protos.Environment.Variable.newBuilder().setName("HAMA_LOG_DIR").setValue("logs"));

    log.debug("JAVA_HOME: " + env.get("JAVA_HOME"));
    if (env.containsKey("JAVA_LIBRARY_PATH")) {
        envBuilder.addVariables(Protos.Environment.Variable.newBuilder().setName("JAVA_LIBRARY_PATH")
                .setValue(env.get("JAVA_LIBRARY_PATH")));
    }
    log.debug("JAVA_LIBRARY_PATH: " + env.get("JAVA_LIBRARY_PATH"));

    CommandInfo commandInfo = CommandInfo.newBuilder().setEnvironment(envBuilder)
            .setValue(String.format("cd %s && %s", directory, command))
            .addUris(CommandInfo.URI.newBuilder().setValue(uri)).build();

    log.debug("Offer: cpus:  " + offer.cpus + " mem: " + offer.mem + "disk: " + offer.disk);
    log.debug("Cpu: " + taskCpus + " Mem: " + taskMem + " Disk: " + taskDisk + " port: " + ports.get(0));
    TaskInfo info = TaskInfo.newBuilder().setName(taskId.getValue()).setTaskId(taskId)
            .setSlaveId(offer.offer.getSlaveId())
            .addResources(Resource.newBuilder().setName("cpus").setType(Value.Type.SCALAR)
                    .setRole(offer.cpuRole).setScalar(Value.Scalar.newBuilder().setValue(taskCpus)))
            .addResources(Resource.newBuilder().setName("mem").setType(Value.Type.SCALAR).setRole(offer.memRole)
                    .setScalar(Value.Scalar.newBuilder().setValue(taskMem)))
            .addResources(Resource.newBuilder().setName("disk").setType(Value.Type.SCALAR)
                    .setRole(offer.diskRole).setScalar(Value.Scalar.newBuilder().setValue(taskDisk)))
            .addResources(Resource.newBuilder().setName("ports").setType(Value.Type.RANGES)
                    .setRole(offer.portRole)
                    .setRanges(Value.Ranges.newBuilder()
                            .addRange(Value.Range.newBuilder().setBegin(ports.get(0)).setEnd(ports.get(0)))
                            .addRange(Value.Range.newBuilder().setBegin(ports.get(1)).setEnd(ports.get(1)))))
            .setExecutor(ExecutorInfo.newBuilder()
                    .setExecutorId(ExecutorID.newBuilder().setValue("executor_" + taskId.getValue()))
                    .setName("Hama Groom Server").setSource(taskId.getValue()).setCommand(commandInfo))
            .setData(ByteString.copyFrom(
                    getConfigurationOverride(ports.get(0), ports.get(1), tips.size(), (long) taskMem)))
            .build();

    log.debug("Accepting offer " + offer.offer.getId() + " cpus: " + taskCpus + " mem: " + taskMem);
    for (TaskInProgress tip : tips) {
        taskDelegator.addTask(tip, taskId, offer.offer.getHostname(), ports.get(0).intValue());
    }
    schedulerDriver.launchTasks(offer.offer.getId(), Arrays.asList(info));
}

From source file:org.mule.util.queue.AbstractTransactionQueueManagerTestCase.java

protected void purgeQueue(Queue queue) throws InterruptedException {
    while (queue.size() > 0) {
        queue.poll(1000);//from  w  w  w  .jav  a  2  s.co m
    }
    assertEquals("Queue must be fully consumed after successful test run. Queue size:", 0, queue.size());
}

From source file:gobblin.ingestion.google.webmaster.GoogleWebmasterDataFetcherImpl.java

/**
 * Get all pages in an async mode.// www  . j  a  v a  2s  .c o  m
 */
private Collection<String> getPages(String startDate, String endDate, List<Dimension> dimensions,
        ApiDimensionFilter countryFilter, Queue<Pair<String, FilterOperator>> toProcess) throws IOException {
    String country = GoogleWebmasterFilter.countryFilterToString(countryFilter);

    ConcurrentLinkedDeque<String> allPages = new ConcurrentLinkedDeque<>();
    int r = 0;
    while (r <= RETRY) {
        ++r;
        log.info(String.format("Get pages at round %d with size %d.", r, toProcess.size()));
        ConcurrentLinkedDeque<Pair<String, FilterOperator>> nextRound = new ConcurrentLinkedDeque<>();
        ExecutorService es = Executors.newFixedThreadPool(10, ExecutorsUtils
                .newDaemonThreadFactory(Optional.of(log), Optional.of(this.getClass().getSimpleName())));

        while (!toProcess.isEmpty()) {
            submitJob(toProcess.poll(), countryFilter, startDate, endDate, dimensions, es, allPages, nextRound);
        }
        //wait for jobs to finish and start next round if necessary.
        try {
            es.shutdown();
            boolean terminated = es.awaitTermination(5, TimeUnit.MINUTES);
            if (!terminated) {
                es.shutdownNow();
                log.warn(String.format(
                        "Timed out while getting all pages for country-%s at round %d. Next round now has size %d.",
                        country, r, nextRound.size()));
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        if (nextRound.isEmpty()) {
            break;
        }
        toProcess = nextRound;
    }
    if (r == RETRY) {
        throw new RuntimeException(String.format(
                "Getting all pages reaches the maximum number of retires %d. Date range: %s ~ %s. Country: %s.",
                RETRY, startDate, endDate, country));
    }
    return allPages;
}

From source file:password.pwm.util.report.ReportService.java

private void updateCacheFromLdap() throws ChaiUnavailableException, ChaiOperationException,
        PwmOperationalException, PwmUnrecoverableException {
    LOGGER.debug(PwmConstants.REPORTING_SESSION_LABEL,
            "beginning process to updating user cache records from ldap");
    if (status != STATUS.OPEN) {
        return;/* w w  w .j ava  2s.  c om*/
    }
    cancelFlag = false;
    reportStatus = new ReportStatusInfo(settings.getSettingsHash());
    reportStatus.setInProgress(true);
    reportStatus.setStartDate(new Date());
    try {
        final Queue<UserIdentity> allUsers = new LinkedList<>(getListOfUsers());
        reportStatus.setTotal(allUsers.size());
        while (status == STATUS.OPEN && !allUsers.isEmpty() && !cancelFlag) {
            final long startUpdateTime = System.currentTimeMillis();
            final UserIdentity userIdentity = allUsers.poll();
            try {
                if (updateCache(userIdentity)) {
                    reportStatus.setUpdated(reportStatus.getUpdated() + 1);
                }
            } catch (Exception e) {
                String errorMsg = "error while updating report cache for " + userIdentity.toString()
                        + ", cause: ";
                errorMsg += e instanceof PwmException ? ((PwmException) e).getErrorInformation().toDebugStr()
                        : e.getMessage();
                final ErrorInformation errorInformation;
                errorInformation = new ErrorInformation(PwmError.ERROR_REPORTING_ERROR, errorMsg);
                LOGGER.error(PwmConstants.REPORTING_SESSION_LABEL, errorInformation.toDebugStr());
                reportStatus.setLastError(errorInformation);
                reportStatus.setErrors(reportStatus.getErrors() + 1);
            }
            reportStatus.setCount(reportStatus.getCount() + 1);
            reportStatus.getEventRateMeter().markEvents(1);
            final long totalUpdateTime = System.currentTimeMillis() - startUpdateTime;
            if (settings.isAutoCalcRest()) {
                avgTracker.addSample(totalUpdateTime);
                Helper.pause(avgTracker.avgAsLong());
            } else {
                Helper.pause(settings.getRestTime().getTotalMilliseconds());
            }
        }
        if (cancelFlag) {
            reportStatus.setLastError(
                    new ErrorInformation(PwmError.ERROR_SERVICE_NOT_AVAILABLE, "report cancelled by operator"));
        }
    } finally {
        reportStatus.setFinishDate(new Date());
        reportStatus.setInProgress(false);
    }
    LOGGER.debug(PwmConstants.REPORTING_SESSION_LABEL,
            "update user cache process completed: " + JsonUtil.serialize(reportStatus));
}

From source file:org.hyperic.hq.product.JDBCMeasurementPlugin.java

protected void removeCachedConnection(String url, String user, String pass) {
    String cacheKey = calculateKey(url, user, pass);
    Queue<Connection> pool = connectionPools.get(cacheKey);
    if (pool != null) {
        Connection conn;//from   ww  w .  j  ava2s  . c om
        while ((conn = pool.poll()) != null) {
            DBUtil.closeJDBCObjects(log, conn, null, null);
            log.debug("[remCC] Connection for '" + cacheKey + "' closed (pool.size=" + pool.size() + ")");
        }
        connectionPools.remove(cacheKey);
    } else {
        log.debug("[remCC] Pool for '" + cacheKey + "' not found");
    }
}