Skip to content

Commit

Permalink
add WorkflowImplementationOptions customizer (#1835)
Browse files Browse the repository at this point in the history
add WorkflowImplementationOptions customizer
  • Loading branch information
antmendoza authored Aug 19, 2023
1 parent 0f37297 commit c012357
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 12 deletions.
1 change: 1 addition & 0 deletions temporal-spring-boot-autoconfigure-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Where `OptionsType` may be one of:
- `WorkflowClientOption.Builder`
- `WorkerFactoryOptions.Builder`
- `WorkerOptions.Builder`
- `WorkflowImplementationOptions.Builder`
- `TestEnvironmentOptions.Builder`

`io.temporal.spring.boot.WorkerOptionsCustomizer` may be used instead of `TemporalOptionsCustomizer<WorkerOptions.Builder>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.temporal.testing.TestEnvironmentOptions;
import io.temporal.worker.WorkerFactoryOptions;
import io.temporal.worker.WorkerOptions;
import io.temporal.worker.WorkflowImplementationOptions;
import javax.annotation.Nonnull;

/**
Expand All @@ -36,7 +37,7 @@
* @param <T> Temporal Options Builder to customize. Respected types: {@link
* WorkflowServiceStubsOptions.Builder}, {@link WorkflowClientOptions.Builder}, {@link
* TestEnvironmentOptions.Builder}, {@link WorkerOptions.Builder}, {@link
* WorkerFactoryOptions.Builder}
* 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 @@ -33,10 +33,7 @@
import io.temporal.spring.boot.autoconfigure.template.NamespaceTemplate;
import io.temporal.spring.boot.autoconfigure.template.TestWorkflowEnvironmentAdapter;
import io.temporal.spring.boot.autoconfigure.template.WorkersTemplate;
import io.temporal.worker.Worker;
import io.temporal.worker.WorkerFactory;
import io.temporal.worker.WorkerFactoryOptions;
import io.temporal.worker.WorkerOptions;
import io.temporal.worker.*;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -90,7 +87,10 @@ public NamespaceTemplate rootNamespaceTemplate(
@Autowired(required = false) @Nullable
TemporalOptionsCustomizer<WorkflowClientOptions.Builder> clientCustomizer,
@Autowired(required = false) @Nullable
TemporalOptionsCustomizer<ScheduleClientOptions.Builder> scheduleCustomize) {
TemporalOptionsCustomizer<ScheduleClientOptions.Builder> scheduleCustomizer,
@Autowired(required = false) @Nullable
TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder>
workflowImplementationCustomizer) {
DataConverter chosenDataConverter =
AutoConfigurationUtils.choseDataConverter(dataConverters, mainDataConverter);
return new NamespaceTemplate(
Expand All @@ -103,7 +103,8 @@ public NamespaceTemplate rootNamespaceTemplate(
workerFactoryCustomizer,
workerCustomizer,
clientCustomizer,
scheduleCustomize);
scheduleCustomizer,
workflowImplementationCustomizer);
}

/** Client */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.temporal.spring.boot.autoconfigure.properties.TemporalProperties;
import io.temporal.worker.WorkerFactoryOptions;
import io.temporal.worker.WorkerOptions;
import io.temporal.worker.WorkflowImplementationOptions;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand All @@ -47,6 +48,8 @@ public class NamespaceTemplate {
private final @Nullable TemporalOptionsCustomizer<WorkflowClientOptions.Builder> clientCustomizer;
private final @Nullable TemporalOptionsCustomizer<ScheduleClientOptions.Builder>
scheduleCustomizer;
private final @Nullable TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder>
workflowImplementationCustomizer;

private ClientTemplate clientTemplate;
private WorkersTemplate workersTemplate;
Expand All @@ -61,7 +64,10 @@ public NamespaceTemplate(
@Nullable TemporalOptionsCustomizer<WorkerFactoryOptions.Builder> workerFactoryCustomizer,
@Nullable TemporalOptionsCustomizer<WorkerOptions.Builder> workerCustomizer,
@Nullable TemporalOptionsCustomizer<WorkflowClientOptions.Builder> clientCustomizer,
@Nullable TemporalOptionsCustomizer<ScheduleClientOptions.Builder> scheduleCustomizer) {
@Nullable TemporalOptionsCustomizer<ScheduleClientOptions.Builder> scheduleCustomizer,
@Nullable
TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder>
workflowImplementationCustomizer) {
this.properties = properties;
this.namespaceProperties = namespaceProperties;
this.workflowServiceStubs = workflowServiceStubs;
Expand All @@ -73,6 +79,7 @@ public NamespaceTemplate(
this.workerCustomizer = workerCustomizer;
this.clientCustomizer = clientCustomizer;
this.scheduleCustomizer = scheduleCustomizer;
this.workflowImplementationCustomizer = workflowImplementationCustomizer;
}

public ClientTemplate getClientTemplate() {
Expand Down Expand Up @@ -100,7 +107,8 @@ public WorkersTemplate getWorkersTemplate() {
tracer,
testWorkflowEnvironment,
workerFactoryCustomizer,
workerCustomizer);
workerCustomizer,
workflowImplementationCustomizer);
}
return this.workersTemplate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class WorkersTemplate implements BeanFactoryAware, EnvironmentAware {
workerFactoryCustomizer;

private final @Nullable TemporalOptionsCustomizer<WorkerOptions.Builder> workerCustomizer;
private final @Nullable TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder>
workflowImplementationCustomizer;

private ConfigurableListableBeanFactory beanFactory;
private Environment environment;
Expand All @@ -80,7 +82,10 @@ public WorkersTemplate(
@Nullable Tracer tracer,
@Nullable TestWorkflowEnvironmentAdapter testWorkflowEnvironment,
@Nullable TemporalOptionsCustomizer<WorkerFactoryOptions.Builder> workerFactoryCustomizer,
@Nullable TemporalOptionsCustomizer<WorkerOptions.Builder> workerCustomizer) {
@Nullable TemporalOptionsCustomizer<WorkerOptions.Builder> workerCustomizer,
@Nullable
TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder>
workflowImplementationCustomizer) {
this.properties = properties;
this.namespaceProperties = namespaceProperties;
this.tracer = tracer;
Expand All @@ -89,6 +94,7 @@ public WorkersTemplate(

this.workerFactoryCustomizer = workerFactoryCustomizer;
this.workerCustomizer = workerCustomizer;
this.workflowImplementationCustomizer = workflowImplementationCustomizer;
}

public WorkerFactory getWorkerFactory() {
Expand Down Expand Up @@ -363,10 +369,15 @@ private <T> void configureWorkflowImplementation(Worker worker, Class<?> clazz)
+ clazz);
}

WorkflowImplementationOptions workflowImplementationOptions =
new WorkflowImplementationOptionsTemplate(workflowImplementationCustomizer)
.createWorkflowImplementationOptions();

for (POJOWorkflowMethodMetadata workflowMethod : workflowMetadata.getWorkflowMethods()) {
worker.registerWorkflowImplementationFactory(
(Class<T>) workflowMethod.getWorkflowInterface(),
() -> (T) beanFactory.createBean(clazz));
() -> (T) beanFactory.createBean(clazz),
workflowImplementationOptions);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this material except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.temporal.spring.boot.autoconfigure.template;

import io.temporal.spring.boot.TemporalOptionsCustomizer;
import io.temporal.worker.WorkflowImplementationOptions;
import javax.annotation.Nullable;

public class WorkflowImplementationOptionsTemplate {
private final @Nullable TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder>
customizer;

public WorkflowImplementationOptionsTemplate(
@Nullable TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder> customizer) {
this.customizer = customizer;
}

public WorkflowImplementationOptions createWorkflowImplementationOptions() {
WorkflowImplementationOptions.Builder options = WorkflowImplementationOptions.newBuilder();

if (customizer != null) {
options = customizer.customize(options);
}

return options.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.temporal.spring.boot.WorkerOptionsCustomizer;
import io.temporal.testing.TestEnvironmentOptions;
import io.temporal.worker.WorkerFactoryOptions;
import io.temporal.worker.WorkflowImplementationOptions;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -59,7 +60,7 @@ void setUp() {
@Test
@Timeout(value = 10)
public void testCustomizersGotCalled() {
assertEquals(4, customizers.size());
assertEquals(5, customizers.size());
customizers.forEach(c -> verify(c).customize(any()));
verify(workerCustomizer).customize(any(), eq("UnitTest"), eq("UnitTest"));
}
Expand All @@ -86,6 +87,12 @@ public TemporalOptionsCustomizer<WorkerFactoryOptions.Builder> workerFactoryCust
return getReturningMock();
}

@Bean
public TemporalOptionsCustomizer<WorkflowImplementationOptions.Builder>
WorkflowImplementationCustomizer() {
return getReturningMock();
}

@Bean
public WorkerOptionsCustomizer workerCustomizer() {
WorkerOptionsCustomizer mock = mock(WorkerOptionsCustomizer.class);
Expand Down

0 comments on commit c012357

Please sign in to comment.