| | |
| | | @Slf4j |
| | | public class ZyStationV5Thread implements Runnable, com.zy.core.thread.StationThread { |
| | | |
| | | private static final int SEGMENT_EXECUTOR_POOL_SIZE = 64; |
| | | private static final int DEFAULT_SEGMENT_EXECUTOR_POOL_SIZE = 128; |
| | | private static final String CFG_SEGMENT_EXECUTOR_POOL_SIZE = "stationV5SegmentExecutorPoolSize"; |
| | | private static final int EXECUTOR_QUEUE_WARN_THRESHOLD = 20; |
| | | private static final int EXECUTOR_ACTIVE_WARN_THRESHOLD = 48; |
| | | private static final long SEGMENT_EXECUTE_WARN_MS = 10_000L; |
| | |
| | | private DeviceConfig deviceConfig; |
| | | private RedisUtil redisUtil; |
| | | private ZyStationConnectDriver zyStationConnectDriver; |
| | | private final ExecutorService executor = Executors.newFixedThreadPool(SEGMENT_EXECUTOR_POOL_SIZE); |
| | | private final ExecutorService executor; |
| | | private StationV5SegmentExecutor segmentExecutor; |
| | | private final RecentStationArrivalTracker recentArrivalTracker; |
| | | private final StationV5StatusReader statusReader; |
| | |
| | | public ZyStationV5Thread(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | | this.redisUtil = redisUtil; |
| | | int poolSize = resolveSegmentExecutorPoolSize(redisUtil); |
| | | this.executor = Executors.newFixedThreadPool(poolSize); |
| | | this.recentArrivalTracker = new RecentStationArrivalTracker(redisUtil); |
| | | this.segmentExecutor = new StationV5SegmentExecutor(deviceConfig, redisUtil, this::sendCommand); |
| | | this.statusReader = new StationV5StatusReader(deviceConfig, redisUtil, recentArrivalTracker); |
| | | this.runBlockReroutePlanner = new StationV5RunBlockReroutePlanner(redisUtil); |
| | | log.info("初始化V5输送线程池,deviceNo={}, poolSize={}", deviceConfig == null ? null : deviceConfig.getDeviceNo(), poolSize); |
| | | } |
| | | |
| | | @Override |
| | |
| | | map.put(stationProtocol.getStationId(), stationProtocol); |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | @Override |
| | | public List<Integer> getAllTaskNoList() { |
| | | return statusReader.getTaskNoList(); |
| | | } |
| | | |
| | | private void pollAndDispatchQueuedCommand() { |
| | |
| | | threadPoolExecutor.getCompletedTaskCount()); |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | private int resolveSegmentExecutorPoolSize(RedisUtil redisUtil) { |
| | | if (redisUtil == null) { |
| | | return DEFAULT_SEGMENT_EXECUTOR_POOL_SIZE; |
| | | } |
| | | try { |
| | | Object systemConfigMapObj = redisUtil.get(com.zy.core.enums.RedisKeyType.SYSTEM_CONFIG_MAP.key); |
| | | if (!(systemConfigMapObj instanceof HashMap)) { |
| | | return DEFAULT_SEGMENT_EXECUTOR_POOL_SIZE; |
| | | } |
| | | HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj; |
| | | String poolSizeText = systemConfigMap.get(CFG_SEGMENT_EXECUTOR_POOL_SIZE); |
| | | if (poolSizeText == null || poolSizeText.trim().isEmpty()) { |
| | | return DEFAULT_SEGMENT_EXECUTOR_POOL_SIZE; |
| | | } |
| | | int configured = Integer.parseInt(poolSizeText.trim()); |
| | | if (configured < 16) { |
| | | return 16; |
| | | } |
| | | return Math.min(configured, 512); |
| | | } catch (Exception ignore) { |
| | | return DEFAULT_SEGMENT_EXECUTOR_POOL_SIZE; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public CommandResponse sendOriginCommand(String address, short[] data) { |
| | | return zyStationConnectDriver.sendOriginCommand(address, data); |