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

The following examples show how to use org.redisson.api.RBlockingQueue#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: RedissonPriorityBlockingQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testDrainToCollectionLimited() throws Exception {
    RBlockingQueue<Integer> queue1 = getQueue();
    queue1.put(1);
    queue1.put(2);
    queue1.put(3);

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

    dst.clear();
    queue1.drainTo(dst, 2);
    assertThat(dst).containsExactly(3);
}
 
Example 2
Source File: RedissonBlockingQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPollLastAndOfferFirstTo() throws InterruptedException {
    final RBlockingQueue<Integer> queue1 = redisson.getBlockingQueue("{queue}1");
    Executors.newSingleThreadScheduledExecutor().schedule(() -> {
        try {
            queue1.put(3);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }, 5, TimeUnit.SECONDS);

    RBlockingQueue<Integer> queue2 = redisson.getBlockingQueue("{queue}2");
    queue2.put(4);
    queue2.put(5);
    queue2.put(6);

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

    RBlockingQueue<Integer> queue2 = redisson.getBlockingQueue("{queue}2");
    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 4
Source File: RedissonBlockingQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testDrainToCollectionLimited() throws Exception {
    RBlockingQueue<Object> queue1 = getQueue();
    queue1.put(1);
    queue1.put(2L);
    queue1.put("e");

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

    dst.clear();
    queue1.drainTo(dst, 2);
    assertThat(dst).containsExactly("e");
}
 
Example 5
Source File: DefaultRedisQueue.java    From SeimiCrawler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean push(Request req) {
    try {
        RBlockingQueue<Request> rBlockingQueue = getQueue(req.getCrawlerName());
        rBlockingQueue.put(req);
        return true;
    } catch (Exception e) {
        logger.warn(e.getMessage());
    }
    return false;
}
 
Example 6
Source File: RedissonPriorityBlockingQueueTest.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);
    
    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i + 1000);
    }
    Integer result = f.get();
    assertThat(queue1.size()).isEqualTo(10);
    
    assertThat(result).isEqualTo(123);
    
    redisson.shutdown();
    runner.stop();
}
 
Example 7
Source File: RedissonPriorityBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeReattach() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.takeAsync();
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i + 10000);
    }

    Integer result = f.get();
    assertThat(result).isEqualTo(123);
    assertThat(queue1.size()).isEqualTo(10);
    runner.stop();
    
    redisson.shutdown();
}
 
Example 8
Source File: RedissonPriorityBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testDrainToCollection() throws Exception {
    RBlockingQueue<Integer> queue1 = getQueue();
    queue1.put(1);
    queue1.put(2);
    queue1.put(3);

    ArrayList<Object> dst = new ArrayList<Object>();
    queue1.drainTo(dst);
    assertThat(dst).containsExactly(1, 2, 3);
    Assert.assertEquals(0, queue1.size());
}
 
Example 9
Source File: RedissonBlockingQueueTest.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);
    
    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    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 10
Source File: RedissonBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeReattach() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);

    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.takeAsync();
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i + 10000);
    }
    assertThat(queue1.size()).isEqualTo(10);
    
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    runner.stop();
    
    redisson.shutdown();
}
 
Example 11
Source File: RedissonBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPoll() throws InterruptedException {
    RBlockingQueue<Integer> queue1 = getQueue();
    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 > 4900);
}
 
Example 12
Source File: RedissonBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAwait() throws InterruptedException {
    RBlockingQueue<Integer> queue1 = getQueue();
    queue1.put(1);

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

    ArrayList<Object> dst = new ArrayList<Object>();
    queue1.drainTo(dst);
    assertThat(dst).containsExactly(1, 2L, "e");
    Assert.assertEquals(0, queue1.size());
}
 
Example 14
Source File: RedissonBlockingQueueTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testTakeReattachSentinel() throws IOException, InterruptedException, TimeoutException, ExecutionException {
    RedisRunner.RedisProcess master = new RedisRunner()
            .nosave()
            .randomDir()
            .run();
    RedisRunner.RedisProcess slave1 = new RedisRunner()
            .port(6380)
            .nosave()
            .randomDir()
            .slaveof("127.0.0.1", 6379)
            .run();
    RedisRunner.RedisProcess slave2 = new RedisRunner()
            .port(6381)
            .nosave()
            .randomDir()
            .slaveof("127.0.0.1", 6379)
            .run();
    RedisRunner.RedisProcess sentinel1 = new RedisRunner()
            .nosave()
            .randomDir()
            .port(26379)
            .sentinel()
            .sentinelMonitor("myMaster", "127.0.0.1", 6379, 2)
            .run();
    RedisRunner.RedisProcess sentinel2 = new RedisRunner()
            .nosave()
            .randomDir()
            .port(26380)
            .sentinel()
            .sentinelMonitor("myMaster", "127.0.0.1", 6379, 2)
            .run();
    RedisRunner.RedisProcess sentinel3 = new RedisRunner()
            .nosave()
            .randomDir()
            .port(26381)
            .sentinel()
            .sentinelMonitor("myMaster", "127.0.0.1", 6379, 2)
            .run();

    Thread.sleep(1000);

    Config config = new Config();
    config.useSentinelServers()
        .setLoadBalancer(new RandomLoadBalancer())
        .addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
    RedissonClient redisson = Redisson.create(config);

    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.takeAsync();
    f.await(1, TimeUnit.SECONDS);

    master.stop();

    Thread.sleep(TimeUnit.SECONDS.toMillis(60));

    queue1.put(123);

    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i + 10000);
    }
    assertThat(queue1.size()).isEqualTo(10);

    Integer result = f.get(80, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);

    redisson.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();

}