Skip to content

Commit 7ecfa96

Browse files
committed
Add worker thread
1 parent 96d53bf commit 7ecfa96

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcWorker.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public final class DurableTaskGrpcWorker implements AutoCloseable {
6565
this.sidecarClient = TaskHubSidecarServiceGrpc.newBlockingStub(sidecarGrpcChannel);
6666
this.dataConverter = builder.dataConverter != null ? builder.dataConverter : new JacksonDataConverter();
6767
this.maximumTimerInterval = builder.maximumTimerInterval != null ? builder.maximumTimerInterval : DEFAULT_MAXIMUM_TIMER_INTERVAL;
68-
this.workerPool = Executors.newCachedThreadPool();
68+
this.workerPool = builder.executorService;
6969
}
7070

7171
/**
@@ -86,20 +86,8 @@ public void start() {
8686
* configured.
8787
*/
8888
public void close() {
89-
this.closeSideCarChannel();
9089
this.shutDownWorkerPool();
91-
}
92-
93-
private void closeSideCarChannel() {
94-
if (this.managedSidecarChannel != null) {
95-
try {
96-
this.managedSidecarChannel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
97-
} catch (InterruptedException e) {
98-
// Best effort. Also note that AutoClose documentation recommends NOT having
99-
// close() methods throw InterruptedException:
100-
// https://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html
101-
}
102-
}
90+
this.closeSideCarChannel();
10391
}
10492

10593
/**
@@ -213,6 +201,18 @@ public void stop() {
213201
this.close();
214202
}
215203

204+
private void closeSideCarChannel() {
205+
if (this.managedSidecarChannel != null) {
206+
try {
207+
this.managedSidecarChannel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
208+
} catch (InterruptedException e) {
209+
// Best effort. Also note that AutoClose documentation recommends NOT having
210+
// close() methods throw InterruptedException:
211+
// https://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html
212+
}
213+
}
214+
}
215+
216216
private void shutDownWorkerPool() {
217217
this.workerPool.shutdown();
218218
try {

client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcWorkerBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import java.time.Duration;
88
import java.util.HashMap;
9+
import java.util.concurrent.Executor;
10+
import java.util.concurrent.ExecutorService;
11+
import java.util.concurrent.Executors;
912

1013
/**
1114
* Builder object for constructing customized {@link DurableTaskGrpcWorker} instances.
@@ -17,6 +20,7 @@ public final class DurableTaskGrpcWorkerBuilder {
1720
Channel channel;
1821
DataConverter dataConverter;
1922
Duration maximumTimerInterval;
23+
ExecutorService executorService;
2024

2125
/**
2226
* Adds an orchestration factory to be used by the constructed {@link DurableTaskGrpcWorker}.
@@ -113,6 +117,17 @@ public DurableTaskGrpcWorkerBuilder maximumTimerInterval(Duration maximumTimerIn
113117
return this;
114118
}
115119

120+
/**
121+
* Sets the executor service that will be used to execute threads.
122+
*
123+
* @param executorService {@link ExecutorService}.
124+
* @return this builder object.
125+
*/
126+
public DurableTaskGrpcWorkerBuilder withExecutorService(ExecutorService executorService) {
127+
this.executorService = executorService;
128+
return this;
129+
}
130+
116131
/**
117132
* Initializes a new {@link DurableTaskGrpcWorker} object with the settings specified in the current builder object.
118133
* @return a new {@link DurableTaskGrpcWorker} object

0 commit comments

Comments
 (0)