Search in sources :

Example 1 with Message

use of org.springframework.data.redis.connection.Message in project redisson by redisson.

the class RedissonSubscribeTest method testPatterTopic.

@Test
public void testPatterTopic() throws IOException, InterruptedException {
    RedisRunner.RedisProcess instance = new RedisRunner().nosave().randomPort().randomDir().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.K, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g, RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.$).run();
    Config config = new Config();
    config.useSingleServer().setAddress(instance.getRedisServerAddressAndPort()).setPingConnectionInterval(0);
    RedissonClient redisson = Redisson.create(config);
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(factory);
    AtomicInteger counterTest = new AtomicInteger();
    container.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            counterTest.incrementAndGet();
        }
    }, new PatternTopic("__keyspace@0__:mykey"));
    container.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            counterTest.incrementAndGet();
        }
    }, new PatternTopic("__keyevent@0__:del"));
    container.afterPropertiesSet();
    container.start();
    Assertions.assertThat(container.isRunning()).isTrue();
    RedisConnection c = factory.getConnection();
    c.set("mykey".getBytes(), "2".getBytes());
    c.del("mykey".getBytes());
    Awaitility.await().atMost(Duration.FIVE_SECONDS).until(() -> {
        return counterTest.get() == 3;
    });
    container.stop();
    redisson.shutdown();
}
Also used : Message(org.springframework.data.redis.connection.Message) PatternTopic(org.springframework.data.redis.listener.PatternTopic) Config(org.redisson.config.Config) MessageListener(org.springframework.data.redis.connection.MessageListener) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) RedissonClient(org.redisson.api.RedissonClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RedisRunner(org.redisson.RedisRunner) RedisConnection(org.springframework.data.redis.connection.RedisConnection) Test(org.junit.Test)

Example 2 with Message

use of org.springframework.data.redis.connection.Message in project redisson by redisson.

the class RedissonSubscribeTest method testUnSubscribe.

@Test
public void testUnSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND).until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    connection.getSubscription().unsubscribe();
}
Also used : Message(org.springframework.data.redis.connection.Message) MessageListener(org.springframework.data.redis.connection.MessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 3 with Message

use of org.springframework.data.redis.connection.Message in project redisson by redisson.

the class RedissonSubscribeTest method testMultipleSubscribers.

@Test
public void testMultipleSubscribers() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(factory);
    AtomicInteger counterTest = new AtomicInteger();
    AtomicInteger counterTest2 = new AtomicInteger();
    container.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            counterTest.incrementAndGet();
        }
    }, new ChannelTopic("test"));
    container.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            counterTest.incrementAndGet();
        }
    }, new ChannelTopic("test"));
    container.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            counterTest2.incrementAndGet();
        }
    }, new ChannelTopic("test2"));
    container.afterPropertiesSet();
    container.start();
    Assertions.assertThat(container.isRunning()).isTrue();
    RedisConnection c = factory.getConnection();
    c.publish("test".getBytes(), "sdfdsf".getBytes());
    Awaitility.await().atMost(Duration.FIVE_SECONDS).until(() -> {
        return counterTest.get() == 2;
    });
    Assertions.assertThat(counterTest2.get()).isZero();
    container.stop();
}
Also used : Message(org.springframework.data.redis.connection.Message) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(org.springframework.data.redis.connection.MessageListener) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) RedisConnection(org.springframework.data.redis.connection.RedisConnection) Test(org.junit.Test)

Example 4 with Message

use of org.springframework.data.redis.connection.Message in project redisson by redisson.

the class RedissonSubscribeTest method testSubscribe.

@Test
public void testSubscribe() {
    RedissonConnection connection = new RedissonConnection(redisson);
    AtomicReference<byte[]> msg = new AtomicReference<byte[]>();
    connection.subscribe(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            msg.set(message.getBody());
        }
    }, "test".getBytes());
    connection.publish("test".getBytes(), "msg".getBytes());
    Awaitility.await().atMost(Duration.ONE_SECOND).until(() -> Arrays.equals("msg".getBytes(), msg.get()));
    connection.getSubscription().unsubscribe();
    connection.publish("test".getBytes(), "msg".getBytes());
}
Also used : Message(org.springframework.data.redis.connection.Message) MessageListener(org.springframework.data.redis.connection.MessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 Message (org.springframework.data.redis.connection.Message)4 MessageListener (org.springframework.data.redis.connection.MessageListener)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 RedisConnection (org.springframework.data.redis.connection.RedisConnection)2 RedisMessageListenerContainer (org.springframework.data.redis.listener.RedisMessageListenerContainer)2 RedisRunner (org.redisson.RedisRunner)1 RedissonClient (org.redisson.api.RedissonClient)1 Config (org.redisson.config.Config)1 ChannelTopic (org.springframework.data.redis.listener.ChannelTopic)1 PatternTopic (org.springframework.data.redis.listener.PatternTopic)1