refactor(websocket): 移除任务线程管理相关代码
This commit is contained in:
-29
@@ -25,16 +25,13 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@ServerEndpoint("/websocket/{sid}")
|
@ServerEndpoint("/websocket/{sid}")
|
||||||
@Getter
|
@Getter
|
||||||
public class CsWsServer {
|
public class CsWsServer {
|
||||||
private static final Map<String, Thread> taskThreadMap = new ConcurrentHashMap<>();
|
|
||||||
/**
|
/**
|
||||||
* 与某个客户端的连接会话, 需要通过它来给客户端发送数据
|
* 与某个客户端的连接会话, 需要通过它来给客户端发送数据
|
||||||
*/
|
*/
|
||||||
@@ -146,30 +143,4 @@ public class CsWsServer {
|
|||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(session, sid);
|
return Objects.hash(session, sid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Thread getTaskThread(String sid) {
|
|
||||||
Thread thread = taskThreadMap.get(sid);
|
|
||||||
if (thread == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (thread.isInterrupted()) {
|
|
||||||
taskThreadMap.remove(sid);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return thread;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void restartTask(Runnable runnable) {
|
|
||||||
stopTask(this.sid);
|
|
||||||
Thread thread = new Thread(runnable);
|
|
||||||
thread.start();
|
|
||||||
taskThreadMap.put(this.sid, thread);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stopTask(String sid) {
|
|
||||||
Thread thread = getTaskThread(this.sid);
|
|
||||||
if (thread != null) {
|
|
||||||
thread.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-30
@@ -25,16 +25,13 @@ import javax.websocket.*;
|
|||||||
import javax.websocket.server.PathParam;
|
import javax.websocket.server.PathParam;
|
||||||
import javax.websocket.server.ServerEndpoint;
|
import javax.websocket.server.ServerEndpoint;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@ServerEndpoint("/websocket/{sid}")
|
@ServerEndpoint("/websocket/{sid}")
|
||||||
@Getter
|
@Getter
|
||||||
public class CsWsServer {
|
public class CsWsServer {
|
||||||
private static final Map<String, Thread> taskThreadMap = new ConcurrentHashMap<>();
|
|
||||||
/**
|
/**
|
||||||
* 与某个客户端的连接会话, 需要通过它来给客户端发送数据
|
* 与某个客户端的连接会话, 需要通过它来给客户端发送数据
|
||||||
*/
|
*/
|
||||||
@@ -138,7 +135,7 @@ public class CsWsServer {
|
|||||||
if (o == null || getClass() != o.getClass()) {
|
if (o == null || getClass() != o.getClass()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
CsWsServer that = (CsWsServer) o;
|
CsWsServer that = (CsWsServer)o;
|
||||||
return Objects.equals(session, that.session) && Objects.equals(sid, that.sid);
|
return Objects.equals(session, that.session) && Objects.equals(sid, that.sid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,30 +143,4 @@ public class CsWsServer {
|
|||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(session, sid);
|
return Objects.hash(session, sid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Thread getTaskThread(String sid) {
|
|
||||||
Thread thread = taskThreadMap.get(sid);
|
|
||||||
if (thread == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (thread.isInterrupted()) {
|
|
||||||
taskThreadMap.remove(sid);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return thread;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void restartTask(Runnable runnable) {
|
|
||||||
stopTask(this.sid);
|
|
||||||
Thread thread = new Thread(runnable);
|
|
||||||
thread.start();
|
|
||||||
taskThreadMap.put(this.sid, thread);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stopTask(String sid) {
|
|
||||||
Thread thread = getTaskThread(this.sid);
|
|
||||||
if (thread != null) {
|
|
||||||
thread.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user