@@ -39,7 +39,8 @@ public final class DurableTaskGrpcWorker implements AutoCloseable {
39
39
private final TaskHubSidecarServiceBlockingStub sidecarClient ;
40
40
private final boolean isExecutorServiceManaged ;
41
41
private volatile boolean isNormalShutdown = false ;
42
-
42
+ private Thread processorThread ;
43
+
43
44
DurableTaskGrpcWorker (DurableTaskGrpcWorkerBuilder builder ) {
44
45
this .orchestrationFactories .putAll (builder .orchestrationFactories );
45
46
this .activityFactories .putAll (builder .activityFactories );
@@ -70,7 +71,7 @@ public final class DurableTaskGrpcWorker implements AutoCloseable {
70
71
this .workerPool = builder .executorService != null ? builder .executorService : Executors .newCachedThreadPool ();
71
72
this .isExecutorServiceManaged = builder .executorService == null ;
72
73
}
73
-
74
+
74
75
/**
75
76
* Establishes a gRPC connection to the sidecar and starts processing work-items in the background.
76
77
* <p>
@@ -79,8 +80,10 @@ public final class DurableTaskGrpcWorker implements AutoCloseable {
79
80
* continues until either a connection succeeds or the process receives an interrupt signal.
80
81
*/
81
82
public void start () {
82
- new Thread (this ::startAndBlock ).start ();
83
+ this .processorThread = new Thread (this ::startAndBlock );
84
+ this .processorThread .start ();
83
85
}
86
+
84
87
85
88
/**
86
89
* Closes the internally managed gRPC channel and executor service, if one exists.
@@ -90,6 +93,7 @@ public void start() {
90
93
*/
91
94
public void close () {
92
95
this .isNormalShutdown = true ;
96
+ this .processorThread .interrupt ();
93
97
this .shutDownWorkerPool ();
94
98
this .closeSideCarChannel ();
95
99
}
@@ -118,7 +122,7 @@ public void startAndBlock() {
118
122
logger );
119
123
120
124
// TODO: How do we interrupt manually?
121
- while (true ) {
125
+ while (true && ! this . isNormalShutdown ) {
122
126
try {
123
127
GetWorkItemsRequest getWorkItemsRequest = GetWorkItemsRequest .newBuilder ().build ();
124
128
Iterator <WorkItem > workItemStream = this .sidecarClient .getWorkItems (getWorkItemsRequest );
0 commit comments