@ -46,6 +46,9 @@ import org.springframework.util.concurrent.ListenableFutureCallback;
import com.google.common.base.Strings ;
/ * *
* Used to execute { @link WorkflowExecuteRunnable } , when
* /
@Component
public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
@ -69,7 +72,7 @@ public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
/ * *
* multi - thread filter , avoid handling workflow at the same time
* /
private ConcurrentHashMap < String , WorkflowExecuteRunnable > multiThreadFilterMap = new ConcurrentHashMap ( ) ;
private ConcurrentHashMap < String , WorkflowExecuteRunnable > multiThreadFilterMap = new ConcurrentHashMap < > ( ) ;
@PostConstruct
private void init ( ) {
@ -92,7 +95,7 @@ public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
}
/ * *
* start workflow
* Start the given workflow .
* /
public void startWorkflow ( WorkflowExecuteRunnable workflowExecuteThread ) {
ProcessInstanceMetrics . incProcessInstanceSubmit ( ) ;
@ -100,13 +103,14 @@ public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
}
/ * *
* execute workflow
* Handle the events belong to the given workflow .
* /
public void executeEvent ( WorkflowExecuteRunnable workflowExecuteThread ) {
if ( ! workflowExecuteThread . isStart ( ) | | workflowExecuteThread . eventSize ( ) = = 0 ) {
return ;
}
if ( multiThreadFilterMap . containsKey ( workflowExecuteThread . getKey ( ) ) ) {
logger . warn ( "The workflow:{} has been executed by another thread" , workflowExecuteThread . getKey ( ) ) ;
return ;
}
multiThreadFilterMap . put ( workflowExecuteThread . getKey ( ) , workflowExecuteThread ) ;
@ -121,8 +125,6 @@ public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
@Override
public void onSuccess ( Object result ) {
// if an exception occurs, first, the error message cannot be printed in the log;
// secondly, the `multiThreadFilterMap` cannot remove the `workflowExecuteThread`, resulting in the state of process instance cannot be changed and memory leak
try {
if ( workflowExecuteThread . workFlowFinish ( ) ) {
stateWheelExecuteThread . removeProcess4TimeoutCheck ( workflowExecuteThread . getProcessInstance ( ) ) ;
@ -132,8 +134,10 @@ public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
}
} catch ( Exception e ) {
logger . error ( "handle events {} success, but notify changed error" , processInstanceId , e ) ;
} finally {
// make sure the process has been removed from multiThreadFilterMap
multiThreadFilterMap . remove ( workflowExecuteThread . getKey ( ) ) ;
}
multiThreadFilterMap . remove ( workflowExecuteThread . getKey ( ) ) ;
}
} ) ;
}