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.mule.util.queue.AbstractTransactionQueueManagerTestCase.java

@Test
public void testPutTake() throws Exception {
    TransactionalQueueManager mgr = createQueueManager();
    mgr.start();/*from  w  ww . j av  a2  s  .  co  m*/

    QueueSession s = mgr.getQueueSession();
    Queue q = s.getQueue("queue1");

    assertEquals("Queue size", 0, q.size());
    q.put("String1");
    assertEquals("Queue size", 1, q.size());
    Object o = q.take();
    assertNotNull(o);
    assertEquals("Queue content", "String1", o);
    assertEquals("Queue size", 0, q.size());

    purgeQueue(q);

    mgr.stop(AbstractResourceManager.SHUTDOWN_MODE_NORMAL);
}

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

@Test
public void testPeek() throws Exception {

    TransactionalQueueManager mgr = createQueueManager();

    try {/*  w w  w  . ja v  a  2s  . c  o  m*/
        mgr.start();

        QueueSession s = mgr.getQueueSession();
        Queue q = s.getQueue("queue1");

        assertEquals("Queue size", 0, q.size());
        Object o = q.peek();
        assertEquals("Queue size", 0, q.size());
        assertNull(o);
        q.put("String1");
        assertEquals("Queue size", 1, q.size());
        o = q.peek();
        assertEquals("Queue size", 1, q.size());
        assertEquals("Queue content", "String1", o);
        o = q.poll(1000);
        assertEquals("Queue size", 0, q.size());
        assertEquals("Queue content", "String1", o);

        purgeQueue(q);
    } finally {
        mgr.stop(AbstractResourceManager.SHUTDOWN_MODE_NORMAL);
    }
}

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

/**
 * Get all pages in an async mode./*from  w ww . ja  v a2  s.  c  o m*/
 */
private Collection<String> getPages(String startDate, String endDate, List<Dimension> dimensions,
        ApiDimensionFilter countryFilter, Queue<Pair<String, FilterOperator>> toProcess, int rowLimit)
        throws IOException {
    String country = GoogleWebmasterFilter.countryFilterToString(countryFilter);

    ConcurrentLinkedDeque<String> allPages = new ConcurrentLinkedDeque<>();
    int r = 0;
    while (r <= GET_PAGES_RETRIES) {
        ++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,
                    rowLimit);
        }
        //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 == GET_PAGES_RETRIES) {
        throw new RuntimeException(String.format(
                "Getting all pages reaches the maximum number of retires %d. Date range: %s ~ %s. Country: %s.",
                GET_PAGES_RETRIES, startDate, endDate, country));
    }
    return allPages;
}

From source file:it.geosolutions.geobatch.opensdi.ndvi.NDVIStatsAction.java

/**
 * Execute process//from  w w  w .j a  v a 2 s .c om
 */
public Queue<EventObject> execute(Queue<EventObject> events) throws ActionException {

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

    while (events.size() > 0) {
        final EventObject ev;
        try {
            if ((ev = events.remove()) != null) {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Working on incoming event: " + ev.getSource());
                }
                if (ev instanceof FileSystemEvent) {
                    FileSystemEvent fileEvent = (FileSystemEvent) ev;
                    File file = fileEvent.getSource();
                    processXMLFile(file);
                }

                // add the event to the return
                ret.add(ev);

            } else {
                if (LOGGER.isErrorEnabled()) {
                    LOGGER.error("Encountered a NULL event: SKIPPING...");
                }
                continue;
            }
        } catch (Exception ioe) {
            final String message = "Unable to produce the output: " + ioe.getLocalizedMessage();
            if (LOGGER.isErrorEnabled())
                LOGGER.error(message, ioe);

            throw new ActionException(this, message);
        }
    }
    return ret;
}

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

@Test
public void testOffer() throws Exception {

    final TransactionalQueueManager mgr = createQueueManager();
    mgr.setDefaultQueueConfiguration(/* w ww  .  j  a  va 2 s  .c  om*/
            new QueueConfiguration(1, new QueueStoreAdapter<Serializable>(new SimpleMemoryObjectStore())));
    try {
        mgr.start();

        QueueSession s = mgr.getQueueSession();
        Queue q = s.getQueue("queue1");

        assertEquals("Queue size", 0, q.size());
        assertTrue(q.offer("String1", 0L));
        assertEquals("Queue size", 1, q.size());
        assertFalse(q.offer("String2", 1000));
        assertEquals("Queue size", 1, q.size());

        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(500);
                    QueueSession s = mgr.getQueueSession();
                    Queue q = s.getQueue("queue1");
                    assertEquals("Queue content", "String1", q.take());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        assertTrue(q.offer("String2", 1000));
        assertEquals("Queue size", 1, q.size());

        purgeQueue(q);
    } finally {
        mgr.stop(AbstractResourceManager.SHUTDOWN_MODE_NORMAL);
    }
}

