| | |
| | | import java.util.Map; |
| | | import java.util.concurrent.*; |
| | | import jakarta.annotation.PreDestroy; |
| | | import org.apache.commons.lang3.concurrent.BasicThreadFactory; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
| | | import com.battery.business.service.ITestLiveService; |
| | |
| | | @Component |
| | | public class TestLiveStream { |
| | | private final ITestLiveService live; |
| | | private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); |
| | | /** |
| | | * SSE 推送线程池显式创建并命名,便于按连接排查推送任务。 |
| | | * |
| | | * <p>连接关闭后立即移除已取消的定点任务,避免取消任务在延迟队列里滞留到下一个周期。</p> |
| | | */ |
| | | private final ScheduledThreadPoolExecutor scheduler = scheduler(); |
| | | public TestLiveStream(ITestLiveService live) { this.live = live; } |
| | | |
| | | private static ScheduledThreadPoolExecutor scheduler() |
| | | { |
| | | ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2, |
| | | BasicThreadFactory.builder().namingPattern("sse-live-pool-%d").daemon(true).build()); |
| | | executor.setRemoveOnCancelPolicy(true); |
| | | return executor; |
| | | } |
| | | |
| | | /** 建立指定测试的 SSE 连接,先推一次当前状态,随后每 3 秒推一次;连接完成、超时或出错时取消该推送任务。 */ |
| | | public SseEmitter stream(Long testId) |
| | | { |
| | | SseEmitter emitter = createEmitter(); |