背景 最近项目里有个类似备忘录的功能,每个用户管理多个客户,每个客户都有一个备忘录,一旦员工管理的客户多了,备忘录自然也多了,往往这时员工就搞不清啥时候该干啥了,所以要做一个类似小秘书的功能,按照备忘录里设置执行时间前30分钟通知一下员工,当然其他的消息都可以通知。
功能介绍 员工会针对某个客户添加一个执行计划,哪天几点上门/电话/短信拜访客户或其他事项。如果说我们设置在执行时间前30分钟后台主动推送消息到前台通知员工的话这时需要用到的技术是:延时队列+websocket
延时队列 我这边用的是rabbitmq,添加执行计划成功后,把执行时间减半小时压入延时队列里,等待到期消费,到期后调用websocket向前台发消息通知员工。
当然也可以写定时任务1min轮训一次,但这样太low。
**websocket:**springboot整合websocket
由于本篇主要讲解springboot整合websocket,所以延时队列怎么实现暂时不做赘述:
- maven先加入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
- 写个websocket配置类:
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* WebSocket配置
* @author zhanghang
* @date 2019/1/3 17:53
*/
@Component
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
- 由于我们的websocket对象中肯定需要用到用户信息