前面界面基本功能做好了,但需要进行定时进行发送消息给使用者,这样才能做到提醒功能。
所以用到了jeecgboot的定时任务,首先建立下面的websocket发送任务类
代码如下:
package org.jeecg.modules.message.job; import java.util.List; import org.jeecg.common.util.DateUtils; import org.jeecg.modules.message.entity.SysMessage; import org.jeecg.modules.message.handle.ISendMsgHandle; import org.jeecg.modules.message.handle.enums.SendMsgStatusEnum; import org.jeecg.modules.message.handle.enums.SendMsgTypeEnum; import org.jeecg.modules.message.service.ISysMessageService; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import lombok.extern.slf4j.Slf4j; /** * @Description: 发送websocket消息job * @author: nbacheng * @date: 2023-05-08 * @version: V1.0 */ @Slf4j public class SendWebMsgJob implements Job { @Autowired private ISysMessageService sysMessageService; @Override public void execute(JobExecutionContext context) throws JobExecutionException { log.info(String.format(" Nbcio-Boot 发送消息任务 SendWebMsgJob ! 时间:" + DateUtils.getTimestamp())); // 1.读取消息中心数据,只查询未发送的和发送失败不超过次数的 QueryWrapper<SysMessage> queryWrapper = new QueryWrapper<SysMessage>(); queryWrapper.eq("es_send_status", SendMsgStatusEnum.WAIT.getCode()) .or(i -> i.eq("es_send_status", SendMsgStatusEnum.FAIL.getCode()).lt("es_send_num", 6)); List<SysMessage> sysMessages = sysMessageService.list(queryWrapper); System.out.println(sysMessages); // 2.根据不同的类型走不通的发送实现类 for (SysMessage sysMessage : sysMessages) { ISendMsgHandle sendMsgHandle = null; try { if (sysMessage.getEsType().equals(SendMsgTypeEnum.WEBMSG.getType())) {//目前只发送websocket消息 sendMsgHandle = (ISendMsgHandle) Class.forName(SendMsgTypeEnum.WEBMSG.getImplClass()).newInstance(); } } catch (Exception e) { log.error(e.getMessage(),e); } Integer sendNum = sysMessage.getEsSendNum(); try { sendMsgHandle.SendMsg(sysMessage.getEsReceiver(), sysMessage.getEsTitle(), sysMessage.getEsContent().toString(),sysMessage.getCreateBy()); // 发送消息成功 sysMessage.setEsSendStatus(SendMsgStatusEnum.SUCCESS.getCode()); } catch (Exception e) { e.printStackTrace(); // 发送消息出现异常 sysMessage.setEsSendStatus(SendMsgStatusEnum.FAIL.getCode()); } sysMessage.setEsSendNum(++sendNum); // 发送结果回写到数据库 sysMessageService.updateById(sysMessage); } } }
上面用到了下面的实现类,实现websocket的消息发送
package org.jeecg.modules.message.handle.impl; import lombok.extern.slf4j.Slf4j; import org.jeecg.common.api.dto.message.MessageDTO; import org.jeecg.common.system.api.ISysBaseAPI; import org.jeecg.common.util.SpringContextUtils; import org.jeecg.modules.message.handle.ISendMsgHandle; @Slf4j public class WebMsgSendMsgHandle implements ISendMsgHandle { @Override public void SendMsg(String es_receiver, String es_title, String es_content, String es_fromuser) { log.info("WebMsgSendMsgHandle 发websocket消息"); ISysBaseAPI sysBaseAPI = (ISysBaseAPI) SpringContextUtils.getBean(ISysBaseAPI.class); MessageDTO messagedto = new MessageDTO(); messagedto.setContent(es_content); messagedto.setCategory("1"); //放到通知通告了 messagedto.setFromUser(es_fromuser); messagedto.setTitle(es_title); messagedto.setToUser(es_receiver); sysBaseAPI.sendSysAnnouncement(messagedto); } }
这样启动定时任务后,就会根据规定的时间进行发送了。
2023-05-10 14:32:50.052 [MyScheduler_Worker-10] INFO org.jeecg.modules.message.job.SendWebMsgJob:35 - Nbcio-Boot 发送消息任务 SendWebMsgJob ! 时间:2023-05-10 14:32:50.052 [SysMessage(esContent=日程安排消息发送测试, esParam=null, esReceiver=admin, esResult=null, esSendNum=0, esSendStatus=0, esSendTime=Mon May 08 14:43:00 CST 2023, esTitle=日程安排消息发送测试, esType=4, remark=null)] 2023-05-10 14:32:50.073 [MyScheduler_Worker-10] INFO o.j.m.message.handle.impl.WebMsgSendMsgHandle:15 - WebMsgSendMsgHandle 发websocket消息 2023-05-10 14:32:50.177 [redisContainer-3] INFO org.jeecg.modules.message.websocket.SocketHandler:23 - 【SocketHandler消息】Redis Listerer:{handlerName=socketHandler, message={"msgId":"1656185507556954114","cmd":"user","msgTxt":"日程安排消息发送测试","userId":"e9ca23d68d884d4ebb19d07889727dae"}, userId=e9ca23d68d884d4ebb19d07889727dae} 2023-05-10 14:32:50.187 [redisContainer-3] INFO org.jeecg.modules.message.websocket.WebSocket:85 - 【websocket消息】 单点消息:{"msgId":"1656185507556954114","cmd":"user","msgTxt":"日程安排消息发送测试","userId":"e9ca23d68d884d4ebb19d07889727dae"}
这样就定时发送了,效果图如下: