Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ to docs, or any other relevant information.
with `TEMPORAL_EXT_AWS_SECRETS_MANAGER_*` environment variables or in the TOML config.
- Spring Boot now auto-configures `ActivityClient` for root and non-root namespaces, with namespace-specific
data converters, options customization, and root activity client interceptor registration.
- Spring Boot now provides singleton `NexusClient` beans for the root and configured non-root namespaces,
with namespace-specific data converters and `TemporalOptionsCustomizer<NexusClientOptions.Builder>`
customization. `NexusClientInterceptor` beans are automatically registered for the root client.
- **Experimental**: External storage in clients, activity workers, and Nexus workers can now read references that
were encoded with the configured payload codec and marked with `__temporal_external_storage_reference` metadata.

Expand Down
26 changes: 26 additions & 0 deletions temporal-spring-boot-autoconfigure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ client interceptors. Set non-root interceptors through that namespace's activity

Standalone activities are not supported by the test server.

# NexusClient
Comment thread
Quinn-With-Two-Ns marked this conversation as resolved.
Resolved

Spring Boot auto-configures a primary `NexusClient` bean named `temporalNexusClient` for the root
namespace unless the application provides its own `NexusClient` bean. Each configured non-root
namespace gets a bean named `<alias-or-namespace>NexusClient`: use the alias if configured, otherwise
use the namespace name. For example, a namespace named `applications` with the alias `apps` uses
`@Qualifier("appsNexusClient")`; without the alias, use `@Qualifier("applicationsNexusClient")`.
Each auto-configured client is a singleton that uses its namespace's connection and data converter.
If you provide your own client alongside non-root clients, use a qualifier or mark your client
`@Primary` to disambiguate injection.

```java
@Bean
TemporalOptionsCustomizer<NexusClientOptions.Builder> nexusClientCustomizer() {
return builder -> builder.setIdentity("my-application");
}
```

For non-root customization, name the bean `<alias-or-namespace>NexusClientCustomizer` using the
same prefix as the client bean.
`NexusClientInterceptor` beans apply to the root client only; configure non-root interceptors
through the namespace's options customizer.

The Temporal test server does not support standalone Nexus operations (SANO). Use a Temporal
Service that supports SANO to test operation execution.

# Running Multiple Name Space (experimental)

Along with the root namespace, you can configure multiple non-root namespaces in the application.yml file. Different namespaces can have different configurations including but not limited to different connection options, registered workflows/activities, data converters etc.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*
* @param <T> Temporal Options Builder to customize. Respected types: {@link
* WorkflowServiceStubsOptions.Builder}, {@link WorkflowClientOptions.Builder}, {@link
* TestEnvironmentOptions.Builder}, {@link WorkerOptions.Builder}, {@link
* WorkerFactoryOptions.Builder}, {@link WorkflowImplementationOptions.Builder}
* io.temporal.client.NexusClientOptions.Builder}, {@link TestEnvironmentOptions.Builder},
* {@link WorkerOptions.Builder}, {@link WorkerFactoryOptions.Builder}, {@link
* WorkflowImplementationOptions.Builder}
* @see WorkerOptionsCustomizer to get access to a name and task queue of a worker that's being
* customized
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.opentracing.Tracer;
import io.temporal.client.ActivityClient;
import io.temporal.client.ActivityClientOptions;
import io.temporal.client.NexusClient;
import io.temporal.client.NexusClientOptions;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowClientOptions;
import io.temporal.client.WorkflowClientPlugin;
Expand Down Expand Up @@ -33,6 +35,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -110,7 +113,8 @@ public Object postProcessAfterInitialization(@Nonnull Object bean, @Nonnull Stri
}

