@ -46,6 +46,9 @@ import org.springframework.util.concurrent.ListenableFutureCallback;
import com.google.common.base.Strings ;
import com.google.common.base.Strings ;
/ * *
* Used to execute { @link WorkflowExecuteRunnable } , when
* /
@Component
@Component
public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
@ -69,7 +72,7 @@ public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
/ * *
/ * *
* multi - thread filter , avoid handling workflow at the same time
* 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
@PostConstruct
private void init ( ) {
private void init ( ) {
@ -92,7 +95,7 @@ public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
}
}
/ * *
/ * *
* start workflow
* Start the given workflow .
* /
* /
public void startWorkflow ( WorkflowExecuteRunnable workflowExecuteThread ) {
public void startWorkflow ( WorkflowExecuteRunnable workflowExecuteThread ) {
ProcessInstanceMetrics . incProcessInstanceSubmit ( ) ;
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 ) {
public void executeEvent ( WorkflowExecuteRunnable workflowExecuteThread ) {
if ( ! workflowExecuteThread . isStart ( ) | | workflowExecuteThread . eventSize ( ) = = 0 ) {
if ( ! workflowExecuteThread . isStart ( ) | | workflowExecuteThread . eventSize ( ) = = 0 ) {
return ;
return ;
}
}
if ( multiThreadFilterMap . containsKey ( workflowExecuteThread . getKey ( ) ) ) {
if ( multiThreadFilterMap . containsKey ( workflowExecuteThread . getKey ( ) ) ) {
logger . warn ( "The workflow:{} has been executed by another thread" , workflowExecuteThread . getKey ( ) ) ;
return ;
return ;
}
}
multiThreadFilterMap . put ( workflowExecuteThread . getKey ( ) , workflowExecuteThread ) ;
multiThreadFilterMap . put ( workflowExecuteThread . getKey ( ) , workflowExecuteThread ) ;
@ -121,8 +125,6 @@ public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
@Override
@Override
public void onSuccess ( Object result ) {
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 {
try {
if ( workflowExecuteThread . workFlowFinish ( ) ) {
if ( workflowExecuteThread . workFlowFinish ( ) ) {
stateWheelExecuteThread . removeProcess4TimeoutCheck ( workflowExecuteThread . getProcessInstance ( ) ) ;
stateWheelExecuteThread . removeProcess4TimeoutCheck ( workflowExecuteThread . getProcessInstance ( ) ) ;
@ -132,8 +134,10 @@ public class WorkflowExecuteThreadPool extends ThreadPoolTaskExecutor {
}
}
} catch ( Exception e ) {
} catch ( Exception e ) {
logger . error ( "handle events {} success, but notify changed error" , processInstanceId , 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 ( ) ) ;
}
}
} ) ;
} ) ;
}
}