Skip to content

Commit 2f20274

Browse files
Try removing the options customizers, and adding a DataConverter customizer
1 parent 7331b7f commit 2f20274

3 files changed

Lines changed: 175 additions & 284 deletions

File tree

temporal-sdk/src/main/java/io/temporal/common/SimplePlugin.java

Lines changed: 39 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.function.BiConsumer;
4545
import java.util.function.Consumer;
4646
import java.util.function.Supplier;
47+
import java.util.function.UnaryOperator;
4748
import javax.annotation.Nonnull;
4849

4950
/**
@@ -62,7 +63,7 @@
6263
* SimplePlugin myPlugin = SimplePlugin.newBuilder("my-plugin")
6364
* .addWorkerInterceptors(new TracingInterceptor())
6465
* .addClientInterceptors(new LoggingInterceptor())
65-
* .customizeWorkflowClient(b -> b.setIdentity("custom-identity"))
66+
* .customizeDataConverter(existing -> new CodecDataConverter(existing, myCodec))
6667
* .build();
6768
* }</pre>
6869
*
@@ -113,11 +114,6 @@ public abstract class SimplePlugin
113114
WorkerPlugin {
114115

115116
private final String name;
116-
private final List<Consumer<WorkflowServiceStubsOptions.Builder>> stubsCustomizers;
117-
private final List<Consumer<WorkflowClientOptions.Builder>> workflowClientCustomizers;
118-
private final List<Consumer<ScheduleClientOptions.Builder>> scheduleCustomizers;
119-
private final List<Consumer<WorkerFactoryOptions.Builder>> factoryCustomizers;
120-
private final List<Consumer<WorkerOptions.Builder>> workerCustomizers;
121117
private final List<BiConsumer<String, Worker>> workerInitializers;
122118
private final List<BiConsumer<String, Worker>> workerStartCallbacks;
123119
private final List<BiConsumer<String, Worker>> workerShutdownCallbacks;
@@ -127,7 +123,7 @@ public abstract class SimplePlugin
127123
private final List<WorkerInterceptor> workerInterceptors;
128124
private final List<WorkflowClientInterceptor> clientInterceptors;
129125
private final List<ContextPropagator> contextPropagators;
130-
private final DataConverter dataConverter;
126+
private final UnaryOperator<DataConverter> dataConverterCustomizer;
131127
private final List<Class<?>> workflowImplementationTypes;
132128
private final List<Object> activitiesImplementations;
133129
private final List<Object> nexusServiceImplementations;
@@ -142,11 +138,6 @@ public abstract class SimplePlugin
142138
*/
143139
protected SimplePlugin(@Nonnull String name) {
144140
this.name = Objects.requireNonNull(name, "Plugin name cannot be null");
145-
this.stubsCustomizers = Collections.emptyList();
146-
this.workflowClientCustomizers = Collections.emptyList();
147-
this.scheduleCustomizers = Collections.emptyList();
148-
this.factoryCustomizers = Collections.emptyList();
149-
this.workerCustomizers = Collections.emptyList();
150141
this.workerInitializers = Collections.emptyList();
151142
this.workerStartCallbacks = Collections.emptyList();
152143
this.workerShutdownCallbacks = Collections.emptyList();
@@ -156,7 +147,7 @@ protected SimplePlugin(@Nonnull String name) {
156147
this.workerInterceptors = Collections.emptyList();
157148
this.clientInterceptors = Collections.emptyList();
158149
this.contextPropagators = Collections.emptyList();
159-
this.dataConverter = null;
150+
this.dataConverterCustomizer = null;
160151
this.workflowImplementationTypes = Collections.emptyList();
161152
this.activitiesImplementations = Collections.emptyList();
162153
this.nexusServiceImplementations = Collections.emptyList();
@@ -172,11 +163,6 @@ protected SimplePlugin(@Nonnull String name) {
172163
protected SimplePlugin(@Nonnull Builder builder) {
173164
Objects.requireNonNull(builder, "Builder cannot be null");
174165
this.name = builder.name;
175-
this.stubsCustomizers = new ArrayList<>(builder.stubsCustomizers);
176-
this.workflowClientCustomizers = new ArrayList<>(builder.workflowClientCustomizers);
177-
this.scheduleCustomizers = new ArrayList<>(builder.scheduleCustomizers);
178-
this.factoryCustomizers = new ArrayList<>(builder.factoryCustomizers);
179-
this.workerCustomizers = new ArrayList<>(builder.workerCustomizers);
180166
this.workerInitializers = new ArrayList<>(builder.workerInitializers);
181167
this.workerStartCallbacks = new ArrayList<>(builder.workerStartCallbacks);
182168
this.workerShutdownCallbacks = new ArrayList<>(builder.workerShutdownCallbacks);
@@ -186,7 +172,7 @@ protected SimplePlugin(@Nonnull Builder builder) {
186172
this.workerInterceptors = new ArrayList<>(builder.workerInterceptors);
187173
this.clientInterceptors = new ArrayList<>(builder.clientInterceptors);
188174
this.contextPropagators = new ArrayList<>(builder.contextPropagators);
189-
this.dataConverter = builder.dataConverter;
175+
this.dataConverterCustomizer = builder.dataConverterCustomizer;
190176
this.workflowImplementationTypes = new ArrayList<>(builder.workflowImplementationTypes);
191177
this.activitiesImplementations = new ArrayList<>(builder.activitiesImplementations);
192178
this.nexusServiceImplementations = new ArrayList<>(builder.nexusServiceImplementations);
@@ -211,21 +197,15 @@ public String getName() {
211197

212198
@Override
213199
public void configureServiceStubs(@Nonnull WorkflowServiceStubsOptions.Builder builder) {
214-
for (Consumer<WorkflowServiceStubsOptions.Builder> customizer : stubsCustomizers) {
215-
customizer.accept(builder);
216-
}
200+
// Subclasses can override this method for custom configuration
217201
}
218202

219203
@Override
220204
public void configureWorkflowClient(@Nonnull WorkflowClientOptions.Builder builder) {
221-
// Apply customizers
222-
for (Consumer<WorkflowClientOptions.Builder> customizer : workflowClientCustomizers) {
223-
customizer.accept(builder);
224-
}
225-
226-
// Set data converter
227-
if (dataConverter != null) {
228-
builder.setDataConverter(dataConverter);
205+
// Apply data converter customizer
206+
if (dataConverterCustomizer != null) {
207+
DataConverter existing = builder.build().getDataConverter();
208+
builder.setDataConverter(dataConverterCustomizer.apply(existing));
229209
}
230210

231211
// Add client interceptors
@@ -249,19 +229,11 @@ public void configureWorkflowClient(@Nonnull WorkflowClientOptions.Builder build
249229

250230
@Override
251231
public void configureScheduleClient(@Nonnull ScheduleClientOptions.Builder builder) {
252-
// Apply customizers
253-
for (Consumer<ScheduleClientOptions.Builder> customizer : scheduleCustomizers) {
254-
customizer.accept(builder);
255-
}
232+
// Subclasses can override this method for custom configuration
256233
}
257234

258235
@Override
259236
public void configureWorkerFactory(@Nonnull WorkerFactoryOptions.Builder builder) {
260-
// Apply customizers
261-
for (Consumer<WorkerFactoryOptions.Builder> customizer : factoryCustomizers) {
262-
customizer.accept(builder);
263-
}
264-
265237
// Add worker interceptors
266238
if (!workerInterceptors.isEmpty()) {
267239
WorkerInterceptor[] existing = builder.build().getWorkerInterceptors();
@@ -274,9 +246,7 @@ public void configureWorkerFactory(@Nonnull WorkerFactoryOptions.Builder builder
274246

275247
@Override
276248
public void configureWorker(@Nonnull String taskQueue, @Nonnull WorkerOptions.Builder builder) {
277-
for (Consumer<WorkerOptions.Builder> customizer : workerCustomizers) {
278-
customizer.accept(builder);
279-
}
249+
// Subclasses can override this method for custom configuration
280250
}
281251

282252
@Override
@@ -364,15 +334,6 @@ public String toString() {
364334
public static final class Builder {
365335

366336
private final String name;
367-
private final List<Consumer<WorkflowServiceStubsOptions.Builder>> stubsCustomizers =
368-
new ArrayList<>();
369-
private final List<Consumer<WorkflowClientOptions.Builder>> workflowClientCustomizers =
370-
new ArrayList<>();
371-
private final List<Consumer<ScheduleClientOptions.Builder>> scheduleCustomizers =
372-
new ArrayList<>();
373-
private final List<Consumer<WorkerFactoryOptions.Builder>> factoryCustomizers =
374-
new ArrayList<>();
375-
private final List<Consumer<WorkerOptions.Builder>> workerCustomizers = new ArrayList<>();
376337
private final List<BiConsumer<String, Worker>> workerInitializers = new ArrayList<>();
377338
private final List<BiConsumer<String, Worker>> workerStartCallbacks = new ArrayList<>();
378339
private final List<BiConsumer<String, Worker>> workerShutdownCallbacks = new ArrayList<>();
@@ -383,7 +344,7 @@ public static final class Builder {
383344
private final List<WorkerInterceptor> workerInterceptors = new ArrayList<>();
384345
private final List<WorkflowClientInterceptor> clientInterceptors = new ArrayList<>();
385346
private final List<ContextPropagator> contextPropagators = new ArrayList<>();
386-
private DataConverter dataConverter;
347+
private UnaryOperator<DataConverter> dataConverterCustomizer;
387348
private final List<Class<?>> workflowImplementationTypes = new ArrayList<>();
388349
private final List<Object> activitiesImplementations = new ArrayList<>();
389350
private final List<Object> nexusServiceImplementations = new ArrayList<>();
@@ -392,70 +353,6 @@ private Builder(@Nonnull String name) {
392353
this.name = Objects.requireNonNull(name, "Plugin name cannot be null");
393354
}
394355

395-
/**
396-
* Adds a customizer for {@link WorkflowServiceStubsOptions}. Multiple customizers are applied
397-
* in the order they are added.
398-
*
399-
* @param customizer a consumer that modifies the options builder
400-
* @return this builder for chaining
401-
*/
402-
public Builder customizeServiceStubs(
403-
@Nonnull Consumer<WorkflowServiceStubsOptions.Builder> customizer) {
404-
stubsCustomizers.add(Objects.requireNonNull(customizer));
405-
return this;
406-
}
407-
408-
/**
409-
* Adds a customizer for {@link WorkflowClientOptions}. Multiple customizers are applied in the
410-
* order they are added.
411-
*
412-
* @param customizer a consumer that modifies the options builder
413-
* @return this builder for chaining
414-
*/
415-
public Builder customizeWorkflowClient(
416-
@Nonnull Consumer<WorkflowClientOptions.Builder> customizer) {
417-
workflowClientCustomizers.add(Objects.requireNonNull(customizer));
418-
return this;
419-
}
420-
421-
/**
422-
* Adds a customizer for {@link ScheduleClientOptions}. Multiple customizers are applied in the
423-
* order they are added.
424-
*
425-
* @param customizer a consumer that modifies the options builder
426-
* @return this builder for chaining
427-
*/
428-
public Builder customizeScheduleClient(
429-
@Nonnull Consumer<ScheduleClientOptions.Builder> customizer) {
430-
scheduleCustomizers.add(Objects.requireNonNull(customizer));
431-
return this;
432-
}
433-
434-
/**
435-
* Adds a customizer for {@link WorkerFactoryOptions}. Multiple customizers are applied in the
436-
* order they are added.
437-
*
438-
* @param customizer a consumer that modifies the options builder
439-
* @return this builder for chaining
440-
*/
441-
public Builder customizeWorkerFactory(
442-
@Nonnull Consumer<WorkerFactoryOptions.Builder> customizer) {
443-
factoryCustomizers.add(Objects.requireNonNull(customizer));
444-
return this;
445-
}
446-
447-
/**
448-
* Adds a customizer for {@link WorkerOptions}. Multiple customizers are applied in the order
449-
* they are added. The customizer is applied to all workers created by the factory.
450-
*
451-
* @param customizer a consumer that modifies the options builder
452-
* @return this builder for chaining
453-
*/
454-
public Builder customizeWorker(@Nonnull Consumer<WorkerOptions.Builder> customizer) {
455-
workerCustomizers.add(Objects.requireNonNull(customizer));
456-
return this;
457-
}
458-
459356
/**
460357
* Adds an initializer that is called after a worker is created. This can be used to register
461358
* workflows, activities, and Nexus services on the worker.
@@ -641,14 +538,35 @@ public Builder addContextPropagators(ContextPropagator... propagators) {
641538
}
642539

643540
/**
644-
* Sets the data converter to use for serializing workflow and activity arguments and results.
645-
* This overrides any data converter previously set on the client options.
541+
* Sets a customizer for the data converter. The customizer receives the existing data converter
542+
* and returns a new or modified data converter.
543+
*
544+
* <p>This is useful for wrapping the existing data converter with additional functionality,
545+
* such as adding a codec for encryption or compression.
546+
*
547+
* <p>Example - wrapping with a codec:
548+
*
549+
* <pre>{@code
550+
* SimplePlugin.newBuilder("my-plugin")
551+
* .customizeDataConverter(existing ->
552+
* new CodecDataConverter(existing, Collections.singletonList(new MyCodec())))
553+
* .build();
554+
* }</pre>
555+
*
556+
* <p>Example - replacing entirely:
557+
*
558+
* <pre>{@code
559+
* SimplePlugin.newBuilder("my-plugin")
560+
* .customizeDataConverter(existing -> myCustomDataConverter)
561+
* .build();
562+
* }</pre>
646563
*
647-
* @param dataConverter the data converter to use
564+
* @param customizer a function that receives the existing data converter and returns the data
565+
* converter to use
648566
* @return this builder for chaining
649567
*/
650-
public Builder setDataConverter(@Nonnull DataConverter dataConverter) {
651-
this.dataConverter = Objects.requireNonNull(dataConverter);
568+
public Builder customizeDataConverter(@Nonnull UnaryOperator<DataConverter> customizer) {
569+
this.dataConverterCustomizer = Objects.requireNonNull(customizer);
652570
return this;
653571
}
654572

0 commit comments

Comments
 (0)