|
|
|
@ -43,18 +43,21 @@ public class StateWheelExecuteThread extends Thread {
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(StateWheelExecuteThread.class); |
|
|
|
|
|
|
|
|
|
ConcurrentHashMap<Integer, ProcessInstance> processInstanceCheckList; |
|
|
|
|
ConcurrentHashMap<Integer, TaskInstance> taskInstanceCheckList; |
|
|
|
|
private ConcurrentHashMap<Integer, ProcessInstance> processInstanceTimeoutCheckList; |
|
|
|
|
private ConcurrentHashMap<Integer, TaskInstance> taskInstanceTimeoutCheckList; |
|
|
|
|
private ConcurrentHashMap<Integer, TaskInstance> taskInstanceRetryCheckList; |
|
|
|
|
private ProcessInstanceExecCacheManager processInstanceExecCacheManager; |
|
|
|
|
|
|
|
|
|
private int stateCheckIntervalSecs; |
|
|
|
|
|
|
|
|
|
public StateWheelExecuteThread(ConcurrentHashMap<Integer, ProcessInstance> processInstances, |
|
|
|
|
ConcurrentHashMap<Integer, TaskInstance> taskInstances, |
|
|
|
|
public StateWheelExecuteThread(ConcurrentHashMap<Integer, ProcessInstance> processInstanceTimeoutCheckList, |
|
|
|
|
ConcurrentHashMap<Integer, TaskInstance> taskInstanceTimeoutCheckList, |
|
|
|
|
ConcurrentHashMap<Integer, TaskInstance> taskInstanceRetryCheckList, |
|
|
|
|
ProcessInstanceExecCacheManager processInstanceExecCacheManager, |
|
|
|
|
int stateCheckIntervalSecs) { |
|
|
|
|
this.processInstanceCheckList = processInstances; |
|
|
|
|
this.taskInstanceCheckList = taskInstances; |
|
|
|
|
this.processInstanceTimeoutCheckList = processInstanceTimeoutCheckList; |
|
|
|
|
this.taskInstanceTimeoutCheckList = taskInstanceTimeoutCheckList; |
|
|
|
|
this.taskInstanceRetryCheckList = taskInstanceRetryCheckList; |
|
|
|
|
this.processInstanceExecCacheManager = processInstanceExecCacheManager; |
|
|
|
|
this.stateCheckIntervalSecs = stateCheckIntervalSecs; |
|
|
|
|
} |
|
|
|
@ -65,8 +68,9 @@ public class StateWheelExecuteThread extends Thread {
|
|
|
|
|
logger.info("state wheel thread start"); |
|
|
|
|
while (Stopper.isRunning()) { |
|
|
|
|
try { |
|
|
|
|
checkProcess(); |
|
|
|
|
checkTask(); |
|
|
|
|
checkTask4Timeout(); |
|
|
|
|
checkTask4Retry(); |
|
|
|
|
checkProcess4Timeout(); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error("state wheel thread check error:", e); |
|
|
|
|
} |
|
|
|
@ -74,85 +78,96 @@ public class StateWheelExecuteThread extends Thread {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean addProcess(ProcessInstance processInstance) { |
|
|
|
|
this.processInstanceCheckList.put(processInstance.getId(), processInstance); |
|
|
|
|
return true; |
|
|
|
|
public void addProcess4TimeoutCheck(ProcessInstance processInstance) { |
|
|
|
|
this.processInstanceTimeoutCheckList.put(processInstance.getId(), processInstance); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean addTask(TaskInstance taskInstance) { |
|
|
|
|
this.taskInstanceCheckList.put(taskInstance.getId(), taskInstance); |
|
|
|
|
return true; |
|
|
|
|
public void addTask4TimeoutCheck(TaskInstance taskInstance) { |
|
|
|
|
this.taskInstanceTimeoutCheckList.put(taskInstance.getId(), taskInstance); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void addTask4RetryCheck(TaskInstance taskInstance) { |
|
|
|
|
this.taskInstanceRetryCheckList.put(taskInstance.getId(), taskInstance); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void checkTask() { |
|
|
|
|
if (taskInstanceCheckList.isEmpty()) { |
|
|
|
|
public void checkTask4Timeout() { |
|
|
|
|
if (taskInstanceTimeoutCheckList.isEmpty()) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (TaskInstance taskInstance : this.taskInstanceCheckList.values()) { |
|
|
|
|
for (TaskInstance taskInstance : taskInstanceTimeoutCheckList.values()) { |
|
|
|
|
if (TimeoutFlag.OPEN == taskInstance.getTaskDefine().getTimeoutFlag()) { |
|
|
|
|
long timeRemain = DateUtils.getRemainTime(taskInstance.getStartTime(), taskInstance.getTaskDefine().getTimeout() * Constants.SEC_2_MINUTES_TIME_UNIT); |
|
|
|
|
if (0 >= timeRemain && processTimeout(taskInstance)) { |
|
|
|
|
taskInstanceCheckList.remove(taskInstance.getId()); |
|
|
|
|
if (0 >= timeRemain) { |
|
|
|
|
addTaskTimeoutEvent(taskInstance); |
|
|
|
|
taskInstanceTimeoutCheckList.remove(taskInstance.getId()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void checkTask4Retry() { |
|
|
|
|
if (taskInstanceRetryCheckList.isEmpty()) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (TaskInstance taskInstance : this.taskInstanceRetryCheckList.values()) { |
|
|
|
|
if (taskInstance.taskCanRetry() && taskInstance.retryTaskIntervalOverTime()) { |
|
|
|
|
processDependCheck(taskInstance); |
|
|
|
|
taskInstanceCheckList.remove(taskInstance.getId()); |
|
|
|
|
addTaskStateChangeEvent(taskInstance); |
|
|
|
|
taskInstanceRetryCheckList.remove(taskInstance.getId()); |
|
|
|
|
} |
|
|
|
|
if (taskInstance.isSubProcess() || taskInstance.isDependTask()) { |
|
|
|
|
processDependCheck(taskInstance); |
|
|
|
|
addTaskStateChangeEvent(taskInstance); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void checkProcess() { |
|
|
|
|
if (processInstanceCheckList.isEmpty()) { |
|
|
|
|
private void checkProcess4Timeout() { |
|
|
|
|
if (processInstanceTimeoutCheckList.isEmpty()) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
for (ProcessInstance processInstance : this.processInstanceCheckList.values()) { |
|
|
|
|
for (ProcessInstance processInstance : this.processInstanceTimeoutCheckList.values()) { |
|
|
|
|
|
|
|
|
|
long timeRemain = DateUtils.getRemainTime(processInstance.getStartTime(), processInstance.getTimeout() * Constants.SEC_2_MINUTES_TIME_UNIT); |
|
|
|
|
if (0 <= timeRemain && processTimeout(processInstance)) { |
|
|
|
|
processInstanceCheckList.remove(processInstance.getId()); |
|
|
|
|
if (0 >= timeRemain) { |
|
|
|
|
addProcessTimeoutEvent(processInstance); |
|
|
|
|
processInstanceTimeoutCheckList.remove(processInstance.getId()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void putEvent(StateEvent stateEvent) { |
|
|
|
|
|
|
|
|
|
if (!processInstanceExecCacheManager.contains(stateEvent.getProcessInstanceId())) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
WorkflowExecuteThread workflowExecuteThread = this.processInstanceExecCacheManager.getByProcessInstanceId(stateEvent.getProcessInstanceId()); |
|
|
|
|
workflowExecuteThread.addStateEvent(stateEvent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean processDependCheck(TaskInstance taskInstance) { |
|
|
|
|
private boolean addTaskStateChangeEvent(TaskInstance taskInstance) { |
|
|
|
|
StateEvent stateEvent = new StateEvent(); |
|
|
|
|
stateEvent.setType(StateEventType.TASK_STATE_CHANGE); |
|
|
|
|
stateEvent.setProcessInstanceId(taskInstance.getProcessInstanceId()); |
|
|
|
|
stateEvent.setTaskInstanceId(taskInstance.getId()); |
|
|
|
|
stateEvent.setExecutionStatus(ExecutionStatus.RUNNING_EXECUTION); |
|
|
|
|
putEvent(stateEvent); |
|
|
|
|
addEvent(stateEvent); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean processTimeout(TaskInstance taskInstance) { |
|
|
|
|
private boolean addTaskTimeoutEvent(TaskInstance taskInstance) { |
|
|
|
|
StateEvent stateEvent = new StateEvent(); |
|
|
|
|
stateEvent.setType(StateEventType.TASK_TIMEOUT); |
|
|
|
|
stateEvent.setProcessInstanceId(taskInstance.getProcessInstanceId()); |
|
|
|
|
stateEvent.setTaskInstanceId(taskInstance.getId()); |
|
|
|
|
putEvent(stateEvent); |
|
|
|
|
addEvent(stateEvent); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean processTimeout(ProcessInstance processInstance) { |
|
|
|
|
private boolean addProcessTimeoutEvent(ProcessInstance processInstance) { |
|
|
|
|
StateEvent stateEvent = new StateEvent(); |
|
|
|
|
stateEvent.setType(StateEventType.PROCESS_TIMEOUT); |
|
|
|
|
stateEvent.setProcessInstanceId(processInstance.getId()); |
|
|
|
|
putEvent(stateEvent); |
|
|
|
|
addEvent(stateEvent); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void addEvent(StateEvent stateEvent) { |
|
|
|
|
if (!processInstanceExecCacheManager.contains(stateEvent.getProcessInstanceId())) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
WorkflowExecuteThread workflowExecuteThread = this.processInstanceExecCacheManager.getByProcessInstanceId(stateEvent.getProcessInstanceId()); |
|
|
|
|
workflowExecuteThread.addStateEvent(stateEvent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|