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;
|
}
|
}
|
}
|
}
|
}
|
}
|