網站首頁 java綜合 正文
WebSocket不使用@ServerEndpoint報java.lang.IllegalStateException: Unexpected use of scheduler
作者:小徐敲java 更新時間: 2024-04-03 java綜合第一種webSocket的WebSocketConfig.java類
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import java.util.concurrent.Executors;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Autowired
FlowWebSocketHandlerWs flowWebSocketHandlerWs;
@Autowired
FlowWebSocketDoorStatusWs flowWebSocketDoorStatusWs;
@Autowired
FlowWebSocketAttachmentWs flowWebSocketAttachmentWs;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(flowWebSocketHandlerWs, "/doorLog/ws")
.addInterceptors(new WebSocketHandshakeInterceptor()).setAllowedOrigins("*");
registry.addHandler(flowWebSocketDoorStatusWs, "/doorStatus/ws")
.addInterceptors(new WebSocketHandshakeInterceptor()).setAllowedOrigins("*");
registry.addHandler(flowWebSocketAttachmentWs, "/attatchment/ws")
.addInterceptors(new WebSocketHandshakeInterceptor()).setAllowedOrigins("*");
}
/**不使用@ServerEndpoint這樣會造成使用定時@scheduler出現問題需要注入一個@Bean*/
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler(Executors.newSingleThreadScheduledExecutor());
}
}
第二種sockJs的WebSocketConfig.java類
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import java.util.concurrent.Executors;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Autowired
FlowWebSocketHandlerWs flowWebSocketHandlerWs;
@Autowired
FlowWebSocketDoorStatusWs flowWebSocketDoorStatusWs;
@Autowired
FlowWebSocketAttachmentWs flowWebSocketAttachmentWs;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(flowWebSocketHandlerWs, "/doorLog/ws")
.addInterceptors(new WebSocketHandshakeInterceptor()).setAllowedOrigins("*").withSockJS();
registry.addHandler(flowWebSocketDoorStatusWs, "/doorStatus/ws")
.addInterceptors(new WebSocketHandshakeInterceptor()).setAllowedOrigins("*").withSockJS();
registry.addHandler(flowWebSocketAttachmentWs, "/attatchment/ws")
.addInterceptors(new WebSocketHandshakeInterceptor()).setAllowedOrigins("*").withSockJS();
}
/**不使用@ServerEndpoint這樣會造成使用定時@scheduler出現問題需要注入一個@Bean*/
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler(Executors.newSingleThreadScheduledExecutor());
}
}
webSocked攔截器WebSocketHandshakeInterceptor類
@Slf4j
public class WebSocketHandshakeInterceptor extends HttpSessionHandshakeInterceptor {
/**
* 握手前
*/
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler, Map<String, Object> attr) throws Exception {
String ip = request.getRemoteAddress().getHostString();
if (ip.indexOf(".0.0.1") > -1 || ip.indexOf(":0:0:1") > -1) {
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
return false;
}
}
User user = UserService.ipAndUserHashTable.get(ip);
if (user != null) {
return true;
} else {
user = UserService.ALL_ALIVE_USER_FOR_PHONE.get(ip);
if (user != null) {
return true;
}
}
return false;
}
/**
* 握手后
*/
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler, Exception e) {
log.info("webSocket======握手成功");
}
}
FlowWebSocketHandlerWs類
@Component
public class FlowWebSocketHandlerWs extends TextWebSocketHandler {
private List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
@Override
public void afterConnectionEstablished(WebSocketSession session) {
sessions.add(session);
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
// ...
}
}
FlowWebSocketDoorStatusWs類
@Component
public class FlowWebSocketDoorStatusWs extends TextWebSocketHandler {
private List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
@Override
public void afterConnectionEstablished(WebSocketSession session) {
sessions.add(session);
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
// ...
}
}
FlowWebSocketAttachmentWs類
@Component
public class FlowWebSocketAttachmentWs extends TextWebSocketHandler {
private List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
@Override
public void afterConnectionEstablished(WebSocketSession session) {
sessions.add(session);
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
// ...
}
}
原文鏈接:https://blog.csdn.net/qq_19891197/article/details/135974437
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-09-08 Python列表list的詳細用法介紹_python
- 2022-08-30 python中Requests請求的安裝與常見用法_python
- 2023-03-16 Python?asyncio異步編程簡單實現示例_python
- 2022-07-19 mybatis plus 代碼生成器配置
- 2022-08-02 shell自動拉取鏡像并運行容器的shell腳本_linux shell
- 2022-09-25 MyBatis實現多表查詢(一對一、一對多)的方式
- 2021-12-07 關于postman上傳文件執行成功而使用collection?runner執行失敗的問題_相關技巧
- 2021-12-01 Android?NDK開發(C語言--聯合體與枚舉)_Android
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支