@@ -24,6 +24,7 @@ public static WorkerFactoryOptions getDefaultInstance() {
2424
2525 private static final int DEFAULT_WORKFLOW_CACHE_SIZE = 600 ;
2626 private static final int DEFAULT_MAX_WORKFLOW_THREAD_COUNT = 600 ;
27+ private static final Duration DEFAULT_SHUTDOWN_CHECK_INTERVAL = Duration .ofMillis (250 );
2728
2829 private static final WorkerFactoryOptions DEFAULT_INSTANCE ;
2930
@@ -41,6 +42,7 @@ public static class Builder {
4142 private boolean enableLoggingInReplay ;
4243 private boolean usingVirtualWorkflowThreads ;
4344 private ExecutorService overrideLocalActivityTaskExecutor ;
45+ private Duration shutdownCheckInterval ;
4446
4547 private Builder () {}
4648
@@ -57,6 +59,7 @@ private Builder(WorkerFactoryOptions options) {
5759 this .enableLoggingInReplay = options .enableLoggingInReplay ;
5860 this .usingVirtualWorkflowThreads = options .usingVirtualWorkflowThreads ;
5961 this .overrideLocalActivityTaskExecutor = options .overrideLocalActivityTaskExecutor ;
62+ this .shutdownCheckInterval = options .shutdownCheckInterval ;
6063 }
6164
6265 /**
@@ -155,6 +158,22 @@ Builder setOverrideLocalActivityTaskExecutor(
155158 return this ;
156159 }
157160
161+ /**
162+ * Sets the interval between polls when checking for executor termination during shutdown. Lower
163+ * values speed up shutdown at the cost of more frequent polling.
164+ *
165+ * <p>Default is 250ms, which is suitable for production. For test environments, consider
166+ * setting a much lower value (e.g., 1ms) to minimize teardown overhead.
167+ *
168+ * @param shutdownCheckInterval the interval between shutdown polls. Must be positive.
169+ * @return this builder for chaining
170+ */
171+ @ Experimental
172+ public Builder setShutdownCheckInterval (Duration shutdownCheckInterval ) {
173+ this .shutdownCheckInterval = shutdownCheckInterval ;
174+ return this ;
175+ }
176+
158177 public WorkerFactoryOptions build () {
159178 return new WorkerFactoryOptions (
160179 workflowCacheSize ,
@@ -165,6 +184,7 @@ public WorkerFactoryOptions build() {
165184 enableLoggingInReplay ,
166185 usingVirtualWorkflowThreads ,
167186 overrideLocalActivityTaskExecutor ,
187+ shutdownCheckInterval ,
168188 false );
169189 }
170190
@@ -189,6 +209,7 @@ public WorkerFactoryOptions validateAndBuildWithDefaults() {
189209 enableLoggingInReplay ,
190210 usingVirtualWorkflowThreads ,
191211 overrideLocalActivityTaskExecutor ,
212+ shutdownCheckInterval ,
192213 true );
193214 }
194215 }
@@ -201,6 +222,7 @@ public WorkerFactoryOptions validateAndBuildWithDefaults() {
201222 private final boolean enableLoggingInReplay ;
202223 private final boolean usingVirtualWorkflowThreads ;
203224 private final ExecutorService overrideLocalActivityTaskExecutor ;
225+ private final Duration shutdownCheckInterval ;
204226
205227 private WorkerFactoryOptions (
206228 int workflowCacheSize ,
@@ -211,6 +233,7 @@ private WorkerFactoryOptions(
211233 boolean enableLoggingInReplay ,
212234 boolean usingVirtualWorkflowThreads ,
213235 ExecutorService overrideLocalActivityTaskExecutor ,
236+ Duration shutdownCheckInterval ,
214237 boolean validate ) {
215238 if (validate ) {
216239 Preconditions .checkState (workflowCacheSize >= 0 , "negative workflowCacheSize" );
@@ -233,6 +256,13 @@ private WorkerFactoryOptions(
233256 if (plugins == null ) {
234257 plugins = new WorkerPlugin [0 ];
235258 }
259+ if (shutdownCheckInterval != null ) {
260+ Preconditions .checkState (
261+ !shutdownCheckInterval .isNegative () && !shutdownCheckInterval .isZero (),
262+ "shutdownCheckInterval must be positive" );
263+ } else {
264+ shutdownCheckInterval = DEFAULT_SHUTDOWN_CHECK_INTERVAL ;
265+ }
236266 }
237267 this .workflowCacheSize = workflowCacheSize ;
238268 this .maxWorkflowThreadCount = maxWorkflowThreadCount ;
@@ -243,6 +273,7 @@ private WorkerFactoryOptions(
243273 this .enableLoggingInReplay = enableLoggingInReplay ;
244274 this .usingVirtualWorkflowThreads = usingVirtualWorkflowThreads ;
245275 this .overrideLocalActivityTaskExecutor = overrideLocalActivityTaskExecutor ;
276+ this .shutdownCheckInterval = shutdownCheckInterval ;
246277 }
247278
248279 public int getWorkflowCacheSize () {
@@ -291,6 +322,16 @@ ExecutorService getOverrideLocalActivityTaskExecutor() {
291322 return overrideLocalActivityTaskExecutor ;
292323 }
293324
325+ /**
326+ * Returns the interval between polls when checking for executor termination during shutdown.
327+ *
328+ * @return the configured shutdown check interval
329+ */
330+ @ Experimental
331+ public Duration getShutdownCheckInterval () {
332+ return shutdownCheckInterval ;
333+ }
334+
294335 /**
295336 * @deprecated not used anymore by JavaSDK, this value doesn't have any effect
296337 */
0 commit comments