Search in sources :

Example 61 with RedissonClient

use of org.redisson.api.RedissonClient in project redisson by redisson.

the class RedissonTopicTest method testListenerRemove.

@Test
public void testListenerRemove() throws InterruptedException {
    RedissonClient redisson1 = BaseTest.createInstance();
    RTopic<Message> topic1 = redisson1.getTopic("topic");
    int id = topic1.addListener((channel, msg) -> {
        Assert.fail();
    });
    RedissonClient redisson2 = BaseTest.createInstance();
    RTopic<Message> topic2 = redisson2.getTopic("topic");
    topic1.removeListener(id);
    topic2.publish(new Message("123"));
    Thread.sleep(1000);
    redisson1.shutdown();
    redisson2.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Test(org.junit.Test)

Example 62 with RedissonClient

use of org.redisson.api.RedissonClient in project redisson by redisson.

the class RedissonTopicTest method test.

@Test
public void test() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(2);
    RedissonClient redisson1 = BaseTest.createInstance();
    RTopic<Message> topic1 = redisson1.getTopic("topic");
    topic1.addListener((channel, msg) -> {
        Assert.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });
    RedissonClient redisson2 = BaseTest.createInstance();
    RTopic<Message> topic2 = redisson2.getTopic("topic");
    topic2.addListener((channel, msg) -> {
        Assert.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });
    topic2.publish(new Message("123"));
    messageRecieved.await();
    redisson1.shutdown();
    redisson2.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 63 with RedissonClient

use of org.redisson.api.RedissonClient in project redisson by redisson.

the class RedissonRedLockTest method testTryLockLeasetime.

@Test
public void testTryLockLeasetime() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    RedissonClient client1 = createClient(redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(redis2.getRedisServerAddressAndPort());
    RLock lock1 = client1.getLock("lock1");
    RLock lock2 = client1.getLock("lock2");
    RLock lock3 = client2.getLock("lock3");
    RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3);
    ExecutorService executor = Executors.newFixedThreadPool(10);
    AtomicInteger counter = new AtomicInteger();
    for (int i = 0; i < 10; i++) {
        executor.submit(() -> {
            for (int j = 0; j < 5; j++) {
                try {
                    if (lock.tryLock(4, 2, TimeUnit.SECONDS)) {
                        int nextValue = counter.get() + 1;
                        Thread.sleep(1000);
                        counter.set(nextValue);
                        lock.unlock();
                    } else {
                        j--;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    executor.shutdown();
    assertThat(executor.awaitTermination(2, TimeUnit.MINUTES)).isTrue();
    assertThat(counter.get()).isEqualTo(50);
    client1.shutdown();
    client2.shutdown();
    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) RLock(org.redisson.api.RLock) Test(org.junit.Test)

Example 64 with RedissonClient

use of org.redisson.api.RedissonClient in project redisson by redisson.

the class RedissonRedLockTest method createClient.

private RedissonClient createClient(NioEventLoopGroup group, String host) {
    Config config1 = new Config();
    config1.useSingleServer().setAddress(host);
    config1.setEventLoopGroup(group);
    RedissonClient client1 = Redisson.create(config1);
    client1.getKeys().flushdb();
    return client1;
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Config(org.redisson.config.Config)

Example 65 with RedissonClient

use of org.redisson.api.RedissonClient in project redisson by redisson.

the class RedissonRedLockTest method test.

//    @Test
public void test() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    RedisProcess redis3 = redisTestMultilockInstance();
    NioEventLoopGroup group = new NioEventLoopGroup();
    RedissonClient client1 = createClient(group, redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(group, redis2.getRedisServerAddressAndPort());
    RedissonClient client3 = createClient(group, redis3.getRedisServerAddressAndPort());
    final RLock lock1 = client1.getLock("lock1");
    final RLock lock2 = client2.getLock("lock2");
    final RLock lock3 = client3.getLock("lock3");
    RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
    lock.lock();
    final AtomicBoolean executed = new AtomicBoolean();
    Thread t = new Thread() {

        @Override
        public void run() {
            RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
            assertThat(lock.tryLock()).isFalse();
            assertThat(lock.tryLock()).isFalse();
            executed.set(true);
        }
    };
    t.start();
    t.join();
    await().atMost(5, TimeUnit.SECONDS).until(() -> assertThat(executed.get()).isTrue());
    lock.unlock();
    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
    assertThat(redis3.stop()).isEqualTo(0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) RLock(org.redisson.api.RLock) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

RedissonClient (org.redisson.api.RedissonClient)95 Test (org.junit.Test)87 Config (org.redisson.config.Config)44 RedisProcess (org.redisson.RedisRunner.RedisProcess)23 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 CountDownLatch (java.util.concurrent.CountDownLatch)15 ExecutorService (java.util.concurrent.ExecutorService)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 RLock (org.redisson.api.RLock)10 IOException (java.io.IOException)9 ExecutionException (java.util.concurrent.ExecutionException)9 NotSerializableException (java.io.NotSerializableException)8 RemoteServiceAckTimeoutException (org.redisson.remote.RemoteServiceAckTimeoutException)8 RemoteServiceTimeoutException (org.redisson.remote.RemoteServiceTimeoutException)8 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 RemoteInvocationOptions (org.redisson.api.RemoteInvocationOptions)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 BasePatternStatusListener (org.redisson.api.listener.BasePatternStatusListener)3 SerializationCodec (org.redisson.codec.SerializationCodec)3