Skip to content

Commit 0e8ac35

Browse files
Make @Profile annotation work (#2501)
Make @Profile annotation work
1 parent 0d539ff commit 0e8ac35

File tree

7 files changed

+107
-1
lines changed

7 files changed

+107
-1
lines changed

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
@@ -311,7 +311,7 @@ private void configureNexusServiceBeansByWorkerName(
311311

312312
private Collection<Class<?>> autoDiscoverWorkflowImplementations() {
313313
ClassPathScanningCandidateComponentProvider scanner =
314-
new ClassPathScanningCandidateComponentProvider(false);
314+
new ClassPathScanningCandidateComponentProvider(false, environment);
315315
scanner.addIncludeFilter(new AnnotationTypeFilter(WorkflowImpl.class));
316316
Set<Class<?>> implementations = new HashSet<>();
317317
for (String pckg : namespaceProperties.getWorkersAutoDiscovery().getPackages()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.temporal.spring.boot.autoconfigure;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import io.temporal.client.WorkflowClient;
6+
import io.temporal.client.WorkflowOptions;
7+
import io.temporal.spring.boot.autoconfigure.bytaskqueue.TestWorkflow;
8+
import io.temporal.testing.TestWorkflowEnvironment;
9+
import org.junit.jupiter.api.*;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.context.ConfigurableApplicationContext;
13+
import org.springframework.context.annotation.ComponentScan;
14+
import org.springframework.context.annotation.FilterType;
15+
import org.springframework.test.context.ActiveProfiles;
16+
17+
@SpringBootTest(classes = AutoDiscoveryWithProfileTest.Configuration.class)
18+
@ActiveProfiles(profiles = "auto-discovery-with-profile")
19+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
20+
public class AutoDiscoveryWithProfileTest {
21+
@Autowired ConfigurableApplicationContext applicationContext;
22+
23+
@Autowired TestWorkflowEnvironment testWorkflowEnvironment;
24+
25+
@Autowired WorkflowClient workflowClient;
26+
27+
@BeforeEach
28+
void setUp() {
29+
applicationContext.start();
30+
}
31+
32+
@Test
33+
@Timeout(value = 10)
34+
public void testAutoDiscoveryWithProfile() {
35+
TestWorkflow testWorkflow =
36+
workflowClient.newWorkflowStub(
37+
TestWorkflow.class, WorkflowOptions.newBuilder().setTaskQueue("UnitTest").build());
38+
assertEquals("other workflow discovered", testWorkflow.execute("profile"));
39+
}
40+
41+
@ComponentScan(
42+
excludeFilters =
43+
@ComponentScan.Filter(
44+
pattern = "io\\.temporal\\.spring\\.boot\\.autoconfigure\\.bytaskqueue\\..*",
45+
type = FilterType.REGEX))
46+
public static class Configuration {}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.temporal.spring.boot.autoconfigure.byworkername;
2+
3+
import io.temporal.spring.boot.ActivityImpl;
4+
import org.springframework.context.annotation.Profile;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component("TestActivityImpl")
8+
@ActivityImpl(workers = "mainWorker")
9+
@Profile("auto-discovery-with-profile")
10+
public class OtherTestActivityImpl implements TestActivity {
11+
@Override
12+
public String execute(String input) {
13+
return "other workflow " + input;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.temporal.spring.boot.autoconfigure.byworkername;
2+
3+
import io.temporal.activity.ActivityOptions;
4+
import io.temporal.spring.boot.WorkflowImpl;
5+
import io.temporal.workflow.Workflow;
6+
import java.time.Duration;
7+
import org.springframework.context.ConfigurableApplicationContext;
8+
import org.springframework.context.annotation.Profile;
9+
10+
@WorkflowImpl(workers = "mainWorker")
11+
@Profile("auto-discovery-with-profile")
12+
public class OtherTestWorkflowImpl implements TestWorkflow {
13+
14+
// Test auto-wiring of the application context works, this is not indicative of a real-world use
15+
// case as the workflow implementation should be stateless.
16+
public OtherTestWorkflowImpl(ConfigurableApplicationContext applicationContext) {}
17+
18+
@Override
19+
public String execute(String input) {
20+
return Workflow.newActivityStub(
21+
TestActivity.class,
22+
ActivityOptions.newBuilder()
23+
.setStartToCloseTimeout(Duration.ofSeconds(1))
24+
.validateAndBuildWithDefaults())
25+
.execute("discovered");
26+
}
27+
}

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
@@ -1,10 +1,12 @@
11
package io.temporal.spring.boot.autoconfigure.byworkername;
22

33
import io.temporal.spring.boot.ActivityImpl;
4+
import org.springframework.context.annotation.Profile;
45
import org.springframework.stereotype.Component;
56

67
@Component("TestActivityImpl")
78
@ActivityImpl(workers = "mainWorker")
9+
@Profile("!auto-discovery-with-profile")
810
public class TestActivityImpl implements TestActivity {
911
@Override
1012
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
@@ -7,8 +7,10 @@
77
import io.temporal.workflow.Workflow;
88
import java.time.Duration;
99
import org.springframework.context.ConfigurableApplicationContext;
10+
import org.springframework.context.annotation.Profile;
1011

1112
@WorkflowImpl(workers = "mainWorker")
13+
@Profile("!auto-discovery-with-profile")
1214
public class TestWorkflowImpl implements TestWorkflow {
1315

1416
// 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)