Skip to content

Commit 992feec

Browse files
committed
[Fix #722] Close executor service if created
Signed-off-by: fjtirado <[email protected]>
1 parent 9f7b967 commit 992feec

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/DefaultExecutorServiceFactory.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,37 @@
1717

1818
import java.util.concurrent.ExecutorService;
1919
import java.util.concurrent.Executors;
20+
import java.util.concurrent.locks.Lock;
21+
import java.util.concurrent.locks.ReentrantLock;
2022

2123
public class DefaultExecutorServiceFactory implements ExecutorServiceFactory {
2224

23-
private static final ExecutorServiceFactory instance = new DefaultExecutorServiceFactory();
25+
private ExecutorService service;
26+
private Lock serviceLock = new ReentrantLock();
2427

2528
public static ExecutorServiceFactory instance() {
26-
return instance;
27-
}
28-
29-
private static class ExecutorServiceHolder {
30-
private static ExecutorService instance = Executors.newCachedThreadPool();
29+
return new DefaultExecutorServiceFactory();
3130
}
3231

3332
@Override
3433
public ExecutorService get() {
35-
return ExecutorServiceHolder.instance;
34+
try {
35+
serviceLock.lock();
36+
if (service == null) {
37+
service = Executors.newCachedThreadPool();
38+
}
39+
} finally {
40+
serviceLock.unlock();
41+
}
42+
return service;
3643
}
3744

3845
private DefaultExecutorServiceFactory() {}
46+
47+
@Override
48+
public void close() {
49+
if (service != null) {
50+
service.shutdown();
51+
}
52+
}
3953
}

impl/core/src/main/java/io/serverlessworkflow/impl/ExecutorServiceFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
import java.util.concurrent.ExecutorService;
1919
import java.util.function.Supplier;
2020

21-
@FunctionalInterface
22-
public interface ExecutorServiceFactory extends Supplier<ExecutorService> {}
21+
public interface ExecutorServiceFactory extends Supplier<ExecutorService>, AutoCloseable {
22+
void close();
23+
}

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.ServiceLoader.Provider;
4040
import java.util.concurrent.ConcurrentHashMap;
4141
import java.util.concurrent.ExecutorService;
42-
import java.util.concurrent.Executors;
4342
import java.util.stream.Collectors;
4443

4544
public class WorkflowApplication implements AutoCloseable {
@@ -137,7 +136,7 @@ public SchemaValidator getValidator(SchemaInline inline) {
137136
private SchemaValidatorFactory schemaValidatorFactory;
138137
private WorkflowPositionFactory positionFactory = () -> new QueueWorkflowPosition();
139138
private WorkflowIdFactory idFactory = () -> UlidCreator.getMonotonicUlid().toString();
140-
private ExecutorServiceFactory executorFactory = () -> Executors.newCachedThreadPool();
139+
private ExecutorServiceFactory executorFactory = DefaultExecutorServiceFactory.instance();
141140
private EventConsumer<?, ?> eventConsumer = InMemoryEvents.get();
142141
private EventPublisher eventPublisher = InMemoryEvents.get();
143142
private RuntimeDescriptorFactory descriptorFactory =
@@ -236,6 +235,7 @@ public WorkflowDefinition workflowDefinition(Workflow workflow) {
236235

237236
@Override
238237
public void close() {
238+
executorFactory.close();
239239
for (WorkflowDefinition definition : definitions.values()) {
240240
definition.close();
241241
}

0 commit comments

Comments
 (0)