1
zhang
2026-01-24 6f2e70d03c9dde5cdb26b8911d6c19e2993a38ab
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
56
57
58
59
package com.zy.core;
 
import com.zy.core.enums.ConveyorStateType;
import com.zy.core.operation.OperationHandler;
import com.zy.core.properties.CtuOperationConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
@Slf4j
@Component
public class ConveyorOperationExecutor {
 
    private final Map<ConveyorStateType, OperationHandler> handlerMap = new ConcurrentHashMap<>();
 
    @Autowired
    public ConveyorOperationExecutor(List<OperationHandler> handlers) {
        // 自动注册所有操作处理器
        for (OperationHandler handler : handlers) {
            handlerMap.put(handler.getType(), handler);
            log.info("注册输送线操作处理器: {}", handler.getType());
        }
    }
 
    public void execute(CtuOperationConfig config) {
        OperationHandler handler = handlerMap.get(config.getType());
        if (handler == null) {
            log.warn("未找到操作处理器: {}", config.getType());
            return;
        }
 
        int retryCount = 0;
        while (retryCount < config.getMaxRetries()) {
            try {
                handler.execute(config);
                return;
            } catch (Exception e) {
                retryCount++;
                if (retryCount >= config.getMaxRetries()) {
                    log.error("操作执行失败,已达到最大重试次数: {}", config.getType(), e);
                } else {
                    log.warn("操作执行失败,正在重试({}/{})",
                            retryCount, config.getMaxRetries(), e);
                    try {
                        Thread.sleep(config.getRetryDelay());
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                        log.error("重试休眠被中断", ie);
                        return;
                    }
                }
            }
        }
    }
}