#
Junjie
7 天以前 2c1e3b7b10c0d4afbf09a9151e132f1ee85b9c6f
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.zy.core.task;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.service.DeviceConfigService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.action.ShuttleAction;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.utils.TimeoutExecutor;
import org.springframework.scheduling.annotation.Scheduled;
 
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
public class ShuttleExecuteScheduler implements Runnable {
 
    private ShuttleAction shuttleAction;
    private DeviceConfigService deviceConfigService;
    private RedisUtil redisUtil;
    private int threadControlCount;
 
    @Scheduled(cron = "0/1 * * * * ? ")
    public void execute() {
 
    }
 
    public ShuttleExecuteScheduler(ShuttleAction shuttleAction, DeviceConfigService deviceConfigService, RedisUtil redisUtil, int threadControlCount) {
        this.shuttleAction = shuttleAction;
        this.deviceConfigService = deviceConfigService;
        this.redisUtil = redisUtil;
        this.threadControlCount = threadControlCount;
    }
 
    @Override
    public void run() {
        List<DeviceConfig> shuttleList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                .eq("device_type", String.valueOf(SlaveType.Shuttle)));
 
        List<List<DeviceConfig>> lists = new ArrayList<>();
        List<DeviceConfig> tmp = new ArrayList<>();
 
        for (int i = 0; i < shuttleList.size(); i++) {
            DeviceConfig deviceConfig = shuttleList.get(i);
            if (i != 0 && i % threadControlCount == 0) {
                lists.add(tmp);
                tmp = new ArrayList<>();
            }
            tmp.add(deviceConfig);
        }
        lists.add(tmp);
 
        for (List<DeviceConfig> list : lists) {
            if (list.isEmpty()) {
                continue;
            }
 
            new Thread(() -> {
                while (true) {
                    try {
                        for (DeviceConfig deviceConfig : list) {
                            Object object = redisUtil.get(RedisKeyType.SHUTTLE_FLAG.key + deviceConfig.getDeviceNo());
                            if (object == null) {
                                continue;
                            }
 
                            int taskNo = Integer.parseInt(String.valueOf(object));
                            if (taskNo != 0) {
                                //存在任务需要执行
                                long startTime = System.currentTimeMillis();
                                News.info("execute {},{}", deviceConfig.getDeviceNo(), taskNo);
                                // 在循环中使用
                                boolean result = TimeoutExecutor.executeWithTimeout(
                                        () -> shuttleAction.executeWork(deviceConfig.getDeviceNo(), taskNo),
                                        30,  // 30秒超时
                                        TimeUnit.SECONDS
                                );
                                Thread.sleep(100);
                                News.info("execute end {},{},{}", deviceConfig.getDeviceNo(), taskNo, System.currentTimeMillis() - startTime);
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}