From source file:org.commonjava.maven.ext.io.rest.DefaultTranslator.java

/**
 * Translate the versions.//from   ww w  .j  ava2s. c om
 * <pre>{@code
 * [ {
 *     "groupId": "com.google.guava",
 *     "artifactId": "guava",
 *     "version": "13.0.1"
 * } }
 * }</pre>
 * This equates to a List of ProjectVersionRef.
 *
 * <pre>{@code
 * {
 *     "productNames": [],
 *     "productVersionIds": [],
 *     "repositoryGroup": "",
 *     "gavs": [
 *     {
 *         "groupId": "com.google.guava",
 *         "artifactId": "guava",
 *         "version": "13.0.1"
 *     } ]
 * }
 * }</pre>
 * There may be a lot of them, possibly causing timeouts or other issues.
 * This is mitigated by splitting them into smaller chunks when an error occurs and retrying.
 */
public Map<ProjectVersionRef, String> translateVersions(List<ProjectVersionRef> projects) {
    init(rgm);

    final Map<ProjectVersionRef, String> result = new HashMap<>();
    final Queue<Task> queue = new ArrayDeque<>();
    if (initialRestMaxSize != 0) {
        // Presplit
        final List<List<ProjectVersionRef>> partition = ListUtils.partition(projects, initialRestMaxSize);
        for (List<ProjectVersionRef> p : partition) {
            queue.add(new Task(rgm, p, endpointUrl + REPORTS_LOOKUP_GAVS));
        }
        logger.debug("For initial sizing of {} have split the queue into {} ", initialRestMaxSize,
                queue.size());
    } else {
        queue.add(new Task(rgm, projects, endpointUrl + REPORTS_LOOKUP_GAVS));
    }

    while (!queue.isEmpty()) {
        Task task = queue.remove();
        task.executeTranslate();
        if (task.isSuccess()) {
            result.putAll(task.getResult());
        } else {
            if (task.canSplit() && task.getStatus() == 504) {
                List<Task> tasks = task.split();

                logger.warn(
                        "Failed to translate versions for task @{} due to {}, splitting and retrying. Chunk size was: {} and new chunk size {} in {} segments.",
                        task.hashCode(), task.getStatus(), task.getChunkSize(), tasks.get(0).getChunkSize(),
                        tasks.size());
                queue.addAll(tasks);
            } else {
                if (task.getStatus() < 0) {
                    logger.debug("Caught exception calling server with message {}", task.getErrorMessage());
                } else {
                    logger.debug("Did not get status {} but received {}", SC_OK, task.getStatus());
                }

                if (task.getStatus() > 0) {
                    throw new RestException("Received response status " + task.getStatus() + " with message: "
                            + task.getErrorMessage());
                } else {
                    throw new RestException("Received response status " + task.getStatus() + " with message "
                            + task.getErrorMessage());
                }
            }
        }
    }
    return result;
}

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

@Test
public void testPoll() throws Exception {
    final TransactionalQueueManager mgr = createQueueManager();

    try {//from  w  w w .  j a va2  s.com
        mgr.start();

        QueueSession s = mgr.getQueueSession();
        Queue q = s.getQueue("queue1");

        assertEquals("Queue size", 0, q.size());
        Object o = q.poll(0);
        assertEquals("Queue size", 0, q.size());
        assertNull(o);
        o = q.poll(1000);
        assertEquals("Queue size", 0, q.size());
        assertNull(o);
        q.put("String1");
        assertEquals("Queue size", 1, q.size());
        o = q.poll(0);
        assertEquals("Queue size", 0, q.size());
        assertEquals("Queue content", "String1", o);

        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(500);
                    QueueSession s = mgr.getQueueSession();
                    Queue q = s.getQueue("queue1");
                    q.put("String1");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        o = q.poll(1000);
        assertEquals("Queue size", q.size(), 0);
        assertEquals("Queue content", "String1", o);

        purgeQueue(q);
    } finally {
        mgr.stop(AbstractResourceManager.SHUTDOWN_MODE_NORMAL);
    }
}

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

