自动化立体仓库 - WCS系统
123
ZY
3 天以前 1a1e3398d15027a65e56e0ed82e54f3a4feea0b6
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
package com.zy.core.cache;
 
import com.zy.core.ThreadHandler;
import com.zy.core.enums.SlaveType;
 
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 线程缓存容器
 * Created by vincent on 2020/8/4
 */
public class SlaveConnection {
 
    private static final String _LINK = "_";
 
    private static final Map<String, ThreadHandler> conContain = new ConcurrentHashMap<>();
 
    public static void put(SlaveType type, Integer id, ThreadHandler threadHandler) {
        String key = toKey(type, id);
        remove(type, id);
        conContain.put(key, threadHandler);
    }
 
    public static ThreadHandler get(SlaveType type, Integer id) {
        return conContain.get(toKey(type, id));
    }
 
    public static void remove(SlaveType type, Integer id) {
        ThreadHandler threadHandler = get(type, id);
        if (null == threadHandler) {
            return;
        }
        conContain.remove(toKey(type, id));
        threadHandler.close();
    }
 
    public static Integer remove(ThreadHandler threadHandler) {
        if (null == threadHandler) {
            return null;
        }
        String key = null;
        for (Map.Entry<String, ThreadHandler> entry : conContain.entrySet()) {
            if (entry.getValue() == threadHandler) {
                key = entry.getKey();
                break;
            }
        }
        if (null != key) {
            SlaveType type = SlaveType.findInstance(key);
            Integer id = Integer.parseInt(key.split(_LINK)[1]);
            remove(type, id);
            return id;
        } else {
            return null;
        }
    }
 
    private static String toKey(SlaveType type, Integer id) {
        return type.toString() + _LINK + id;
    }
 
}