Skip to content

Commit 8fbb068

Browse files
Add interceptor support to springboot integration (#2500)
Add interceptor beans
1 parent 0e8ac35 commit 8fbb068

File tree

12 files changed

+228
-12
lines changed

12 files changed

+228
-12
lines changed

temporal-spring-boot-autoconfigure/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ Task Queue names directly for simplicity.
182182

183183
Note: Worker whose name is not explicitly specified is named after it's Task Queue.
184184

185+
## Interceptors
186+
187+
To enable interceptors users can create beans implementing the `io.temporal.common.interceptors.WorkflowClientInterceptor`
188+
, `io.temporal.common.interceptors.ScheduleClientInterceptor`, or `io.temporal.common.interceptors.WorkerInterceptor`
189+
interface. Interceptors will be registered in the order specified by the `@Order` annotation.
190+
185191
## Customization of `*Options`
186192

187193
To provide freedom in customization of `*Options` instances that are created by this module,
@@ -283,7 +289,8 @@ spring.temporal:
283289
## Customization
284290

285291
All customization points for the root namespace also exist for the non-root namespaces. To specify for a particular
286-
namespace users just need to append the alias/namespace to the bean.
292+
namespace users just need to append the alias/namespace to the bean. Currently, auto registered interceptors are not
293+
supported, but `WorkerFactoryOptions` can always be used to customize it per namespace.
287294

288295
```java
289296
// TemporalOptionsCustomizer type beans must start with the namespace/alias you defined and end with function class

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/AutoConfigurationUtils.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.google.common.base.MoreObjects;
44
import io.temporal.common.converter.DataConverter;
5+
import io.temporal.common.interceptors.ScheduleClientInterceptor;
6+
import io.temporal.common.interceptors.WorkerInterceptor;
7+
import io.temporal.common.interceptors.WorkflowClientInterceptor;
58
import io.temporal.spring.boot.TemporalOptionsCustomizer;
69
import io.temporal.spring.boot.autoconfigure.properties.NonRootNamespaceProperties;
710
import io.temporal.spring.boot.autoconfigure.properties.TemporalProperties;
@@ -17,7 +20,7 @@
1720
class AutoConfigurationUtils {
1821

1922
@Nullable
20-
static DataConverter choseDataConverter(
23+
static DataConverter chooseDataConverter(
2124
List<DataConverter> dataConverters, DataConverter mainDataConverter) {
2225
DataConverter chosenDataConverter = null;
2326
if (dataConverters.size() == 1) {
@@ -38,7 +41,7 @@ static DataConverter choseDataConverter(
3841
}
3942

4043
@Nullable
41-
static DataConverter choseDataConverter(
44+
static DataConverter chooseDataConverter(
4245
Map<String, DataConverter> dataConverters,
4346
DataConverter mainDataConverter,
4447
TemporalProperties properties) {
@@ -47,7 +50,7 @@ static DataConverter choseDataConverter(
4750
}
4851
List<NonRootNamespaceProperties> nonRootNamespaceProperties = properties.getNamespaces();
4952
if (Objects.isNull(nonRootNamespaceProperties) || nonRootNamespaceProperties.isEmpty()) {
50-
return choseDataConverter(new ArrayList<>(dataConverters.values()), mainDataConverter);
53+
return chooseDataConverter(new ArrayList<>(dataConverters.values()), mainDataConverter);
5154
} else {
5255
List<DataConverter> dataConverterList = new ArrayList<>();
5356
List<String> nonRootBeanNames =
@@ -69,10 +72,28 @@ static DataConverter choseDataConverter(
6972
}
7073
dataConverterList.add(dataConverter);
7174
}
72-
return choseDataConverter(dataConverterList, mainDataConverter);
75+
return chooseDataConverter(dataConverterList, mainDataConverter);
7376
}
7477
}
7578

79+
@Nullable
80+
static List<WorkflowClientInterceptor> chooseWorkflowClientInterceptors(
81+
List<WorkflowClientInterceptor> workflowClientInterceptors, TemporalProperties properties) {
82+
return workflowClientInterceptors;
83+
}
84+
85+
@Nullable
86+
static List<ScheduleClientInterceptor> chooseScheduleClientInterceptors(
87+
List<ScheduleClientInterceptor> scheduleClientInterceptor, TemporalProperties properties) {
88+
return scheduleClientInterceptor;
89+
}
90+
91+
@Nullable
92+
static List<WorkerInterceptor> chooseWorkerInterceptors(
93+
List<WorkerInterceptor> workerInterceptor, TemporalProperties properties) {
94+
return workerInterceptor;
95+
}
96+
7697
static <T> TemporalOptionsCustomizer<T> chooseTemporalCustomizerBean(
7798
Map<String, TemporalOptionsCustomizer<T>> customizerMap,
7899
Class<T> genericOptionsBuilderClass,

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/NonRootBeanPostProcessor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ private void injectBeanByNonRootNamespace(NonRootNamespaceProperties ns) {
109109
ns,
110110
workflowServiceStubs,
111111
dataConverterByNamespace,
112+
null, // Currently interceptors are not supported in non-root namespace
113+
null,
114+
null,
112115
tracer,
113116
testWorkflowEnvironment,
114117
workFactoryCustomizer,

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/RootNamespaceAutoConfiguration.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import io.temporal.client.schedules.ScheduleClient;
77
import io.temporal.client.schedules.ScheduleClientOptions;
88
import io.temporal.common.converter.DataConverter;
9+
import io.temporal.common.interceptors.ScheduleClientInterceptor;
10+
import io.temporal.common.interceptors.WorkerInterceptor;
11+
import io.temporal.common.interceptors.WorkflowClientInterceptor;
912
import io.temporal.serviceclient.WorkflowServiceStubs;
1013
import io.temporal.spring.boot.TemporalOptionsCustomizer;
1114
import io.temporal.spring.boot.autoconfigure.properties.TemporalProperties;
@@ -19,6 +22,7 @@
1922
import io.temporal.worker.WorkerOptions;
2023
import io.temporal.worker.WorkflowImplementationOptions;
2124
import java.util.Collection;
25+
import java.util.List;
2226
import java.util.Map;
2327
import javax.annotation.Nonnull;
2428
import javax.annotation.Nullable;
@@ -64,6 +68,11 @@ public NamespaceTemplate rootNamespaceTemplate(
6468
@Qualifier("mainDataConverter") @Autowired(required = false) @Nullable
6569
DataConverter mainDataConverter,
6670
@Autowired(required = false) @Nullable Tracer otTracer,
71+
@Autowired(required = false) @Nullable
72+
List<WorkflowClientInterceptor> workflowClientInterceptors,
73+
@Autowired(required = false) @Nullable
74+
List<ScheduleClientInterceptor> scheduleClientInterceptors,
75+
@Autowired(required = false) @Nullable List<WorkerInterceptor> workerInterceptors,
6776
@Qualifier("temporalTestWorkflowEnvironmentAdapter") @Autowired(required = false) @Nullable
6877
TestWorkflowEnvironmentAdapter testWorkflowEnvironment,
6978
@Autowired(required = false) @Nullable
@@ -80,7 +89,15 @@ public NamespaceTemplate rootNamespaceTemplate(
8089
Map<String, TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder>>
8190
workflowImplementationCustomizerMap) {
8291
DataConverter chosenDataConverter =
83-
AutoConfigurationUtils.choseDataConverter(dataConverters, mainDataConverter, properties);
92+
AutoConfigurationUtils.chooseDataConverter(dataConverters, mainDataConverter, properties);
93+
List<WorkflowClientInterceptor> chosenClientInterceptors =
94+
AutoConfigurationUtils.chooseWorkflowClientInterceptors(
95+
workflowClientInterceptors, properties);
96+
List<ScheduleClientInterceptor> chosenScheduleClientInterceptors =
97+
AutoConfigurationUtils.chooseScheduleClientInterceptors(
98+
scheduleClientInterceptors, properties);
99+
List<WorkerInterceptor> chosenWorkerInterceptors =
100+
AutoConfigurationUtils.chooseWorkerInterceptors(workerInterceptors, properties);
84101
TemporalOptionsCustomizer<WorkerFactoryOptions.Builder> workerFactoryCustomizer =
85102
AutoConfigurationUtils.chooseTemporalCustomizerBean(
86103
workerFactoryCustomizerMap, WorkerFactoryOptions.Builder.class, properties);
@@ -104,6 +121,9 @@ public NamespaceTemplate rootNamespaceTemplate(
104121
properties,
105122
workflowServiceStubs,
106123
chosenDataConverter,
124+
chosenClientInterceptors,
125+
chosenScheduleClientInterceptors,
126+
chosenWorkerInterceptors,
107127
otTracer,
108128
testWorkflowEnvironment,
109129
workerFactoryCustomizer,

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/TestServerAutoConfiguration.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import io.temporal.client.WorkflowClientOptions;
66
import io.temporal.client.schedules.ScheduleClientOptions;
77
import io.temporal.common.converter.DataConverter;
8+
import io.temporal.common.interceptors.ScheduleClientInterceptor;
9+
import io.temporal.common.interceptors.WorkerInterceptor;
10+
import io.temporal.common.interceptors.WorkflowClientInterceptor;
811
import io.temporal.spring.boot.TemporalOptionsCustomizer;
912
import io.temporal.spring.boot.autoconfigure.properties.TemporalProperties;
1013
import io.temporal.spring.boot.autoconfigure.template.TestWorkflowEnvironmentAdapter;
@@ -13,6 +16,7 @@
1316
import io.temporal.testing.TestEnvironmentOptions;
1417
import io.temporal.testing.TestWorkflowEnvironment;
1518
import io.temporal.worker.WorkerFactoryOptions;
19+
import java.util.List;
1620
import java.util.Map;
1721
import javax.annotation.Nullable;
1822
import org.slf4j.Logger;
@@ -54,6 +58,11 @@ public TestWorkflowEnvironment testWorkflowEnvironment(
5458
@Qualifier("mainDataConverter") @Autowired(required = false) @Nullable
5559
DataConverter mainDataConverter,
5660
@Autowired(required = false) @Nullable Tracer otTracer,
61+
@Autowired(required = false) @Nullable
62+
List<WorkflowClientInterceptor> workflowClientInterceptors,
63+
@Autowired(required = false) @Nullable
64+
List<ScheduleClientInterceptor> scheduleClientInterceptors,
65+
@Autowired(required = false) @Nullable List<WorkerInterceptor> workerInterceptors,
5766
@Autowired(required = false) @Nullable
5867
TemporalOptionsCustomizer<TestEnvironmentOptions.Builder> testEnvOptionsCustomizer,
5968
@Autowired(required = false) @Nullable
@@ -65,7 +74,15 @@ public TestWorkflowEnvironment testWorkflowEnvironment(
6574
Map<String, TemporalOptionsCustomizer<ScheduleClientOptions.Builder>>
6675
scheduleCustomizerMap) {
6776
DataConverter chosenDataConverter =
68-
AutoConfigurationUtils.choseDataConverter(dataConverters, mainDataConverter, properties);
77+
AutoConfigurationUtils.chooseDataConverter(dataConverters, mainDataConverter, properties);
78+
List<WorkflowClientInterceptor> chosenClientInterceptors =
79+
AutoConfigurationUtils.chooseWorkflowClientInterceptors(
80+
workflowClientInterceptors, properties);
81+
List<ScheduleClientInterceptor> chosenScheduleClientInterceptors =
82+
AutoConfigurationUtils.chooseScheduleClientInterceptors(
83+
scheduleClientInterceptors, properties);
84+
List<WorkerInterceptor> chosenWorkerInterceptors =
85+
AutoConfigurationUtils.chooseWorkerInterceptors(workerInterceptors, properties);
6986

7087
TemporalOptionsCustomizer<WorkerFactoryOptions.Builder> workerFactoryCustomizer =
7188
AutoConfigurationUtils.chooseTemporalCustomizerBean(
@@ -83,6 +100,8 @@ public TestWorkflowEnvironment testWorkflowEnvironment(
83100
new WorkflowClientOptionsTemplate(
84101
properties.getNamespace(),
85102
chosenDataConverter,
103+
chosenClientInterceptors,
104+
chosenScheduleClientInterceptors,
86105
otTracer,
87106
clientCustomizer,
88107
scheduleCustomizer)
@@ -93,7 +112,8 @@ public TestWorkflowEnvironment testWorkflowEnvironment(
93112
}
94113

95114
options.setWorkerFactoryOptions(
96-
new WorkerFactoryOptionsTemplate(properties, otTracer, workerFactoryCustomizer)
115+
new WorkerFactoryOptionsTemplate(
116+
properties, chosenWorkerInterceptors, otTracer, workerFactoryCustomizer)
97117
.createWorkerFactoryOptions());
98118

99119
if (testEnvOptionsCustomizer != null) {

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/template/ClientTemplate.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import io.temporal.client.schedules.ScheduleClient;
88
import io.temporal.client.schedules.ScheduleClientOptions;
99
import io.temporal.common.converter.DataConverter;
10+
import io.temporal.common.interceptors.ScheduleClientInterceptor;
11+
import io.temporal.common.interceptors.WorkflowClientInterceptor;
1012
import io.temporal.serviceclient.WorkflowServiceStubs;
1113
import io.temporal.spring.boot.TemporalOptionsCustomizer;
14+
import java.util.List;
1215
import javax.annotation.Nonnull;
1316
import javax.annotation.Nullable;
1417

@@ -24,14 +27,22 @@ public class ClientTemplate {
2427
public ClientTemplate(
2528
@Nonnull String namespace,
2629
@Nullable DataConverter dataConverter,
30+
@Nullable List<WorkflowClientInterceptor> workflowClientInterceptors,
31+
@Nullable List<ScheduleClientInterceptor> scheduleClientInterceptors,
2732
@Nullable Tracer tracer,
2833
@Nullable WorkflowServiceStubs workflowServiceStubs,
2934
@Nullable TestWorkflowEnvironmentAdapter testWorkflowEnvironment,
3035
@Nullable TemporalOptionsCustomizer<WorkflowClientOptions.Builder> clientCustomizer,
3136
@Nullable TemporalOptionsCustomizer<ScheduleClientOptions.Builder> scheduleCustomer) {
3237
this.optionsTemplate =
3338
new WorkflowClientOptionsTemplate(
34-
namespace, dataConverter, tracer, clientCustomizer, scheduleCustomer);
39+
namespace,
40+
dataConverter,
41+
workflowClientInterceptors,
42+
scheduleClientInterceptors,
43+
tracer,
44+
clientCustomizer,
45+
scheduleCustomer);
3546
this.workflowServiceStubs = workflowServiceStubs;
3647
this.testWorkflowEnvironment = testWorkflowEnvironment;
3748
}

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/template/NamespaceTemplate.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,26 @@
44
import io.temporal.client.WorkflowClientOptions;
55
import io.temporal.client.schedules.ScheduleClientOptions;
66
import io.temporal.common.converter.DataConverter;
7+
import io.temporal.common.interceptors.ScheduleClientInterceptor;
8+
import io.temporal.common.interceptors.WorkerInterceptor;
9+
import io.temporal.common.interceptors.WorkflowClientInterceptor;
710
import io.temporal.serviceclient.WorkflowServiceStubs;
811
import io.temporal.spring.boot.TemporalOptionsCustomizer;
912
import io.temporal.spring.boot.autoconfigure.properties.NamespaceProperties;
1013
import io.temporal.worker.WorkerFactoryOptions;
1114
import io.temporal.worker.WorkerOptions;
1215
import io.temporal.worker.WorkflowImplementationOptions;
16+
import java.util.List;
1317
import javax.annotation.Nonnull;
1418
import javax.annotation.Nullable;
1519

1620
public class NamespaceTemplate {
1721
private final @Nonnull NamespaceProperties namespaceProperties;
1822
private final @Nonnull WorkflowServiceStubs workflowServiceStubs;
1923
private final @Nullable DataConverter dataConverter;
24+
private final @Nullable List<WorkflowClientInterceptor> workflowClientInterceptors;
25+
private final @Nullable List<ScheduleClientInterceptor> scheduleClientInterceptors;
26+
private final @Nullable List<WorkerInterceptor> workerInterceptors;
2027
private final @Nullable Tracer tracer;
2128
private final @Nullable TestWorkflowEnvironmentAdapter testWorkflowEnvironment;
2229

@@ -36,6 +43,9 @@ public NamespaceTemplate(
3643
@Nonnull NamespaceProperties namespaceProperties,
3744
@Nonnull WorkflowServiceStubs workflowServiceStubs,
3845
@Nullable DataConverter dataConverter,
46+
@Nullable List<WorkflowClientInterceptor> workflowClientInterceptors,
47+
@Nullable List<ScheduleClientInterceptor> scheduleClientInterceptors,
48+
@Nullable List<WorkerInterceptor> workerInterceptors,
3949
@Nullable Tracer tracer,
4050
@Nullable TestWorkflowEnvironmentAdapter testWorkflowEnvironment,
4151
@Nullable TemporalOptionsCustomizer<WorkerFactoryOptions.Builder> workerFactoryCustomizer,
@@ -48,6 +58,9 @@ public NamespaceTemplate(
4858
this.namespaceProperties = namespaceProperties;
4959
this.workflowServiceStubs = workflowServiceStubs;
5060
this.dataConverter = dataConverter;
61+
this.workflowClientInterceptors = workflowClientInterceptors;
62+
this.scheduleClientInterceptors = scheduleClientInterceptors;
63+
this.workerInterceptors = workerInterceptors;
5164
this.tracer = tracer;
5265
this.testWorkflowEnvironment = testWorkflowEnvironment;
5366

@@ -64,6 +77,8 @@ public ClientTemplate getClientTemplate() {
6477
new ClientTemplate(
6578
namespaceProperties.getNamespace(),
6679
dataConverter,
80+
workflowClientInterceptors,
81+
scheduleClientInterceptors,
6782
tracer,
6883
workflowServiceStubs,
6984
testWorkflowEnvironment,
@@ -79,6 +94,7 @@ public WorkersTemplate getWorkersTemplate() {
7994
new WorkersTemplate(
8095
namespaceProperties,
8196
getClientTemplate(),
97+
workerInterceptors,
8298
tracer,
8399
testWorkflowEnvironment,
84100
workerFactoryCustomizer,

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/template/NonRootNamespaceTemplate.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
import io.temporal.client.WorkflowClientOptions;
55
import io.temporal.client.schedules.ScheduleClientOptions;
66
import io.temporal.common.converter.DataConverter;
7+
import io.temporal.common.interceptors.ScheduleClientInterceptor;
8+
import io.temporal.common.interceptors.WorkerInterceptor;
9+
import io.temporal.common.interceptors.WorkflowClientInterceptor;
710
import io.temporal.serviceclient.WorkflowServiceStubs;
811
import io.temporal.spring.boot.TemporalOptionsCustomizer;
912
import io.temporal.spring.boot.autoconfigure.properties.NonRootNamespaceProperties;
1013
import io.temporal.worker.WorkerFactoryOptions;
1114
import io.temporal.worker.WorkerOptions;
1215
import io.temporal.worker.WorkflowImplementationOptions;
16+
import java.util.List;
1317
import javax.annotation.Nonnull;
1418
import javax.annotation.Nullable;
1519
import org.springframework.beans.factory.BeanFactory;
@@ -24,6 +28,9 @@ public NonRootNamespaceTemplate(
2428
@Nonnull NonRootNamespaceProperties namespaceProperties,
2529
@Nonnull WorkflowServiceStubs workflowServiceStubs,
2630
@Nullable DataConverter dataConverter,
31+
@Nullable List<WorkflowClientInterceptor> workflowClientInterceptors,
32+
@Nullable List<ScheduleClientInterceptor> scheduleClientInterceptors,
33+
@Nullable List<WorkerInterceptor> workerInterceptors,
2734
@Nullable Tracer tracer,
2835
@Nullable TestWorkflowEnvironmentAdapter testWorkflowEnvironment,
2936
@Nullable TemporalOptionsCustomizer<WorkerFactoryOptions.Builder> workerFactoryCustomizer,
@@ -37,6 +44,9 @@ public NonRootNamespaceTemplate(
3744
namespaceProperties,
3845
workflowServiceStubs,
3946
dataConverter,
47+
workflowClientInterceptors,
48+
scheduleClientInterceptors,
49+
workerInterceptors,
4050
tracer,
4151
testWorkflowEnvironment,
4252
workerFactoryCustomizer,

0 commit comments

Comments
 (0)