32
32
import java .util .HashSet ;
33
33
import java .util .Map ;
34
34
import java .util .concurrent .ConcurrentHashMap ;
35
+ import java .util .concurrent .ExecutorService ;
36
+ import java .util .concurrent .Executors ;
35
37
36
38
public class WorkflowApplication implements AutoCloseable {
37
39
@@ -43,8 +45,11 @@ public class WorkflowApplication implements AutoCloseable {
43
45
private final Collection <WorkflowExecutionListener > listeners ;
44
46
private final Map <WorkflowId , WorkflowDefinition > definitions ;
45
47
private final WorkflowPositionFactory positionFactory ;
48
+ private final ExecutorServiceFactory executorFactory ;
46
49
private final RuntimeDescriptorFactory runtimeDescriptorFactory ;
47
50
51
+ private ExecutorService executorService ;
52
+
48
53
public WorkflowApplication (
49
54
TaskExecutorFactory taskFactory ,
50
55
ExpressionFactory exprFactory ,
@@ -53,6 +58,7 @@ public WorkflowApplication(
53
58
WorkflowPositionFactory positionFactory ,
54
59
WorkflowIdFactory idFactory ,
55
60
RuntimeDescriptorFactory runtimeDescriptorFactory ,
61
+ ExecutorServiceFactory executorFactory ,
56
62
Collection <WorkflowExecutionListener > listeners ) {
57
63
this .taskFactory = taskFactory ;
58
64
this .exprFactory = exprFactory ;
@@ -61,6 +67,7 @@ public WorkflowApplication(
61
67
this .positionFactory = positionFactory ;
62
68
this .idFactory = idFactory ;
63
69
this .runtimeDescriptorFactory = runtimeDescriptorFactory ;
70
+ this .executorFactory = executorFactory ;
64
71
this .listeners = listeners ;
65
72
this .definitions = new ConcurrentHashMap <>();
66
73
}
@@ -101,6 +108,7 @@ public static class Builder {
101
108
private SchemaValidatorFactory schemaValidatorFactory = DefaultSchemaValidatorFactory .get ();
102
109
private WorkflowPositionFactory positionFactory = () -> new QueueWorkflowPosition ();
103
110
private WorkflowIdFactory idFactory = () -> UlidCreator .getMonotonicUlid ().toString ();
111
+ private ExecutorServiceFactory executorFactory = () -> Executors .newCachedThreadPool ();
104
112
private RuntimeDescriptorFactory descriptorFactory =
105
113
() -> new RuntimeDescriptor ("reference impl" , "1.0.0_alpha" , Collections .emptyMap ());
106
114
@@ -129,6 +137,11 @@ public Builder withResourceLoaderFactory(ResourceLoaderFactory resourceLoader) {
129
137
return this ;
130
138
}
131
139
140
+ public Builder withExecutorFactory (ExecutorServiceFactory executorFactory ) {
141
+ this .executorFactory = executorFactory ;
142
+ return this ;
143
+ }
144
+
132
145
public Builder withPositionFactory (WorkflowPositionFactory positionFactory ) {
133
146
this .positionFactory = positionFactory ;
134
147
return this ;
@@ -158,6 +171,7 @@ public WorkflowApplication build() {
158
171
positionFactory ,
159
172
idFactory ,
160
173
descriptorFactory ,
174
+ executorFactory ,
161
175
listeners == null
162
176
? Collections .emptySet ()
163
177
: Collections .unmodifiableCollection (listeners ));
@@ -190,4 +204,13 @@ public WorkflowPositionFactory positionFactory() {
190
204
public RuntimeDescriptorFactory runtimeDescriptorFactory () {
191
205
return runtimeDescriptorFactory ;
192
206
}
207
+
208
+ public ExecutorService executorService () {
209
+ synchronized (executorFactory ) {
210
+ if (executorService == null ) {
211
+ executorService = executorFactory .get ();
212
+ }
213
+ }
214
+ return executorService ;
215
+ }
193
216
}
0 commit comments