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

The following examples show how to use org.redisson.api.RBoundedBlockingQueue#put() . 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 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 2
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 3
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testDrainToCollectionLimited() throws Exception {
    RBoundedBlockingQueue<Object> queue1 = redisson.getBoundedBlockingQueue("queue1");
    assertThat(queue1.trySetCapacity(10)).isTrue();
    queue1.put(1);
    queue1.put(2L);
    queue1.put("e");

    ArrayList<Object> dst = new ArrayList<Object>();
    queue1.drainTo(dst, 2);
    assertThat(queue1.remainingCapacity()).isEqualTo(9);
    assertThat(dst).containsExactly(1, 2L);
    Assert.assertEquals(1, queue1.size());

    dst.clear();
    queue1.drainTo(dst, 2);
    assertThat(queue1.remainingCapacity()).isEqualTo(10);
    assertThat(dst).containsExactly("e");
}
 
Example 4
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 5
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPollAsyncReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("queue:pollany");
    RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    assertThat(queue1.trySetCapacity(15)).isTrue();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i);
    }
    assertThat(queue1.size()).isEqualTo(10);
    
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    
    redisson.shutdown();
    runner.stop();
}
 
Example 6
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    redisson.getKeys().flushall();
    
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("testTakeReattach");
    assertThat(queue1.trySetCapacity(15)).isTrue();
    RFuture<Integer> f = queue1.takeAsync();
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    assertThat(queue1.trySetCapacity(15)).isTrue();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i);
    }
    assertThat(queue1.size()).isEqualTo(10);
    
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    runner.stop();
    
    redisson.shutdown();
}
 
Example 7
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPoll() throws InterruptedException {
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("queue1");
    assertThat(queue1.trySetCapacity(10)).isTrue();
    queue1.put(1);
    Assert.assertEquals((Integer)1, queue1.poll(2, TimeUnit.SECONDS));

    long s = System.currentTimeMillis();
    Assert.assertNull(queue1.poll(5, TimeUnit.SECONDS));
    Assert.assertTrue(System.currentTimeMillis() - s > 5000);
}
 
Example 8
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAwait() throws InterruptedException {
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("queue1");
    assertThat(queue1.trySetCapacity(10)).isTrue();
    queue1.put(1);

    Assert.assertEquals((Integer)1, queue1.poll(10, TimeUnit.SECONDS));
}
 
Example 9
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testDrainToCollection() throws Exception {
    RBoundedBlockingQueue<Object> queue1 = redisson.getBoundedBlockingQueue("queue1");
    assertThat(queue1.trySetCapacity(10)).isTrue();
    queue1.put(1);
    queue1.put(2L);
    queue1.put("e");

    ArrayList<Object> dst = new ArrayList<Object>();
    queue1.drainTo(dst);
    assertThat(queue1.remainingCapacity()).isEqualTo(10);
    assertThat(dst).containsExactly(1, 2L, "e");
    Assert.assertEquals(0, queue1.size());
}