package com.zy.acs.manager.core.service; import com.zy.acs.framework.exception.CoolException; import com.zy.acs.manager.manager.entity.Agv; import com.zy.acs.manager.manager.entity.Jam; import com.zy.acs.manager.manager.entity.Task; import com.zy.acs.manager.manager.enums.TaskTypeType; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.interceptor.TransactionAspectSupport; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; /** * Created by vincent on 11/12/2024 */ @Slf4j @Service public class MainLockWrapService { private static final int LOCK_TIMEOUT = 5; private final ReentrantLock lock = new ReentrantLock(Boolean.TRUE); @Autowired private MainService mainService; public void buildMajorTask(Long agvId, List taskList) { boolean lockAcquired = false; try { if (!(lockAcquired = this.lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS))) { throw new CoolException("failed to generate [major task] action, cause can not acquire lock ..."); } mainService.buildMajorTask(agvId, taskList); } catch (Exception e) { log.error("MainLockWrapService.buildMajorTask[task]", e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } finally { if (lockAcquired) { this.lock.unlock(); } } } public boolean buildMinorTask(Long agvId, TaskTypeType taskType, String destination, Jam jam) { boolean lockAcquired = false; try { if (!(lockAcquired = this.lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS))) { throw new CoolException("failed to generate [minor task] actions, cause can not acquire lock ..."); } // log.info("buildMinorTask AGV[{}] lock time: {}", agv.getUuid(), System.currentTimeMillis()); return mainService.buildMinorTask(agvId, taskType, destination, jam); } catch (Exception e) { log.error("MainLockWrapService.buildMinorTask[task]", e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return false; } finally { if (lockAcquired) { this.lock.unlock(); } // log.info("buildMinorTask AGV[{}] unlock time: {}", agv.getUuid(), System.currentTimeMillis()); } } }