Skip to content

Commit 6c76718

Browse files
Make @Profile annotation work
1 parent 537d99d commit 6c76718

7 files changed

Lines changed: 167 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ private void configureNexusServiceBeansByWorkerName(
331331

332332
private Collection<Class<?>> autoDiscoverWorkflowImplementations() {
333333
ClassPathScanningCandidateComponentProvider scanner =
334-
new ClassPathScanningCandidateComponentProvider(false);
334+
new ClassPathScanningCandidateComponentProvider(false, environment);
335335
scanner.addIncludeFilter(new AnnotationTypeFilter(WorkflowImpl.class));
336336
Set<Class<?>> implementations = new HashSet<>();
337337
for (String pckg : namespaceProperties.getWorkersAutoDiscovery().getPackages()) {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 static org.junit.jupiter.api.Assertions.assertEquals;
24+
25+
import io.temporal.client.WorkflowClient;
26+
import io.temporal.client.WorkflowOptions;
27+
import io.temporal.spring.boot.autoconfigure.bytaskqueue.TestWorkflow;
28+
import io.temporal.testing.TestWorkflowEnvironment;
29+
import org.junit.jupiter.api.*;
30+
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.boot.test.context.SpringBootTest;
32+
import org.springframework.context.ConfigurableApplicationContext;
33+
import org.springframework.context.annotation.ComponentScan;
34+
import org.springframework.context.annotation.FilterType;
35+
import org.springframework.test.context.ActiveProfiles;
36+
37+
@SpringBootTest(classes = AutoDiscoveryWithProfileTest.Configuration.class)
38+
@ActiveProfiles(profiles = "auto-discovery-with-profile")
39+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
40+
public class AutoDiscoveryWithProfileTest {
41+
@Autowired ConfigurableApplicationContext applicationContext;
42+
43+
@Autowired TestWorkflowEnvironment testWorkflowEnvironment;
44+
45+
@Autowired WorkflowClient workflowClient;
46+
47+
@BeforeEach
48+
void setUp() {
49+
applicationContext.start();
50+
}
51+
52+
@Test
53+
@Timeout(value = 10)
54+
public void testAutoDiscoveryWithProfile() {
55+
TestWorkflow testWorkflow =
56+
workflowClient.newWorkflowStub(
57+
TestWorkflow.class, WorkflowOptions.newBuilder().setTaskQueue("UnitTest").build());
58+
assertEquals("other workflow discovered", testWorkflow.execute("profile"));
59+
}
60+
61+
@ComponentScan(
62+
excludeFilters =
63+
@ComponentScan.Filter(
64+
pattern = "io\\.temporal\\.spring\\.boot\\.autoconfigure\\.bytaskqueue\\..*",
65+
type = FilterType.REGEX))
66+
public static class Configuration {}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.byworkername;
22+
23+
import io.temporal.spring.boot.ActivityImpl;
24+
import org.springframework.context.annotation.Profile;
25+
import org.springframework.stereotype.Component;
26+
27+
@Component("TestActivityImpl")
28+
@ActivityImpl(workers = "mainWorker")
29+
@Profile("auto-discovery-with-profile")
30+
public class OtherTestActivityImpl implements TestActivity {
31+
@Override
32+
public String execute(String input) {
33+
return "other workflow " + input;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.byworkername;
22+
23+
import io.temporal.activity.ActivityOptions;
24+
import io.temporal.spring.boot.WorkflowImpl;
25+
import io.temporal.workflow.Workflow;
26+
import java.time.Duration;
27+
import org.springframework.context.ConfigurableApplicationContext;
28+
import org.springframework.context.annotation.Profile;
29+
30+
@WorkflowImpl(workers = "mainWorker")
31+
@Profile("auto-discovery-with-profile")
32+
public class OtherTestWorkflowImpl implements TestWorkflow {
33+
34+
// Test auto-wiring of the application context works, this is not indicative of a real-world use
35+
// case as the workflow implementation should be stateless.
36+
public OtherTestWorkflowImpl(ConfigurableApplicationContext applicationContext) {}
37+
38+
@Override
39+
public String execute(String input) {
40+
return Workflow.newActivityStub(
41+
TestActivity.class,
42+
ActivityOptions.newBuilder()
43+
.setStartToCloseTimeout(Duration.ofSeconds(1))
44+
.validateAndBuildWithDefaults())
45+
.execute("discovered");
46+
}
47+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
package io.temporal.spring.boot.autoconfigure.byworkername;
2222

2323
import io.temporal.spring.boot.ActivityImpl;
24+
import org.springframework.context.annotation.Profile;
2425
import org.springframework.stereotype.Component;
2526

2627
@Component("TestActivityImpl")
2728
@ActivityImpl(workers = "mainWorker")
29+
@Profile("!auto-discovery-with-profile")
2830
public class TestActivityImpl implements TestActivity {
2931
@Override
3032
public String execute(String input) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import io.temporal.workflow.Workflow;
2828
import java.time.Duration;
2929
import org.springframework.context.ConfigurableApplicationContext;
30+
import org.springframework.context.annotation.Profile;
3031

3132
@WorkflowImpl(workers = "mainWorker")
33+
@Profile("!auto-discovery-with-profile")
3234
public class TestWorkflowImpl implements TestWorkflow {
3335

3436
// Test auto-wiring of the application context works, this is not indicative of a real-world use

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ spring:
5959
packages:
6060
- io.temporal.spring.boot.autoconfigure.byworkername
6161

62+
---
63+
spring:
64+
config:
65+
activate:
66+
on-profile: auto-discovery-with-profile
67+
temporal:
68+
workers:
69+
- task-queue: UnitTest
70+
name: mainWorker
71+
workers-auto-discovery:
72+
packages:
73+
- io.temporal.spring.boot.autoconfigure.byworkername
74+
6275
---
6376
spring:
6477
config:

0 commit comments

Comments
 (0)