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

The following examples show how to use org.redisson.api.RBoundedBlockingQueue#trySetCapacity() . 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: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPollLastAndOfferFirstTo() throws InterruptedException {
    final RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("{queue}1");
    queue1.trySetCapacity(10);
    Executors.newSingleThreadScheduledExecutor().schedule(() -> {
        try {
            queue1.put(3);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }, 10, TimeUnit.SECONDS);

    RBoundedBlockingQueue<Integer> queue2 = redisson.getBoundedBlockingQueue("{queue}2");
    queue2.trySetCapacity(10);
    queue2.put(4);
    queue2.put(5);
    queue2.put(6);

    Integer value = queue1.pollLastAndOfferFirstTo(queue2.getName(), 10, TimeUnit.SECONDS);
    assertThat(value).isEqualTo(3);
    assertThat(queue2).containsExactly(3, 4, 5, 6);
}
 
Example 4
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testTakeLastAndOfferFirstTo() throws InterruptedException {
    final RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("{queue}1");
    queue1.trySetCapacity(10);
    Executors.newSingleThreadScheduledExecutor().schedule(() -> {
        try {
            queue1.put(3);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }, 3, TimeUnit.SECONDS);

    RBoundedBlockingQueue<Integer> queue2 = redisson.getBoundedBlockingQueue("{queue}2");
    queue2.trySetCapacity(10);
    queue2.put(4);
    queue2.put(5);
    queue2.put(6);

    long startTime = System.currentTimeMillis();
    Integer value = queue1.takeLastAndOfferFirstTo(queue2.getName());
    assertThat(System.currentTimeMillis() - startTime).isBetween(2900L, 3200L);
    assertThat(value).isEqualTo(3);
    assertThat(queue2).containsExactly(3, 4, 5, 6);
}
 
Example 5
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testDrainTo() {
    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("queue");
    queue.trySetCapacity(100);
    for (int i = 0 ; i < 100; i++) {
        assertThat(queue.offer(i)).isTrue();
    }
    Assert.assertEquals(100, queue.size());
    Set<Integer> batch = new HashSet<Integer>();
    assertThat(queue.remainingCapacity()).isEqualTo(0);
    int count = queue.drainTo(batch, 10);
    assertThat(queue.remainingCapacity()).isEqualTo(10);
    Assert.assertEquals(10, count);
    Assert.assertEquals(10, batch.size());
    Assert.assertEquals(90, queue.size());
    queue.drainTo(batch, 10);
    assertThat(queue.remainingCapacity()).isEqualTo(20);
    queue.drainTo(batch, 20);
    assertThat(queue.remainingCapacity()).isEqualTo(40);
    queue.drainTo(batch, 60);
    assertThat(queue.remainingCapacity()).isEqualTo(100);
    Assert.assertEquals(0, queue.size());
}
 
Example 6
Source File: DbJobServiceContext.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/** 任务队列 */
RBoundedBlockingQueue<String> getJobQueue(String jobType, String topic) {
    String key = StringHelper.join(KEY_QUEUE, topic, ":",
            NamingConstant.shortName(jobType));
    RBoundedBlockingQueue<String> queue = redisson
            .getBoundedBlockingQueue(key);
    queue.trySetCapacity(QUEUE_SIZE);
    return queue;
}
 
Example 7
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 8
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 9
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlockingQueue() {

    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("test_:blocking:queue:");
    queue.trySetCapacity(10);

    ExecutorService executor = Executors.newFixedThreadPool(10);

    final AtomicInteger counter = new AtomicInteger();
    int total = 100;
    for (int i = 0; i < total; i++) {
        // runnable won't be executed in any particular order, and hence, int value as well.
        executor.submit(() -> {
            redisson.getQueue("test_:blocking:queue:").add(counter.incrementAndGet());
        });
    }
    int count = 0;
    while (count < total) {
        try {
            // blocking
            int item = queue.take();
            assertThat(item > 0 && item <= total).isTrue();
        } catch (InterruptedException exception) {
            Assert.fail();
        }
        count++;
    }

    assertThat(counter.get()).isEqualTo(total);
    queue.delete();
}
 
Example 10
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 11
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 12
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 13
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 14
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test(expected = NoSuchElementException.class)
public void testRemoveEmpty() {
    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("blocking:queue");
    queue.trySetCapacity(10);
    queue.remove();
}