@Test
public void testTakePut() throws Exception {
    final TransactionalQueueManager mgr = createQueueManager();
    mgr.start();//from   ww  w .j a va  2 s  .  co  m

    final Latch latch = new Latch();

    Thread t = new Thread() {
        @Override
        public void run() {
            try {
                latch.countDown();
                Thread.sleep(200);
                QueueSession s = mgr.getQueueSession();
                Queue q = s.getQueue("queue1");
                assertEquals("Queue size", 0, q.size());
                q.put("String1");
            } catch (Exception e) {
                // ignore, let test fail
            }
        }
    };
    t.start();
    latch.await();
    long t0 = System.currentTimeMillis();
    QueueSession s = mgr.getQueueSession();
    Queue q = s.getQueue("queue1");
    assertEquals("Queue size", 0, q.size());
    Object o = q.take();
    long t1 = System.currentTimeMillis();
    t.join();
    assertNotNull(o);
    assertEquals("Queue content", "String1", o);
    assertEquals("Queue size", 0, q.size());
    assertTrue(t1 - t0 > 100);

    purgeQueue(q);

    mgr.stop(AbstractResourceManager.SHUTDOWN_MODE_NORMAL);
}

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

@Test
public void testTakePutRollbackPut() throws Exception {
    final TransactionalQueueManager mgr = createQueueManager();
    mgr.start();/*from   w w  w. ja va 2s.co  m*/

    final Latch latch = new Latch();

    Thread t = new Thread() {
        @Override
        public void run() {
            try {
                latch.countDown();
                Thread.sleep(200);
                QueueSession s = mgr.getQueueSession();
                Queue q = s.getQueue("queue1");
                assertEquals("Queue size", 0, q.size());
                s.begin();
                q.put("String1");
                s.rollback();
                s.begin();
                q.put("String2");
                s.commit();
            } catch (Exception e) {
                // ignore, let test fail
            }
        }
    };
    t.start();
    latch.await();
    long t0 = System.currentTimeMillis();
    QueueSession s = mgr.getQueueSession();
    Queue q = s.getQueue("queue1");
    assertEquals("Queue size", 0, q.size());
    Object o = q.take();
    long t1 = System.currentTimeMillis();
    t.join();
    assertNotNull(o);
    assertEquals("Queue content", "String2", o);
    assertEquals("Queue size", 0, q.size());
    assertTrue(t1 - t0 > 100);

    purgeQueue(q);

    mgr.stop(AbstractResourceManager.SHUTDOWN_MODE_NORMAL);
}

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

@Test
public void testPutTakeUntake() throws Exception {
    final TransactionalQueueManager mgr = createQueueManager();
    mgr.start();//from  w  w  w .  j  a v  a2 s.co  m

    final Latch latch = new Latch();

    Thread t = new Thread() {
        @Override
        public void run() {
            try {
                latch.countDown();
                Thread.sleep(200);
                QueueSession s = mgr.getQueueSession();
                Queue q = s.getQueue("queue1");
                assertEquals("Queue size", 0, q.size());
                q.put("String1");
                q.put("String2");
            } catch (Exception e) {
                // ignore, let test fail
            }
        }
    };
    t.start();
    latch.await();
    long t0 = System.currentTimeMillis();
    QueueSession s = mgr.getQueueSession();
    Queue q = s.getQueue("queue1");
    assertEquals("Queue size", 0, q.size());
    Serializable o = q.take();
    long t1 = System.currentTimeMillis();
    t.join();
    assertNotNull(o);
    assertEquals("Queue content", "String1", o);
    assertEquals("Queue size", 1, q.size());
    assertTrue(t1 - t0 > 100);

    // Same as put/take until now, but now we do an untake
    q.untake(o);
    // Ensure queue size is now 2
    assertEquals("Queue size", 2, q.size());
    // Take to ensure order is correct
    Object o2 = q.take();
    assertEquals("Queue content", "String1", o2);
    assertEquals("Queue size", 1, q.size());

    purgeQueue(q);

    mgr.stop(AbstractResourceManager.SHUTDOWN_MODE_NORMAL);

}