huang
2025-11-20 366ba040d2447bacd3455299425e3166f1f992bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.mes.task.controller;
 
import com.mes.task.service.TaskStatusNotificationService;
import com.mes.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 
/**
 * 任务状态通知控制器
 * 提供SSE端点用于实时推送任务执行状态
 * 
 * @author mes
 * @since 2025-01-XX
 */
@RestController
@RequestMapping("/api/plcSend/task/notification")
@Api(tags = "任务状态通知")
@RequiredArgsConstructor
public class TaskStatusNotificationController {
 
    private final TaskStatusNotificationService notificationService;
 
    @GetMapping(value = "/sse", produces = "text/event-stream")
    @ApiOperation("创建SSE连接,监听任务状态变化")
    public SseEmitter createConnection(@RequestParam(required = false) String taskId) {
        SseEmitter emitter = notificationService.createConnection(taskId);
        if (emitter == null) {
            throw new RuntimeException("创建SSE连接失败");
        }
        return emitter;
    }
 
    @GetMapping(value = "/sse/all", produces = "text/event-stream")
    @ApiOperation("创建SSE连接,监听所有任务状态变化")
    public SseEmitter createConnectionForAllTasks() {
        return createConnection(null);
    }
 
    @PostMapping("/close/{taskId}")
    @ApiOperation("关闭指定任务的SSE连接")
    public Result<Boolean> closeConnections(@PathVariable String taskId) {
        notificationService.closeConnections(taskId);
        return Result.success(true);
    }
 
    @PostMapping("/close/all")
    @ApiOperation("关闭所有SSE连接")
    public Result<Boolean> closeAllConnections() {
        notificationService.closeAllConnections();
        return Result.success(true);
    }
}