package com.ruoyi.web.websocket.config;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class ScheduleUtil {
private final static Logger logger = LoggerFactory.getLogger(ScheduleUtil.class);
@Scheduled(cron = "0/5 * 1 * * *")
public void work1(){
logger.info("定时任务");
}
@Scheduled(fixedDelay = 1000*1)
public void work2(){
logger.info("固定间隔任务");
}
@Scheduled(fixedRate = 1000*10)
public void work3(){
logger.info("固定频率任务");
}
public static final Map<String,Map<String,Object>> ConstantsUtildataMap = new ConcurrentHashMap<>();
@Scheduled(fixedDelay = 1000*1)
public void work4(){
Map<String,Map<String,Object>> dataMap = new HashMap<>();
dataMap.putAll(ConstantsUtildataMap);
ConstantsUtildataMap.clear();
for (WebSocket webSocket : WebSocket.webSockets) {
if (webSocket!=null){
Map<String, Object> map = dataMap.get(webSocket.userId);
try {
synchronized (webSocket){
webSocket.session.getAsyncRemote().sendText(JSONObject.toJSONString(map));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
logger.info("固定间隔任务");
}
}
package com.ruoyi.web.websocket.config;
import com.ruoyi.web.controller.demo.domain.UserOperateModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
@ServerEndpoint(value = "/ws/userId")
@Component
public class WebSocket {
public Session session;
public static CopyOnWriteArraySet<WebSocket> webSockets = new CopyOnWriteArraySet<>();
public String userId;
public List<UserOperateModel> userOperateModels = new ArrayList<>();
private static final Logger logger = LoggerFactory.getLogger(WebSocket.class);
public static WebSocket webSocketBean;
@PostConstruct
public void init(){
webSocketBean = this;
}
@OnOpen
public void open(Session session, @PathParam("userId") String userId){
this.session = session;
this.userId = userId;
webSockets.add(this);
logger.info("webSocket有新的连接"+userId+",当前连接总数为:"+webSockets.size());
}
@OnMessage
public void onMessage(String message,Session session){
for (WebSocket webSocket : WebSocket.webSockets) {
if (webSocket!=null&&webSocket.userId.equals("")){
Iterator<UserOperateModel> iterator = webSocket.userOperateModels.iterator();
while (iterator.hasNext()){
UserOperateModel next = iterator.next();
if (next.getCreateTime().equals("")){
iterator.remove();
}else {
webSocket.userOperateModels.add(new UserOperateModel());
}
}
}
}
logger.info("收到消息"+message);
}
@OnClose
public void onClose(){
boolean remove = webSockets.remove(this);
logger.info("webSocket有连接断开,当前总数为:"+webSockets.size()+remove);
}
public void sendMessage(String message){
this.session.getAsyncRemote().sendText(message);
}
}
|