private void injectBeanByNonRootNamespace(NonRootNamespaceProperties ns) {
String beanPrefix = MoreObjects.firstNonNull(ns.getAlias(), ns.getNamespace());
String namespace = ns.getNamespace();
String beanPrefix = MoreObjects.firstNonNull(ns.getAlias(), namespace);
DataConverter dataConverterByNamespace = findBeanByNamespace(beanPrefix, DataConverter.class);

// found regarding namespace customizer bean, it can be optional
Expand All @@ -123,21 +127,25 @@ private void injectBeanByNonRootNamespace(NonRootNamespaceProperties ns) {
List<TemporalOptionsCustomizer<WorkerOptions.Builder>> workerCustomizers =
findBeanByNameSpaceForTemporalCustomizer(beanPrefix, WorkerOptions.Builder.class);
List<TemporalOptionsCustomizer<WorkflowClientOptions.Builder>> workflowClientCustomizers =
findBeanByNameSpaceForTemporalCustomizer(beanPrefix, WorkflowClientOptions.Builder.class);
if (workflowClientCustomizers != null) {
workflowClientCustomizers =
workflowClientCustomizers.stream()
.map(
c ->
(TemporalOptionsCustomizer<WorkflowClientOptions.Builder>)
(WorkflowClientOptions.Builder o) ->
c.customize(o).setNamespace(ns.getNamespace()))
.collect(Collectors.toList());
}
findNamespaceBoundClientCustomizers(
beanPrefix,
namespace,
WorkflowClientOptions.Builder.class,
WorkflowClientOptions.Builder::setNamespace);
List<TemporalOptionsCustomizer<ScheduleClientOptions.Builder>> scheduleClientCustomizers =
findBeanByNameSpaceForTemporalCustomizer(beanPrefix, ScheduleClientOptions.Builder.class);
List<TemporalOptionsCustomizer<ActivityClientOptions.Builder>> activityClientCustomizers =
findBeanByNameSpaceForTemporalCustomizer(beanPrefix, ActivityClientOptions.Builder.class);
findNamespaceBoundClientCustomizers(
beanPrefix,
namespace,
ActivityClientOptions.Builder.class,
ActivityClientOptions.Builder::setNamespace);
List<TemporalOptionsCustomizer<NexusClientOptions.Builder>> nexusClientCustomizers =
findNamespaceBoundClientCustomizers(
beanPrefix,
namespace,
NexusClientOptions.Builder.class,
NexusClientOptions.Builder::setNamespace);
List<TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder>>
workflowImplementationCustomizers =
findBeanByNameSpaceForTemporalCustomizer(
Expand Down Expand Up @@ -175,12 +183,15 @@ private void injectBeanByNonRootNamespace(NonRootNamespaceProperties ns) {
scheduleClientPlugins,
workerPlugins,
null,
activityClientCustomizers);
activityClientCustomizers,
null,
nexusClientCustomizers);

ClientTemplate clientTemplate = namespaceTemplate.getClientTemplate();
WorkflowClient workflowClient = clientTemplate.getWorkflowClient();
ScheduleClient scheduleClient = clientTemplate.getScheduleClient();
ActivityClient activityClient = clientTemplate.getActivityClient();
NexusClient nexusClient = clientTemplate.getNexusClient();
WorkersTemplate workersTemplate = namespaceTemplate.getWorkersTemplate();
WorkerFactory workerFactory = workersTemplate.getWorkerFactory();

Expand All @@ -201,6 +212,7 @@ private void injectBeanByNonRootNamespace(NonRootNamespaceProperties ns) {
beanPrefix + ScheduleClient.class.getSimpleName(), scheduleClient);
beanFactory.registerSingleton(
beanPrefix + ActivityClient.class.getSimpleName(), activityClient);
beanFactory.registerSingleton(beanPrefix + NexusClient.class.getSimpleName(), nexusClient);
beanFactory.registerSingleton(beanPrefix + WorkerFactory.class.getSimpleName(), workerFactory);
}

Expand Down Expand Up @@ -246,6 +258,24 @@ private <T> T findBeanByNamespace(String beanPrefix, Class<T> clazz) {
return null;
}

private <T> @Nullable List<TemporalOptionsCustomizer<T>> findNamespaceBoundClientCustomizers(
String beanPrefix,
String namespace,
Class<T> builderClass,
BiFunction<T, String, T> setNamespace) {
List<TemporalOptionsCustomizer<T>> customizers =
findBeanByNameSpaceForTemporalCustomizer(beanPrefix, builderClass);
if (customizers == null) {
return null;
}

// A customizer can return a new builder that no longer has the configured namespace.
return customizers.stream()
.<TemporalOptionsCustomizer<T>>map(
customizer -> options -> setNamespace.apply(customizer.customize(options), namespace))
.collect(Collectors.toList());
}

private <T> List<TemporalOptionsCustomizer<T>> findBeanByNameSpaceForTemporalCustomizer(
String beanPrefix, Class<T> genericOptionsBuilderClass) {
String beanName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.opentracing.Tracer;
import io.temporal.client.ActivityClient;
import io.temporal.client.ActivityClientOptions;
import io.temporal.client.NexusClient;
import io.temporal.client.NexusClientOptions;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowClientOptions;
import io.temporal.client.WorkflowClientPlugin;
Expand All @@ -11,6 +13,7 @@
import io.temporal.client.schedules.ScheduleClientPlugin;
import io.temporal.common.converter.DataConverter;
import io.temporal.common.interceptors.ActivityClientInterceptor;
import io.temporal.common.interceptors.NexusClientInterceptor;
import io.temporal.common.interceptors.ScheduleClientInterceptor;
import io.temporal.common.interceptors.WorkerInterceptor;
import io.temporal.common.interceptors.WorkflowClientInterceptor;
Expand Down Expand Up @@ -102,7 +105,11 @@ public NamespaceTemplate rootNamespaceTemplate(
workflowImplementationCustomizerMap,
@Autowired(required = false) @Nullable List<WorkflowClientPlugin> workflowClientPlugins,
@Autowired(required = false) @Nullable List<ScheduleClientPlugin> scheduleClientPlugins,
@Autowired(required = false) @Nullable List<WorkerPlugin> workerPlugins) {
@Autowired(required = false) @Nullable List<WorkerPlugin> workerPlugins,
@Autowired(required = false) @Nullable List<NexusClientInterceptor> nexusClientInterceptors,
@Autowired(required = false) @Nullable
Map<String, TemporalOptionsCustomizer<NexusClientOptions.Builder>>
nexusClientCustomizerMap) {
DataConverter chosenDataConverter =
AutoConfigurationUtils.chooseDataConverter(dataConverters, mainDataConverter, properties);
List<WorkflowClientInterceptor> chosenClientInterceptors =
Expand Down Expand Up @@ -131,6 +138,9 @@ public NamespaceTemplate rootNamespaceTemplate(
List<TemporalOptionsCustomizer<ActivityClientOptions.Builder>> activityCustomizer =
AutoConfigurationUtils.chooseTemporalCustomizerBeans(
beanFactory, activityCustomizerMap, ActivityClientOptions.Builder.class, properties);
List<TemporalOptionsCustomizer<NexusClientOptions.Builder>> nexusClientCustomizers =
AutoConfigurationUtils.chooseTemporalCustomizerBeans(
beanFactory, nexusClientCustomizerMap, NexusClientOptions.Builder.class, properties);
List<TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder>>
workflowImplementationCustomizer =
AutoConfigurationUtils.chooseTemporalCustomizerBeans(
Expand Down Expand Up @@ -180,7 +190,9 @@ public NamespaceTemplate rootNamespaceTemplate(
filteredSchedulePlugins,
filteredWorkerPlugins,
activityClientInterceptors,
activityCustomizer);
activityCustomizer,
nexusClientInterceptors,
nexusClientCustomizers);
}

/** Client */
Expand Down Expand Up @@ -210,6 +222,13 @@ public ActivityClient activityClient(ClientTemplate clientTemplate) {
return clientTemplate.getActivityClient();
}

@Primary
@Bean(name = "temporalNexusClient")
@ConditionalOnMissingBean(NexusClient.class)
public NexusClient nexusClient(ClientTemplate clientTemplate) {
return clientTemplate.getNexusClient();
}

/** Workers */
@Primary
@Bean(name = "temporalWorkersTemplate")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class NonRootNamespaceProperties extends NamespaceProperties {
* - WorkflowClient <br>
* - ScheduleClient <br>
* - ActivityClient <br>
* - NexusClient <br>
* - WorkerFactory <br>
* You guys can use this alias to get the beans. <br>
* for example if you set spring.temporal.namespace[0].alias=foo <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import io.opentracing.Tracer;
import io.temporal.client.ActivityClient;
import io.temporal.client.ActivityClientOptions;
import io.temporal.client.NexusClient;
import io.temporal.client.NexusClientOptions;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowClientOptions;
import io.temporal.client.WorkflowClientPlugin;
Expand All @@ -12,6 +14,7 @@
import io.temporal.client.schedules.ScheduleClientPlugin;
import io.temporal.common.converter.DataConverter;
import io.temporal.common.interceptors.ActivityClientInterceptor;
import io.temporal.common.interceptors.NexusClientInterceptor;
import io.temporal.common.interceptors.ScheduleClientInterceptor;
import io.temporal.common.interceptors.WorkflowClientInterceptor;
import io.temporal.serviceclient.WorkflowServiceStubs;
Expand All @@ -22,13 +25,15 @@

public class ClientTemplate {
private final @Nonnull WorkflowClientOptionsTemplate optionsTemplate;
private final @Nonnull NexusClientOptionsTemplate nexusOptionsTemplate;
private final @Nullable WorkflowServiceStubs workflowServiceStubs;
// if not null, we work with an environment with defined test server
private final @Nullable TestWorkflowEnvironmentAdapter testWorkflowEnvironment;

private WorkflowClient workflowClient;
private ScheduleClient scheduleClient;
private ActivityClient activityClient;
private NexusClient nexusClient;

public ClientTemplate(
@Nonnull String namespace,
Expand All @@ -43,8 +48,10 @@ public ClientTemplate(
@Nullable List<WorkflowClientPlugin> workflowClientPlugins,
@Nullable List<ScheduleClientPlugin> scheduleClientPlugins,
@Nullable List<ActivityClientInterceptor> activityClientInterceptors,
@Nullable List<TemporalOptionsCustomizer<ActivityClientOptions.Builder>> activityCustomizers,
@Nullable List<NexusClientInterceptor> nexusClientInterceptors,
@Nullable
List<TemporalOptionsCustomizer<ActivityClientOptions.Builder>> activityCustomizers) {
List<TemporalOptionsCustomizer<NexusClientOptions.Builder>> nexusClientCustomizers) {
this.optionsTemplate =
new WorkflowClientOptionsTemplate(
namespace,
Expand All @@ -58,6 +65,9 @@ public ClientTemplate(
scheduleClientPlugins,
activityClientInterceptors,
activityCustomizers);
this.nexusOptionsTemplate =
new NexusClientOptionsTemplate(
namespace, dataConverter, nexusClientInterceptors, nexusClientCustomizers);
this.workflowServiceStubs = workflowServiceStubs;
this.testWorkflowEnvironment = testWorkflowEnvironment;
}
Expand All @@ -83,6 +93,20 @@ public ActivityClient getActivityClient() {
return activityClient;
}

public NexusClient getNexusClient() {
if (nexusClient == null) {
WorkflowServiceStubs serviceStubs =
testWorkflowEnvironment != null
? testWorkflowEnvironment.getWorkflowClient().getWorkflowServiceStubs()
: workflowServiceStubs;
Preconditions.checkState(
serviceStubs != null, "ClientTemplate was created without workflowServiceStubs");
this.nexusClient =
NexusClient.newInstance(serviceStubs, nexusOptionsTemplate.createNexusClientOptions());
}
return nexusClient;
}

private WorkflowClient createWorkflowClient() {
if (testWorkflowEnvironment != null) {
return testWorkflowEnvironment.getWorkflowClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import io.opentracing.Tracer;
import io.temporal.client.ActivityClientOptions;
import io.temporal.client.NexusClientOptions;
import io.temporal.client.WorkflowClientOptions;
import io.temporal.client.WorkflowClientPlugin;
import io.temporal.client.schedules.ScheduleClientOptions;
import io.temporal.client.schedules.ScheduleClientPlugin;
import io.temporal.common.converter.DataConverter;
import io.temporal.common.interceptors.ActivityClientInterceptor;
import io.temporal.common.interceptors.NexusClientInterceptor;
import io.temporal.common.interceptors.ScheduleClientInterceptor;
import io.temporal.common.interceptors.WorkerInterceptor;
import io.temporal.common.interceptors.WorkflowClientInterceptor;
Expand Down Expand Up @@ -47,6 +49,9 @@ public class NamespaceTemplate {
private final @Nullable List<ActivityClientInterceptor> activityClientInterceptors;
private final @Nullable List<TemporalOptionsCustomizer<ActivityClientOptions.Builder>>
activityCustomizers;
private final @Nullable List<NexusClientInterceptor> nexusClientInterceptors;
private final @Nullable List<TemporalOptionsCustomizer<NexusClientOptions.Builder>>
nexusClientCustomizers;

private ClientTemplate clientTemplate;
private WorkersTemplate workersTemplate;
Expand All @@ -72,8 +77,10 @@ public NamespaceTemplate(
@Nullable List<ScheduleClientPlugin> scheduleClientPlugins,
@Nullable List<WorkerPlugin> workerPlugins,
@Nullable List<ActivityClientInterceptor> activityClientInterceptors,
@Nullable List<TemporalOptionsCustomizer<ActivityClientOptions.Builder>> activityCustomizers,
@Nullable List<NexusClientInterceptor> nexusClientInterceptors,
@Nullable
List<TemporalOptionsCustomizer<ActivityClientOptions.Builder>> activityCustomizers) {
List<TemporalOptionsCustomizer<NexusClientOptions.Builder>> nexusClientCustomizers) {
this.namespaceProperties = namespaceProperties;
this.workflowServiceStubs = workflowServiceStubs;
this.dataConverter = dataConverter;
Expand All @@ -93,6 +100,8 @@ public NamespaceTemplate(
this.workerPlugins = workerPlugins;
this.activityClientInterceptors = activityClientInterceptors;
this.activityCustomizers = activityCustomizers;
this.nexusClientInterceptors = nexusClientInterceptors;
this.nexusClientCustomizers = nexusClientCustomizers;
}

public ClientTemplate getClientTemplate() {
Expand All @@ -111,7 +120,9 @@ public ClientTemplate getClientTemplate() {
workflowClientPlugins,
scheduleClientPlugins,
activityClientInterceptors,
activityCustomizers);
activityCustomizers,
nexusClientInterceptors,
nexusClientCustomizers);
}
return clientTemplate;
}
Expand Down
Loading
Loading