| | |
| | | import java.security.MessageDigest; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.concurrent.locks.ReentrantLock; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.function.Supplier; |
| | | |
| | | @Component |
| | | public class StationMoveCoordinator { |
| | | |
| | | private static final int SESSION_EXPIRE_SECONDS = 60 * 60 * 24; |
| | | private final Map<Integer, ReentrantLock> taskDispatchLocks = new ConcurrentHashMap<>(); |
| | | |
| | | @Autowired |
| | | private StationMoveSessionRegistry sessionRegistry; |
| | |
| | | |
| | | public boolean isActiveRoute(Integer taskNo, Integer routeVersion) { |
| | | return sessionRegistry != null && sessionRegistry.isActiveRoute(taskNo, routeVersion); |
| | | } |
| | | |
| | | public boolean canDispatchRoute(Integer taskNo, Integer routeVersion) { |
| | | return sessionRegistry != null && sessionRegistry.canDispatchRoute(taskNo, routeVersion); |
| | | } |
| | | |
| | | public void markSegmentIssued(Integer taskNo, Integer routeVersion) { |
| | |
| | | saveSession(session); |
| | | } |
| | | |
| | | public <T> T withTaskDispatchLock(Integer taskNo, Supplier<T> supplier) { |
| | | if (supplier == null) { |
| | | return null; |
| | | } |
| | | if (taskNo == null || taskNo <= 0) { |
| | | return supplier.get(); |
| | | } |
| | | // 同一任务的切路和分段发送必须共享一把锁,避免旧 routeVersion 在线程晚到时继续把上一条段命令写出去。 |
| | | ReentrantLock lock = taskDispatchLocks.computeIfAbsent(taskNo, key -> new ReentrantLock()); |
| | | lock.lock(); |
| | | try { |
| | | return supplier.get(); |
| | | } finally { |
| | | lock.unlock(); |
| | | } |
| | | } |
| | | |
| | | public boolean shouldSuppressDispatch(Integer taskNo, Integer currentStationId, StationCommand candidateCommand) { |
| | | if (taskNo == null || taskNo <= 0 || currentStationId == null || candidateCommand == null) { |
| | | return false; |