2626import io .temporal .common .interceptors .WorkerInterceptor ;
2727import io .temporal .common .interceptors .WorkflowClientInterceptor ;
2828import io .temporal .serviceclient .WorkflowServiceStubsOptions ;
29+ import io .temporal .worker .Worker ;
2930import io .temporal .worker .WorkerFactoryOptions ;
3031import io .temporal .worker .WorkerOptions ;
3132import java .util .ArrayList ;
3233import java .util .Arrays ;
3334import java .util .List ;
3435import java .util .Objects ;
36+ import java .util .function .BiConsumer ;
3537import java .util .function .Consumer ;
3638import javax .annotation .Nonnull ;
3739
@@ -69,6 +71,7 @@ public final class SimplePluginBuilder {
6971 private final List <Consumer <WorkflowClientOptions .Builder >> clientCustomizers = new ArrayList <>();
7072 private final List <Consumer <WorkerFactoryOptions .Builder >> factoryCustomizers = new ArrayList <>();
7173 private final List <Consumer <WorkerOptions .Builder >> workerCustomizers = new ArrayList <>();
74+ private final List <BiConsumer <String , Worker >> workerInitializers = new ArrayList <>();
7275 private final List <WorkerInterceptor > workerInterceptors = new ArrayList <>();
7376 private final List <WorkflowClientInterceptor > clientInterceptors = new ArrayList <>();
7477 private final List <ContextPropagator > contextPropagators = new ArrayList <>();
@@ -139,6 +142,29 @@ public SimplePluginBuilder customizeWorker(@Nonnull Consumer<WorkerOptions.Build
139142 return this ;
140143 }
141144
145+ /**
146+ * Adds an initializer that is called after a worker is created. This can be used to register
147+ * workflows, activities, and Nexus services on the worker.
148+ *
149+ * <p>Example:
150+ *
151+ * <pre>{@code
152+ * SimplePluginBuilder.newBuilder("my-plugin")
153+ * .initializeWorker((taskQueue, worker) -> {
154+ * worker.registerWorkflowImplementationTypes(MyWorkflow.class);
155+ * worker.registerActivitiesImplementations(new MyActivityImpl());
156+ * })
157+ * .build();
158+ * }</pre>
159+ *
160+ * @param initializer a consumer that receives the task queue name and worker
161+ * @return this builder for chaining
162+ */
163+ public SimplePluginBuilder initializeWorker (@ Nonnull BiConsumer <String , Worker > initializer ) {
164+ workerInitializers .add (Objects .requireNonNull (initializer ));
165+ return this ;
166+ }
167+
142168 /**
143169 * Adds worker interceptors. Interceptors are appended to any existing interceptors in the
144170 * configuration.
@@ -188,6 +214,7 @@ public PluginBase build() {
188214 new ArrayList <>(clientCustomizers ),
189215 new ArrayList <>(factoryCustomizers ),
190216 new ArrayList <>(workerCustomizers ),
217+ new ArrayList <>(workerInitializers ),
191218 new ArrayList <>(workerInterceptors ),
192219 new ArrayList <>(clientInterceptors ),
193220 new ArrayList <>(contextPropagators ));
@@ -199,6 +226,7 @@ private static final class SimplePlugin extends PluginBase {
199226 private final List <Consumer <WorkflowClientOptions .Builder >> clientCustomizers ;
200227 private final List <Consumer <WorkerFactoryOptions .Builder >> factoryCustomizers ;
201228 private final List <Consumer <WorkerOptions .Builder >> workerCustomizers ;
229+ private final List <BiConsumer <String , Worker >> workerInitializers ;
202230 private final List <WorkerInterceptor > workerInterceptors ;
203231 private final List <WorkflowClientInterceptor > clientInterceptors ;
204232 private final List <ContextPropagator > contextPropagators ;
@@ -209,6 +237,7 @@ private static final class SimplePlugin extends PluginBase {
209237 List <Consumer <WorkflowClientOptions .Builder >> clientCustomizers ,
210238 List <Consumer <WorkerFactoryOptions .Builder >> factoryCustomizers ,
211239 List <Consumer <WorkerOptions .Builder >> workerCustomizers ,
240+ List <BiConsumer <String , Worker >> workerInitializers ,
212241 List <WorkerInterceptor > workerInterceptors ,
213242 List <WorkflowClientInterceptor > clientInterceptors ,
214243 List <ContextPropagator > contextPropagators ) {
@@ -217,6 +246,7 @@ private static final class SimplePlugin extends PluginBase {
217246 this .clientCustomizers = clientCustomizers ;
218247 this .factoryCustomizers = factoryCustomizers ;
219248 this .workerCustomizers = workerCustomizers ;
249+ this .workerInitializers = workerInitializers ;
220250 this .workerInterceptors = workerInterceptors ;
221251 this .clientInterceptors = clientInterceptors ;
222252 this .contextPropagators = contextPropagators ;
@@ -292,5 +322,12 @@ public WorkerOptions.Builder configureWorker(
292322 }
293323 return builder ;
294324 }
325+
326+ @ Override
327+ public void initializeWorker (@ Nonnull String taskQueue , @ Nonnull Worker worker ) {
328+ for (BiConsumer <String , Worker > initializer : workerInitializers ) {
329+ initializer .accept (taskQueue , worker );
330+ }
331+ }
295332 }
296333}
0 commit comments