背景
公司系統(tǒng)中,要求做一個(gè)類型消息推送的東西,有一些待處理的消息要在管理端提供聲音消息提醒涩维,以便讓處理人員可以及時(shí)的處理删掀。我最開始想到的是用websocket通信的方式翔冀,做好了發(fā)現(xiàn)會莫名其妙的斷線,還需要加入心跳機(jī)制保證長時(shí)間沒消息推送的連接不斷披泪。最后我索性就用了輪訓(xùn)的方式纤子。
思路
使用redis中的隊(duì)列,配合前端定時(shí)輪訓(xùn)的方式
直接上代碼
package com.reaps.modules.sys.service;
public interface INoticeService {
/**
* push訂單提醒
*
* @return 是否成功推送
*/
public boolean lPushRetailOrderNotice();
/**
* push提幣提醒
*
* @return 是否成功推送
*/
public boolean lPushCoinOutNotice();
/**
* push實(shí)名提醒
*
* @return 是否成功推送
*/
public boolean lPushRealNameNotice();
/**
* 合約訂單隊(duì)列是否有提醒消息
* @return
*/
public boolean blpopRetailOrderNotice();
/**
* 提幣申請隊(duì)列是否有提醒消息
* @return
*/
public boolean blpopCoinOutNotice();
/**
* 實(shí)名認(rèn)證隊(duì)列是否有提醒消息
* @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隊(duì)列的提醒
*
* @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("推送了一條提幣申請?zhí)嵝严?);
}
return cacheService.lpush(COINOUTLIST, "coinOut");
}
@Override
public boolean lPushRealNameNotice() {
if (LOG.isInfoEnabled()) {
LOG.info("推送了一條實(shí)名認(rèn)證申請?zhí)嵝严?);
}
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, "有新的實(shí)名推送消息", "有新的實(shí)名推送消息");
}
return Result.result(Result.FAIL_CODE, "暫無新的實(shí)名推送消息", "暫無新的實(shí)名推送消息");
}
}
前端
<audio th:src="${domainName + 'notice-audio/retailOrder.mp3'}"
style='width: 0;height: 0;' controls id="audio"></audio>
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();
}
});
}