opt:发货区管理接口优化
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.nl.modules.mnt.websocket;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.nl.common.utils.RedissonUtils;
|
||||
import org.nl.modules.mnt.service.AutoRiKuService;
|
||||
import org.nl.modules.wql.util.SpringContextHolder;
|
||||
import org.nl.wms.basedata.st.service.StructattrService;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.PathParam;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2024/1/8 17:03
|
||||
*/
|
||||
@ServerEndpoint("/webSocket/sendRegion/{sid}")
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SendRegionWebSocketServer {
|
||||
|
||||
/**
|
||||
* concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
|
||||
*/
|
||||
private static CopyOnWriteArraySet<SendRegionWebSocketServer> webSocketSet = new CopyOnWriteArraySet<SendRegionWebSocketServer>();
|
||||
/**
|
||||
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
|
||||
*/
|
||||
private Session session;
|
||||
/**
|
||||
* 接收sid
|
||||
*/
|
||||
private String sid = "";
|
||||
/**
|
||||
* 连接建立成功调用的方法
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(Session session, @PathParam("sid") String sid) {
|
||||
this.session = session;
|
||||
//如果存在就先删除一个,防止重复推送消息
|
||||
for (SendRegionWebSocketServer webSocket : webSocketSet) {
|
||||
if (webSocket.sid.equals(sid)) {
|
||||
webSocketSet.remove(webSocket);
|
||||
}
|
||||
}
|
||||
webSocketSet.add(this);
|
||||
this.sid = sid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接关闭调用的方法
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose() {
|
||||
webSocketSet.remove(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收到客户端消息后调用的方法
|
||||
*
|
||||
* @param message 客户端发送过来的消息
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(String message, Session session) {
|
||||
System.out.println(webSocketSet.size()+"_接收到消息_"+session.getId());
|
||||
if (StringUtils.isNotEmpty(message)){
|
||||
RedissonUtils.lock(key -> {
|
||||
StructattrService service = SpringContextHolder.getBean(StructattrService.class);
|
||||
Map record = service.getStructByCodesFsAnNum(JSONArray.parseArray(message));
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
for (SendRegionWebSocketServer consumer : webSocketSet) {
|
||||
consumer.session.getBasicRemote().sendText(JSON.toJSONString(record));
|
||||
}
|
||||
}catch (Exception ex){
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}, message, message);
|
||||
}
|
||||
}
|
||||
|
||||
@OnError
|
||||
public void onError(Session session, Throwable error) {
|
||||
log.error("发生错误");
|
||||
webSocketSet.remove(session);
|
||||
error.printStackTrace();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user