15
15
public class ExecutorServiceManager {
16
16
private static final Logger log = LoggerFactory .getLogger (ExecutorServiceManager .class );
17
17
private static ExecutorServiceManager instance ;
18
-
19
18
private final ExecutorService executor ;
20
- private ExecutorService workflowExecutor ;
19
+ private final ExecutorService workflowExecutor ;
21
20
private final int terminationTimeoutSeconds ;
22
21
23
- private ExecutorServiceManager (InstrumentedExecutorService executor ,
22
+ private ExecutorServiceManager (ExecutorService executor , ExecutorService workflowExecutor ,
24
23
int terminationTimeoutSeconds ) {
25
- this .executor = executor ;
24
+ this .executor = new InstrumentedExecutorService (executor );
25
+ this .workflowExecutor = new InstrumentedExecutorService (workflowExecutor );
26
26
this .terminationTimeoutSeconds = terminationTimeoutSeconds ;
27
27
}
28
28
29
29
public static void init () {
30
30
if (instance == null ) {
31
31
final var configuration = ConfigurationServiceProvider .instance ();
32
- instance = new ExecutorServiceManager (
33
- new InstrumentedExecutorService (configuration .getExecutorService ()),
32
+ final var executorService = configuration .getExecutorService ();
33
+ final var workflowExecutorService = configuration .getWorkflowExecutorService ();
34
+ instance = new ExecutorServiceManager (executorService , workflowExecutorService ,
34
35
configuration .getTerminationTimeoutSeconds ());
35
- log .debug ("Initialized ExecutorServiceManager executor: {}, timeout: {}" ,
36
- configuration .getExecutorService ().getClass (),
36
+ log .debug (
37
+ "Initialized ExecutorServiceManager executor: {}, workflow executor: {}, timeout: {}" ,
38
+ executorService .getClass (),
39
+ workflowExecutorService .getClass (),
37
40
configuration .getTerminationTimeoutSeconds ());
38
41
} else {
39
42
log .debug ("Already started, reusing already setup instance!" );
40
43
}
41
44
}
42
45
43
- public static void stop () {
46
+ public synchronized static void stop () {
44
47
if (instance != null ) {
45
48
instance .doStop ();
46
49
}
@@ -49,7 +52,7 @@ public static void stop() {
49
52
instance = null ;
50
53
}
51
54
52
- public static ExecutorServiceManager instance () {
55
+ public synchronized static ExecutorServiceManager instance () {
53
56
if (instance == null ) {
54
57
// provide a default configuration if none has been provided by init
55
58
init ();
@@ -61,26 +64,17 @@ public ExecutorService executorService() {
61
64
return executor ;
62
65
}
63
66
64
- // needs to be synchronized since only called when a workflow is initialized, but that happens in
65
- // controllers which are started in parallel
66
- public synchronized ExecutorService workflowExecutorService () {
67
- if (workflowExecutor == null ) {
68
- workflowExecutor =
69
- new InstrumentedExecutorService (
70
- ConfigurationServiceProvider .instance ().getWorkflowExecutorService ());
71
- }
67
+ public ExecutorService workflowExecutorService () {
72
68
return workflowExecutor ;
73
69
}
74
70
75
71
private void doStop () {
76
72
try {
77
73
log .debug ("Closing executor" );
78
74
executor .shutdown ();
79
- if (workflowExecutor != null ) {
80
- workflowExecutor .shutdown ();
81
- if (!workflowExecutor .awaitTermination (terminationTimeoutSeconds , TimeUnit .SECONDS )) {
82
- workflowExecutor .shutdownNow (); // if we timed out, waiting, cancel everything
83
- }
75
+ workflowExecutor .shutdown ();
76
+ if (!workflowExecutor .awaitTermination (terminationTimeoutSeconds , TimeUnit .SECONDS )) {
77
+ workflowExecutor .shutdownNow (); // if we timed out, waiting, cancel everything
84
78
}
85
79
if (!executor .awaitTermination (terminationTimeoutSeconds , TimeUnit .SECONDS )) {
86
80
executor .shutdownNow (); // if we timed out, waiting, cancel everything
0 commit comments