#
vincentlu
2025-01-13 89c7f6e5bcc21b0e8f83a2bc6d680e2ffe431e6f
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
60
61
62
63
64
65
66
67
68
69
70
71
72
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<Task> 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());
        }
    }
 
}