基于redis的消息提醒功能

背景

公司系统中,要求做一个类型消息推送的东西,有一些待处理的消息要在管理端提供声音消息提醒,以便让处理人员可以及时的处理。我最开始想到的是用websocket通信的方式,做好了发现会莫名其妙的断线,还需要加入心跳机制保证长时间没消息推送的连接不断。最后我索性就用了轮训的方式。

思路

使用redis中的队列,配合前端定时轮训的方式

直接上代码

package com.reaps.modules.sys.service;

public interface INoticeService {

    /**
     * push订单提醒
     * 
     * @return 是否成功推送
     */
    public boolean lPushRetailOrderNotice();

    /**
     * push提币提醒
     * 
     * @return 是否成功推送
     */
    public boolean lPushCoinOutNotice();

    /**
     * push实名提醒
     * 
     * @return 是否成功推送
     */
    public boolean lPushRealNameNotice();

    /**
     * 合约订单队列是否有提醒消息
     * @return
     */
    public boolean blpopRetailOrderNotice();

    /**
     * 提币申请队列是否有提醒消息
     * @return
     */
    public boolean blpopCoinOutNotice();

    
    /**
     * 实名认证队列是否有提醒消息
     * @return
     */
    public boolean blpopRealNameNotice();

}


package com.reaps.modules.sys.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.reaps.config.dao.redis.CacheService;
import com.reaps.modules.sys.service.INoticeService;

/**
 * 基于redis队列的提醒
 * 
 * @author zl
 *
 */
@Service("iNoticeService")
public class NoticeServiceImpl implements INoticeService {

    private static Logger LOG = LoggerFactory.getLogger(NoticeServiceImpl.class);

    private static final String ORDERLIST = "retailOrderList";
    private static final String COINOUTLIST = "coinOutList";
    private static final String REALNAMELIST = "realNameList";

    @Autowired
    private CacheService cacheService;

    @Override
    public boolean lPushRetailOrderNotice() {
        
        if (LOG.isInfoEnabled()) {
            LOG.info("推送了一条算力合约订单提醒消息");
        }
        return cacheService.lpush(ORDERLIST, "retailOrder");
    }

    @Override
    public boolean lPushCoinOutNotice() {

        if (LOG.isInfoEnabled()) {
            LOG.info("推送了一条提币申请提醒消息");
        }
        return cacheService.lpush(COINOUTLIST, "coinOut");
    }

    @Override
    public boolean lPushRealNameNotice() {
        if (LOG.isInfoEnabled()) {
            LOG.info("推送了一条实名认证申请提醒消息");
        }
        return cacheService.lpush(REALNAMELIST, "realName");
    }

    @Override
    public boolean blpopRetailOrderNotice() {
        return blopop(ORDERLIST);
    }

    @Override
    public boolean blpopCoinOutNotice() {
        return blopop(COINOUTLIST);
    }

    @Override
    public boolean blpopRealNameNotice() {
        return blopop(REALNAMELIST);
    }

    private boolean blopop(String type) {
        int i = 0;
        //不阻塞
        while (!CollectionUtils.isEmpty(cacheService.blpop(type, 1))) {
            i++;
        }
        if (LOG.isInfoEnabled() && i > 0) {
            LOG.info(String.format("【%s】-----【%d】条消息", type, i));
        }
        return i > 0;
    }

}

管理端controller层

package com.reaps.modules.sys.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.reaps.common.utils.Result;
import com.reaps.modules.sys.service.INoticeService;

@Controller
@RequestMapping(path="sys/notice")
public class SysNoticeController {
    
    @Autowired
    private INoticeService iNoticeService;

    @RequestMapping(path="getOrderNotice",method=RequestMethod.POST)
    @ResponseBody
    public Result getOrderNotice() {
        if(iNoticeService.blpopRetailOrderNotice()) {
            return Result.result(Result.SUCCESS_CODE, "有新的订单推送消息", "有新的订单推送消息");
        }
        return Result.result(Result.FAIL_CODE, "暂无新的订单推送消息", "暂无新的订单推送消息");
    }
    
    @RequestMapping(path="getCoinOutNotice",method=RequestMethod.POST)
    @ResponseBody
    public Result getCoinOutNotice() {
        if(iNoticeService.blpopCoinOutNotice()) {
            return Result.result(Result.SUCCESS_CODE, "有新的提币推送消息", "有新的提币推送消息");
        }
        return Result.result(Result.FAIL_CODE, "暂无新的提币推送消息", "暂无新的提币推送消息");
    }
    
    @RequestMapping(path="getRealNameNotice",method=RequestMethod.POST)
    @ResponseBody
    public Result getRealNameNotice() {
        if(iNoticeService.blpopRealNameNotice()) {
            return Result.result(Result.SUCCESS_CODE, "有新的实名推送消息", "有新的实名推送消息");
        }
        return Result.result(Result.FAIL_CODE, "暂无新的实名推送消息", "暂无新的实名推送消息");
    }
}

前端




setInterval(function() {
    getNotice();
}, 10000);
function getNotice() {
    axios.post('/sys/notice/getOrderNotice').then(
            function(response) {
                var data = response.data;
                if(data.code===0){
                    document.getElementById('audio').play();
                }
            });
}

你可能感兴趣的:(基于redis的消息提醒功能)