Java Code Examples for org.redisson.api.RBoundedBlockingQueue#add()

The following examples show how to use org.redisson.api.RBoundedBlockingQueue#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddAll() {
    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("bounded-queue");
    queue.trySetCapacity(11);
    queue.add(1);
    queue.add(2);
    queue.add(3);
    queue.add(4);
    queue.add(5);

    Assert.assertTrue(queue.addAll(Arrays.asList(7, 8, 9)));
    assertThat(queue.remainingCapacity()).isEqualTo(3);

    Assert.assertTrue(queue.addAll(Arrays.asList(9, 1, 9)));
    assertThat(queue.remainingCapacity()).isEqualTo(0);

    assertThat(queue).containsExactly(1, 2, 3, 4, 5, 7, 8, 9, 9, 1, 9);
}
 
Example 2
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveAll() {
    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("bounded-queue");
    queue.trySetCapacity(5);
    queue.add(1);
    queue.add(2);
    queue.add(3);
    queue.add(4);
    queue.add(5);

    Assert.assertFalse(queue.removeAll(Collections.emptyList()));
    assertThat(queue.remainingCapacity()).isEqualTo(0);
    
    Assert.assertTrue(queue.removeAll(Arrays.asList(3, 2, 10, 6)));
    assertThat(queue.remainingCapacity()).isEqualTo(2);
    assertThat(queue).containsExactly(1, 4, 5);

    Assert.assertTrue(queue.removeAll(Arrays.asList(4)));
    assertThat(queue.remainingCapacity()).isEqualTo(3);
    assertThat(queue).containsExactly(1, 5);

    Assert.assertTrue(queue.removeAll(Arrays.asList(1, 5, 1, 5)));
    assertThat(queue.remainingCapacity()).isEqualTo(5);
    Assert.assertTrue(queue.isEmpty());
}
 
Example 3
Source File: BoundedBlockingQueueExamples.java    From redisson-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RBoundedBlockingQueue<String> queue = redisson.getBoundedBlockingQueue("myQueue");
    queue.add("1");
    queue.add("2");
    queue.add("3");
    queue.add("4");
    queue.add("5");
    
    queue.trySetCapacity(5);
    
    Thread t = new Thread(() -> {
        try {
            String element = queue.take();
            
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    
    t.start();
    
    queue.put("6");
    
    redisson.shutdown();
}
 
Example 4
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testOfferTimeout() throws InterruptedException {
    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("bounded-queue");
    queue.trySetCapacity(5);
    queue.add(1);
    queue.add(2);
    queue.add(3);
    queue.add(4);
    queue.add(5);

    long start = System.currentTimeMillis();
    assertThat(queue.offer(6, 2, TimeUnit.SECONDS)).isFalse();
    assertThat(System.currentTimeMillis() - start).isGreaterThan(1900);
    
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    final AtomicBoolean executed = new AtomicBoolean();
    executor.schedule(new Runnable() {

        @Override
        public void run() {
            RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("bounded-queue");
            assertThat(queue1.remove()).isEqualTo(1);
            executed.set(true);
        }
        
    }, 1, TimeUnit.SECONDS);

    start = System.currentTimeMillis();
    assertThat(queue.offer(6, 3, TimeUnit.SECONDS)).isTrue();
    assertThat(System.currentTimeMillis() - start).isBetween(1000L, 2000L);
    
    await().atMost(2, TimeUnit.SECONDS).untilTrue(executed);
    
    assertThat(queue).containsExactly(2, 3, 4, 5, 6);
    
    executor.shutdown();
    assertThat(executor.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
}
 
Example 5
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFullQueueError() {
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("bounded-queue:testAddFullQueueError");
    assertThat(queue1.trySetCapacity(1)).isTrue();
    assertThat(queue1.add(1)).isTrue();
    try {
        queue1.add(2);
    } catch (RedisException e) {
        assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
    }
}
 
Example 6
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddRemoveFullQueueError() {
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("bounded-queue:testAddRemoveFullQueueError");
    assertThat(queue1.trySetCapacity(1)).isTrue();
    assertThat(queue1.add(12)).isTrue();
    assertThat(queue1.remove()).isEqualTo(12);
    assertThat(queue1.add(1)).isTrue();
    try {
        queue1.add(2);
    } catch (RedisException e) {
        assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
    }
}
 
Example 7
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleCharAsKeyName() {
    String value = "Long Test Message;Long Test Message;Long Test Message;"
            + "Long Test Message;Long Test Message;Long Test Message;Long "
            + "Test Message;Long Test Message;Long Test Message;Long Test "
            + "Message;Long Test Message;Long Test Message;Long Test Messa"
            + "ge;Long Test Message;Long Test Message;Long Test Message;Lo"
            + "ng Test Message;Long Test Message;Long Test Message;Long Te"
            + "st Message;Long Test Message;Long Test Message;Long Test Me"
            + "ssage;Long Test Message;Long Test Message;Long Test Message"
            + ";Long Test Message;Long Test Message;Long Test Message;Long"
            + " Test Message;Long Test Message;Long Test Message;Long Test"
            + " Message;Long Test Message;Long Test Message;Long Test Mess"
            + "age;";
    try {
        for (int i = 0; i < 10; i++) {
            System.out.println("Iteration: " + i);
            RBoundedBlockingQueue<String> q = redisson.getBoundedBlockingQueue(String.valueOf(i));
            q.trySetCapacity(10);
            q.add(value);
            System.out.println("Message added to [" + i + "]");
            q.expire(1, TimeUnit.MINUTES);
            System.out.println("Expiry set to [" + i + "]");
            String poll = q.poll(1, TimeUnit.SECONDS);
            System.out.println("Message polled from [" + i + "]" + poll);
            Assert.assertEquals(value, poll);
        }
    } catch (Exception e) {
        Assert.fail(e.getLocalizedMessage());
    }
}
 
Example 8
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpire() throws InterruptedException {
    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("queue1");
    queue.trySetCapacity(10);
    queue.add(1);
    queue.add(2);

    queue.expire(100, TimeUnit.MILLISECONDS);

    Thread.sleep(500);

    assertThat(queue).isEmpty();
    assertThat(queue.size()).isZero();
}
 
Example 9
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpireAt() throws InterruptedException {
    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("queue1");
    queue.trySetCapacity(10);
    queue.add(1);
    queue.add(2);

    queue.expireAt(System.currentTimeMillis() + 100);

    Thread.sleep(5000);

    assertThat(queue).isEmpty();
    assertThat(queue.size()).isZero();
}
 
Example 10
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testClearExpire() throws InterruptedException {
    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("queue1");
    queue.trySetCapacity(10);
    queue.add(1);
    queue.add(2);

    queue.expireAt(System.currentTimeMillis() + 100);

    queue.clearExpire();

    Thread.sleep(500);

    assertThat(queue).containsExactly(1, 2);
}
 
Example 11
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test(expected = RedisException.class)
public void testInitCapacityError() {
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("bounded-queue:testInitCapacityError");
    queue1.add(1);
}