Skip to content

Commit 5ed77f7

Browse files
authored
Allow task queue names to use property placeholders (#1760)
1 parent d5b3e89 commit 5ed77f7

File tree

7 files changed

+96
-5
lines changed

7 files changed

+96
-5
lines changed

temporal-spring-boot-autoconfigure-alpha/src/main/java/io/temporal/spring/boot/ActivityImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
/**
4343
* @return Worker Task Queues to register this activity bean with. If Worker with the specified
4444
* Task Queue is not present in the application config, it will be created with a default
45-
* config.
45+
* config. Can be specified as a property key, e.g.: ${propertyKey}.
4646
*/
4747
String[] taskQueues() default {};
4848
}

temporal-spring-boot-autoconfigure-alpha/src/main/java/io/temporal/spring/boot/WorkflowImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
/**
4343
* @return Worker Task Queues to register this workflow implementation with. If Worker with the
4444
* specified Task Queue is not defined in the application config, it will be created with a
45-
* default config.
45+
* default config. Can be specified by a property key, e.g.: ${propertyKey}.
4646
*/
4747
String[] taskQueues() default {};
4848
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@
4444
import org.springframework.beans.factory.config.BeanDefinition;
4545
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4646
import org.springframework.beans.factory.support.BeanDefinitionValidationException;
47+
import org.springframework.context.EnvironmentAware;
4748
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
4849
import org.springframework.core.annotation.AnnotationUtils;
50+
import org.springframework.core.env.Environment;
4951
import org.springframework.core.type.filter.AnnotationTypeFilter;
5052
import org.springframework.util.Assert;
5153

5254
/** Creates a {@link WorkerFactory} and Workers for a given namespace provided by WorkflowClient. */
53-
public class WorkersTemplate implements BeanFactoryAware {
55+
public class WorkersTemplate implements BeanFactoryAware, EnvironmentAware {
5456
private static final Logger log = LoggerFactory.getLogger(WorkersTemplate.class);
5557

5658
private final @Nonnull TemporalProperties properties;
@@ -66,6 +68,7 @@ public class WorkersTemplate implements BeanFactoryAware {
6668
private final @Nullable TemporalOptionsCustomizer<WorkerOptions.Builder> workerCustomizer;
6769

6870
private ConfigurableListableBeanFactory beanFactory;
71+
private Environment environment;
6972

7073
private WorkerFactory workerFactory;
7174
private Collection<Worker> workers;
@@ -149,6 +152,7 @@ private void configureWorkflowImplementationsByTaskQueue(
149152
for (Class<?> clazz : autoDiscoveredWorkflowImplementationClasses) {
150153
WorkflowImpl annotation = clazz.getAnnotation(WorkflowImpl.class);
151154
for (String taskQueue : annotation.taskQueues()) {
155+
taskQueue = environment.resolvePlaceholders(taskQueue);
152156
Worker worker = workerFactory.tryGetWorker(taskQueue);
153157
if (worker == null) {
154158
log.info(
@@ -175,6 +179,7 @@ private void configureActivityBeansByTaskQueue(
175179
ActivityImpl annotation = AnnotationUtils.findAnnotation(targetClass, ActivityImpl.class);
176180
if (annotation != null) {
177181
for (String taskQueue : annotation.taskQueues()) {
182+
taskQueue = environment.resolvePlaceholders(taskQueue);
178183
Worker worker = workerFactory.tryGetWorker(taskQueue);
179184
if (worker == null) {
180185
log.info(
@@ -371,6 +376,11 @@ public void setBeanFactory(@Nonnull BeanFactory beanFactory) throws BeansExcepti
371376
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
372377
}
373378

379+
@Override
380+
public void setEnvironment(@Nonnull Environment environment) {
381+
this.environment = environment;
382+
}
383+
374384
private Worker createNewWorker(
375385
@Nonnull String taskQueue, @Nullable WorkerProperties properties, @Nonnull Workers workers) {
376386
Preconditions.checkState(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this material except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package io.temporal.spring.boot.autoconfigure;
22+
23+
import io.temporal.client.WorkflowClient;
24+
import io.temporal.client.WorkflowOptions;
25+
import io.temporal.spring.boot.autoconfigure.bytaskqueue.TestWorkflow;
26+
import io.temporal.worker.WorkerFactory;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.TestInstance;
30+
import org.junit.jupiter.api.Timeout;
31+
import org.springframework.beans.factory.annotation.Autowired;
32+
import org.springframework.boot.test.context.SpringBootTest;
33+
import org.springframework.context.ConfigurableApplicationContext;
34+
import org.springframework.context.annotation.ComponentScan;
35+
import org.springframework.context.annotation.FilterType;
36+
import org.springframework.test.context.ActiveProfiles;
37+
38+
@SpringBootTest(classes = AutoDiscoveryByTaskQueueResolverTest.Configuration.class)
39+
@ActiveProfiles(profiles = "auto-discovery-by-task-queue-dynamic-suffix")
40+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
41+
public class AutoDiscoveryByTaskQueueResolverTest {
42+
@Autowired ConfigurableApplicationContext applicationContext;
43+
44+
@Autowired WorkflowClient workflowClient;
45+
46+
@Autowired WorkerFactory workerFactory;
47+
48+
@BeforeEach
49+
void setUp() {
50+
applicationContext.start();
51+
}
52+
53+
@Test
54+
@Timeout(value = 10)
55+
public void testAutoDiscovery() {
56+
TestWorkflow testWorkflow =
57+
workflowClient.newWorkflowStub(
58+
TestWorkflow.class,
59+
WorkflowOptions.newBuilder().setTaskQueue("PropertyResolverTest").build());
60+
testWorkflow.execute("input");
61+
}
62+
63+
@ComponentScan(
64+
excludeFilters =
65+
@ComponentScan.Filter(
66+
pattern = "io\\.temporal\\.spring\\.boot\\.autoconfigure\\.byworkername\\..*",
67+
type = FilterType.REGEX))
68+
public static class Configuration {}
69+
}

temporal-spring-boot-autoconfigure-alpha/src/test/java/io/temporal/spring/boot/autoconfigure/bytaskqueue/TestActivityImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.springframework.stereotype.Component;
2525

2626
@Component("TestActivityImpl")
27-
@ActivityImpl(taskQueues = "UnitTest")
27+
@ActivityImpl(taskQueues = "${default-queue.name:UnitTest}")
2828
public class TestActivityImpl implements TestActivity {
2929
@Override
3030
public String execute(String input) {

temporal-spring-boot-autoconfigure-alpha/src/test/java/io/temporal/spring/boot/autoconfigure/bytaskqueue/TestWorkflowImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import io.temporal.workflow.Workflow;
2626
import java.time.Duration;
2727

28-
@WorkflowImpl(taskQueues = "UnitTest")
28+
@WorkflowImpl(taskQueues = {"${default-queue.name:UnitTest}"})
2929
public class TestWorkflowImpl implements TestWorkflow {
3030
@Override
3131
public String execute(String input) {

temporal-spring-boot-autoconfigure-alpha/src/test/resources/application.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ spring:
3737
packages:
3838
- io.temporal.spring.boot.autoconfigure.bytaskqueue
3939

40+
---
41+
spring:
42+
config:
43+
activate:
44+
on-profile: auto-discovery-by-task-queue-dynamic-suffix
45+
temporal:
46+
workers-auto-discovery:
47+
packages:
48+
- io.temporal.spring.boot.autoconfigure.bytaskqueue
49+
default-queue:
50+
name: PropertyResolverTest
51+
4052
---
4153
spring:
4254
config:

0 commit comments

Comments
 (0)