fix(接口测试): 修复等待控制器时间过长导致WebSocket连接中断问题

Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
fit2-zhao 2023-03-31 18:49:52 +08:00 committed by fit2-zhao
parent ae64b17dd2
commit 96f7817a66
2 changed files with 9 additions and 7 deletions

View File

@ -1,15 +1,15 @@
package io.metersphere.listener; package io.metersphere.listener;
import com.fit2cloud.quartz.anno.QuartzScheduled;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.service.ApiExecutionQueueService; import io.metersphere.service.ApiExecutionQueueService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
public class ApiExecutionQueueListener { public class ApiExecutionQueueListener {
private ApiExecutionQueueService queueService; private ApiExecutionQueueService queueService;
@Scheduled(cron = "0 0/10 0/1 * * ?") @QuartzScheduled(cron = "0 0/10 0/1 * * ?")
public void execute() { public void execute() {
if (queueService == null) { if (queueService == null) {
queueService = CommonBeanFactory.getBean(ApiExecutionQueueService.class); queueService = CommonBeanFactory.getBean(ApiExecutionQueueService.class);

View File

@ -1,22 +1,23 @@
package io.metersphere.websocket; package io.metersphere.websocket;
import com.fit2cloud.quartz.anno.QuartzScheduled;
import io.metersphere.api.dto.MsgDTO; import io.metersphere.api.dto.MsgDTO;
import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.WebSocketUtil; import io.metersphere.commons.utils.WebSocketUtil;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import jakarta.websocket.*; import jakarta.websocket.*;
import jakarta.websocket.server.PathParam; import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint; import jakarta.websocket.server.ServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
@Slf4j @Slf4j
@Component // 注册到spring @Component // 注册到spring
@ServerEndpoint("/websocket/{reportId}") // 创建websocket服务 @ServerEndpoint("/websocket/{reportId}") // 创建websocket服务
public class IndexWebSocket { public class IndexWebSocket {
private final long MAX_IDLE_TIMEOUT = 180000;
/** /**
* 连接成功响应 * 连接成功响应
@ -27,6 +28,7 @@ public class IndexWebSocket {
RemoteEndpoint.Async async = session.getAsyncRemote(); RemoteEndpoint.Async async = session.getAsyncRemote();
if (async != null) { if (async != null) {
async.sendText("CONN_SUCCEEDED"); async.sendText("CONN_SUCCEEDED");
session.setMaxIdleTimeout(MAX_IDLE_TIMEOUT);
} }
LoggerUtil.info("客户端: [" + reportId + "] : 连接成功!" + WebSocketUtil.ONLINE_USER_SESSIONS.size(), reportId); LoggerUtil.info("客户端: [" + reportId + "] : 连接成功!" + WebSocketUtil.ONLINE_USER_SESSIONS.size(), reportId);
} }
@ -65,7 +67,7 @@ public class IndexWebSocket {
/** /**
* 每一分钟群发一次心跳检查 * 每一分钟群发一次心跳检查
*/ */
@Scheduled(cron = "0 0/1 * * * ?") @QuartzScheduled(cron = "0 0/1 * * * ?")
public void heartbeatCheck() { public void heartbeatCheck() {
WebSocketUtil.sendMessageAll("heartbeat check"); WebSocketUtil.sendMessageAll("heartbeat check");
} }