Redis实现队列

消息通知
使用Redis实现任务队列
使用列表, lpush 和 rpop 命令实现队列的概念

添加数据

public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            jedis.lpush("num", String.valueOf(i));
            Thread.currentThread().sleep(2000);
        }
    }

消费

 public static void main(String[] args) throws InterruptedException {
        while (true) {
            String rpop = jedis.rpop("num");
            System.out.println(rpop);
            Thread.currentThread().sleep(3000);
        }
    }


发布/订阅 模式(publish/subscribe)
此模式有两种角色,分别是发布者和订阅者,订阅者可以定阅一个或多个频道(channel)

发布者发布消息,返回接受到这条信息的订阅者数量
publish channel message

定阅频道
subscribe channel [channel ...]

取消定阅
unsubscribe channel [channel ...]

原文地址:https://www.cnblogs.com/yrjns/p/11722921.html