redis stream 实现生产者消费者模式

   
    @Test
    public void producer() throws InterruptedException {
        while (true){
            String msgKey = "it";
            HashMap<String, String> map = new HashMap<>();
            map.put("name", UUID.randomUUID().toString());
            Random random = new Random();
            map.put("age", random.nextInt() + "");
            jedis.xadd(msgKey, StreamEntryID.NEW_ENTRY, map);
            System.out.println(map.entrySet());
            Thread.sleep(1000);
        }
    }




    @Test
    public void consumer1() throws InterruptedException {
        while (true){
//        jedis.xgroupCreate(msgKey, groupName, null, false);
            String msgKey = "it";
            String groupName = "gp1";
            String consumerName = "c1";
            int count = 0;
            //ack:确保消息至少被消费一次
            Map<String,StreamEntryID> t = new HashMap();
            t.put(msgKey, null);//null 则为 > 重头读起,也可以为$接受新消息,还可以是上一次未读完的消息id
            Map.Entry index = null;
            for(Map.Entry c:t.entrySet()){
                index=c;
            }

            List<Map.Entry<String, List<StreamEntry>>> msg = jedis.xreadGroup(groupName, consumerName, count, 0, true, index);
            if (msg == null){
                System.out.println("consumer1 waiting msg");
                Thread.sleep(1000);
            }else {
                msg.forEach(m -> {
                    List<StreamEntry> msgList = m.getValue();
                    msgList.forEach(en -> {
                        System.out.println("consumer1: " + en.getFields());
                    });
                });
            }
        }

    }


    @Test
    public void earlistModel() throws InterruptedException {
        jedis = new Jedis("127.0.0.1", 6379);

        String msgKey = "item_test";
        String groupName = "gtest4";
        String consumerName = "c1";
        try{
            jedis.xgroupCreate(msgKey, groupName,  new StreamEntryID("0-0"), true);
        }catch (Exception e){
            System.out.println(e);
        }finally {
            //触发一次消息记录,使其之后每次都是从0开始消费
            HashMap<String, StreamEntryID> map = new HashMap<>();
            map.put(msgKey, StreamEntryID.UNRECEIVED_ENTRY);
            Map.Entry<String, StreamEntryID> idEntry = map.entrySet().iterator().next();
            jedis.xreadGroup(groupName, consumerName, 0, 0, false, idEntry);
        }
        HashMap<String, StreamEntryID> map = new HashMap<>();
//        map.put(msgKey, StreamEntryID.UNRECEIVED_ENTRY);
        map.put(msgKey, new StreamEntryID("0-0"));
        Map.Entry<String, StreamEntryID> idEntry = map.entrySet().iterator().next();
        //ack变成true消费记录会无法保存问题
        List<Map.Entry<String, List<StreamEntry>>> msg = jedis.xreadGroup(groupName, consumerName, 0, 0, false, idEntry);
        if (msg == null || msg.get(0).getValue().size() == 0){
            System.out.println("consumer2 waiting msg");
            Thread.sleep(1000);
        }else {
            System.out.println("-----------------");
            print(msg);

        }

    }


    /**
     * latest:只读取最新消息
     * group 创建topic用 $ 忽略历史消息只读取最新的消息
     * read >
     * xreadgroup
     */
    @Test
    public void latestModel() throws InterruptedException {
        String msgKey = "news";
        String groupName = "g8090";
        String consumerName = "c1";
        try{
            jedis.xgroupCreate(msgKey, groupName, StreamEntryID.LAST_ENTRY, true);
        }catch (Exception e){
            System.out.println(e);
        }
        HashMap<String, StreamEntryID> map = new HashMap<>();
        map.put(msgKey, null);
        Map.Entry<String, StreamEntryID> idEntry = map.entrySet().iterator().next();
        while (true){
            List<Map.Entry<String, List<StreamEntry>>> msg = jedis.xreadGroup(groupName, consumerName, 0, 0, true, idEntry);
            if (msg == null){
                System.out.println("consumer2 waiting msg");
                Thread.sleep(1000);
            }else {
                System.out.println("-----------------");
                print(msg);
                System.out.println(msg.size());
                System.out.println("22222222222222222222");

            }
        }
    }

    /**
     * breakpoint:断点 读取未读取过的消息
     * 这种情况必须producer时候就创建group 0-0
     * read > :第一次会读取未消费过的数据
     */
    @Test
    public void breakpointModel() throws InterruptedException {
        String msgKey = "news";
        String groupName = "g88888";
        String consumerName = "c1";
        try{
            jedis.xgroupCreate(msgKey, groupName, null, true);
        }catch (Exception e){
            System.out.println(e);
        }
        HashMap<String, StreamEntryID> map = new HashMap<>();
        map.put(msgKey, StreamEntryID.UNRECEIVED_ENTRY);
        Map.Entry<String, StreamEntryID> idEntry = map.entrySet().iterator().next();
        while (true){
            List<Map.Entry<String, List<StreamEntry>>> msg = jedis.xreadGroup(groupName, consumerName, 0, 0, true, idEntry);
            if (msg == null){
                System.out.println("consumer2 waiting msg");
                Thread.sleep(1000);
            }else {
                System.out.println("-----------------");
                print(msg);
                System.out.println(msg.size());
                System.out.println("22222222222222222222");

            }
        }
    }
    /**
     * breakpoint:断点 读取未读取过的消息
     * 这种情况必须producer时候就创建group 0-0
     * read > :第一次会读取未消费过的数据
     */
    @Test
    public void breakpointModel() throws InterruptedException {
        String msgKey = "news";
        String groupName = "g88888";
        String consumerName = "c1";
        try{
            jedis.xgroupCreate(msgKey, groupName, null, true);
        }catch (Exception e){
            System.out.println(e);
        }
        HashMap<String, StreamEntryID> map = new HashMap<>();
        map.put(msgKey, StreamEntryID.UNRECEIVED_ENTRY);
        Map.Entry<String, StreamEntryID> idEntry = map.entrySet().iterator().next();
        while (true){
            List<Map.Entry<String, List<StreamEntry>>> msg = jedis.xreadGroup(groupName, consumerName, 0, 0, true, idEntry);
            if (msg == null){
                System.out.println("consumer2 waiting msg");
                Thread.sleep(1000);
            }else {
                System.out.println("-----------------");
                print(msg);
                System.out.println(msg.size());
                System.out.println("22222222222222222222");

            }
        }
    }

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值