Search in sources :

Example 16 with IgniteQueue

use of org.apache.ignite.IgniteQueue in project ignite by apache.

the class GridCachePartitionedQueueEntryMoveSelfTest method testQueue.

/**
     * @throws Exception If failed.
     */
public void testQueue() throws Exception {
    final String queueName = "qq";
    System.out.println(U.filler(20, '\n'));
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    IgniteInternalFuture<?> fut1 = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws IgniteInterruptedCheckedException {
            Ignite ignite = grid(0);
            IgniteQueue<Integer> queue = ignite.queue(queueName, QUEUE_CAP, config(true));
            for (int i = 0; i < QUEUE_CAP * 2; i++) {
                if (i == QUEUE_CAP) {
                    latch1.countDown();
                    U.await(latch2);
                }
                try {
                    info(">>> Putting value: " + i);
                    queue.put(i);
                    info(">>> Value is in queue: " + i);
                } catch (Error | RuntimeException e) {
                    error("Failed to put value: " + i, e);
                    throw e;
                }
            }
            return null;
        }
    });
    latch1.await();
    startAdditionalNodes(BACKUP_CNT + 2, queueName);
    System.out.println(U.filler(20, '\n'));
    latch2.countDown();
    IgniteInternalFuture<?> fut2 = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws IgniteCheckedException {
            Ignite ignite = grid(GRID_CNT);
            IgniteQueue<Integer> queue = ignite.queue(queueName, QUEUE_CAP, config(true));
            int cnt = 0;
            do {
                try {
                    Integer i = queue.poll();
                    if (i != null) {
                        info(">>> Polled value: " + cnt);
                        cnt++;
                    } else {
                        info(">>> Waiting for value...");
                        U.sleep(2000);
                    }
                } catch (Error | RuntimeException e) {
                    error("Failed to poll value.", e);
                    throw e;
                }
            } while (cnt < QUEUE_CAP * 2);
            return null;
        }
    });
    fut1.get();
    fut2.get();
}
Also used : IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteQueue(org.apache.ignite.IgniteQueue) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

IgniteQueue (org.apache.ignite.IgniteQueue)16 Ignite (org.apache.ignite.Ignite)8 CollectionConfiguration (org.apache.ignite.configuration.CollectionConfiguration)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 IgniteException (org.apache.ignite.IgniteException)3 Collection (java.util.Collection)2 Callable (java.util.concurrent.Callable)2 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)2 IgniteSet (org.apache.ignite.IgniteSet)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)2 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 UUID (java.util.UUID)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 CacheException (javax.cache.CacheException)1