zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
package com.codingapi.tx.framework.task;
 
import com.lorne.core.framework.utils.task.ConditionUtils;
import org.apache.commons.lang.StringUtils;
 
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * create by lorne on 2017/8/22
 */
public class TaskGroupManager {
 
    private Map<String, TaskGroup> taskMap = new ConcurrentHashMap<String, TaskGroup>();
 
    private static TaskGroupManager instance = null;
 
    private TaskGroupManager(){}
 
    public static TaskGroupManager getInstance() {
        if (instance == null) {
            synchronized (TaskGroupManager.class) {
                if(instance==null){
                    instance = new TaskGroupManager();
                }
            }
        }
        return instance;
    }
 
    public TaskGroup createTask(String key,String type) {
        TaskGroup taskGroup = getTaskGroup(key);
        if(taskGroup==null){
            taskGroup = new TaskGroup();
        }
        taskGroup.setKey(key);
 
        String taskKey = type+"_"+key;
 
        TxTask task =  new TxTask(ConditionUtils.getInstance().createTask(taskKey));
        taskGroup.setCurrent(task);
        taskGroup.addTask(task);
        taskMap.put(key, taskGroup);
        return taskGroup;
    }
 
    public TaskGroup getTaskGroup(String key) {
        return taskMap.get(key);
    }
 
 
    public TxTask getTask(String key,String type) {
        String taskKey = type+"_"+key;
        TaskGroup txGroup =  taskMap.get(key);
        if(txGroup!=null){
            for(TxTask task:txGroup.getTasks()){
                if(taskKey.equals(task.getKey())){
                    return task;
                }
            }
        }
        return null;
    }
 
 
    public void removeKey(String key) {
        if (StringUtils.isNotEmpty(key)) {
            taskMap.remove(key);
        }
    }
 
 
 
}