Redis pub/sub初体验

  |   0 评论   |   0 浏览

背景

重新记录一下。

redis-cli初体验

订阅

> subscribe note
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "note"
3) (integer) 1

1) "message"
2) "note"
3) "hello, world. "

发布

> publish note "111"
(integer) 1

jedis初体验

依赖

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

pub

import redis.clients.jedis.Jedis;

public class JedisRedisPubTest {
    public static void main(String[] args) {
        String host = "127.0.0.1";
        int port = 6379;

        Jedis pubJedis = new Jedis(host, port);

        try {
            long id = pubJedis.publish("note", "hello, world. ");
            System.out.println(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pubJedis.close();
        }
    }
}

sub

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

@Slf4j
public class JedisRedisSubTest {
    public static void main(String[] args) {
        String host = "127.0.0.1";
        int port = 6379;

        Jedis subJedis = new Jedis(host, port);

        JedisPubSub jedisPubSub = new JedisPubSub() {

            public void onUnsubscribe(String channel, int num) {
                log.info("unsubscribe channel = {}, num = {}", channel, num);
            }

            public void onSubscribe(String channel, int num) {
                log.info("subscribe channel = {}, num = {}", channel, num);
            }

            public void onMessage(String channel, String msg) {
                log.info("channel {} : {}", channel, msg);
            }

            public void onPUnsubscribe(String pattern, int num) {
                log.info("pattern unsubscribe {} : {}", pattern, num);
            }

            public void onPSubscribe(String pattern, int num) {
                log.info("pattern subscribe {} : {}", pattern, num);
            }

            public void onPMessage(String pattern, String channel, String msg) {
                log.info("channel {} {} : {}", pattern, channel, msg);
            }
        };

        subJedis.subscribe(jedisPubSub, "note");
    }
}

Lettuce

订阅

RedisClient client = RedisClient.create("redis://127.0.0.1");
    StatefulRedisPubSubConnection<String, String> connection = client.connectPubSub();
    connection.addListener(new MyRedisListener());

    RedisPubSubAsyncCommands<String, String> async = connection.async();
    async.subscribe(channel);

创建Listener

import io.lettuce.core.pubsub.RedisPubSubListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MyRedisListener implements RedisPubSubListener<String, String> {

    @Override
    public void message(String channel, String message) {
        log.debug("channel {}: {}", channel, message);
    }

    @Override
    public void message(String pattern, String channel, String message) {
        log.debug("pattern {} channel {}: {}", pattern, channel, message);
    }

    @Override
    public void subscribed(String channel, long count) {
        log.debug("subscribed channel {} count {}", channel, count);
    }

    @Override
    public void psubscribed(String pattern, long count) {
        log.debug("pattern subscribed {} count {}", pattern, count);
    }

    @Override
    public void unsubscribed(String channel, long count) {
        log.debug("unsubscribed {} count {}", channel, count);
    }

    @Override
    public void punsubscribed(String pattern, long count) {
        log.debug("pattern unsubscribed {} count {}", pattern, count);
    }
}

参考

  1. Redis Lettuce Sub Pub功能使用简要介绍
  2. Java端实现RedisPUB/SUB(订阅发布)