Skip to content

Commit 26a8595

Browse files
authored
SpringBoot - add workflow and activity metadata to RegisteredInfo (#1995)
* SpringBoot - add workflow and activity metadata to RegisteredInfo Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * marked register classes as experimental Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * adding test Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> --------- Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io>
1 parent e603fd8 commit 26a8595

File tree

2 files changed

+155
-9
lines changed

2 files changed

+155
-9
lines changed

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

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.opentracing.Tracer;
2525
import io.temporal.client.WorkflowClient;
2626
import io.temporal.common.Experimental;
27+
import io.temporal.common.metadata.POJOActivityImplMetadata;
2728
import io.temporal.common.metadata.POJOWorkflowImplMetadata;
2829
import io.temporal.common.metadata.POJOWorkflowMethodMetadata;
2930
import io.temporal.spring.boot.ActivityImpl;
@@ -314,7 +315,10 @@ private void createWorkerFromAnExplicitConfig(
314315
AopUtils.getTargetClass(bean),
315316
taskQueue);
316317
worker.registerActivitiesImplementations(bean);
317-
addRegisteredActivityImpl(worker, beanName, bean.getClass().getName());
318+
POJOActivityImplMetadata activityImplMetadata =
319+
POJOActivityImplMetadata.newInstance(AopUtils.getTargetClass(bean));
320+
addRegisteredActivityImpl(
321+
worker, beanName, bean.getClass().getName(), activityImplMetadata);
318322
});
319323
}
320324
}
@@ -328,7 +332,9 @@ private void configureActivityImplementationAutoDiscovery(
328332
Workers workers) {
329333
try {
330334
worker.registerActivitiesImplementations(bean);
331-
addRegisteredActivityImpl(worker, beanName, bean.getClass().getName());
335+
POJOActivityImplMetadata activityImplMetadata =
336+
POJOActivityImplMetadata.newInstance(AopUtils.getTargetClass(bean));
337+
addRegisteredActivityImpl(worker, beanName, bean.getClass().getName(), activityImplMetadata);
332338
if (log.isInfoEnabled()) {
333339
log.info(
334340
"Registering auto-discovered activity bean '{}' of class {} on a worker {}with a task queue '{}'",
@@ -396,7 +402,8 @@ private <T> void configureWorkflowImplementation(Worker worker, Class<?> clazz)
396402
(Class<T>) workflowMethod.getWorkflowInterface(),
397403
() -> (T) beanFactory.createBean(clazz),
398404
workflowImplementationOptions);
399-
addRegisteredWorkflowImpl(worker, workflowMethod.getWorkflowInterface().getName());
405+
addRegisteredWorkflowImpl(
406+
worker, workflowMethod.getWorkflowInterface().getName(), workflowMetadata);
400407
}
401408
}
402409

@@ -429,32 +436,42 @@ private Worker createNewWorker(
429436
return worker;
430437
}
431438

432-
private void addRegisteredWorkflowImpl(Worker worker, String workflowClass) {
439+
private void addRegisteredWorkflowImpl(
440+
Worker worker, String workflowClass, POJOWorkflowImplMetadata metadata) {
433441
if (!registeredInfo.containsKey(worker.getTaskQueue())) {
434442
registeredInfo.put(
435443
worker.getTaskQueue(),
436444
new RegisteredInfo()
437-
.addWorkflowInfo(new RegisteredWorkflowInfo().addClassName(workflowClass)));
445+
.addWorkflowInfo(
446+
new RegisteredWorkflowInfo().addClassName(workflowClass).addMetadata(metadata)));
438447
} else {
439448
registeredInfo
440449
.get(worker.getTaskQueue())
441450
.getRegisteredWorkflowInfo()
442-
.add(new RegisteredWorkflowInfo().addClassName(workflowClass));
451+
.add(new RegisteredWorkflowInfo().addClassName(workflowClass).addMetadata(metadata));
443452
}
444453
}
445454

446-
private void addRegisteredActivityImpl(Worker worker, String beanName, String beanClass) {
455+
private void addRegisteredActivityImpl(
456+
Worker worker, String beanName, String beanClass, POJOActivityImplMetadata metadata) {
447457
if (!registeredInfo.containsKey(worker.getTaskQueue())) {
448458
registeredInfo.put(
449459
worker.getTaskQueue(),
450460
new RegisteredInfo()
451461
.addActivityInfo(
452-
new RegisteredActivityInfo().addBeanName(beanName).addClassName(beanClass)));
462+
new RegisteredActivityInfo()
463+
.addBeanName(beanName)
464+
.addClassName(beanClass)
465+
.addMetadata(metadata)));
453466
} else {
454467
registeredInfo
455468
.get(worker.getTaskQueue())
456469
.getRegisteredActivityInfo()
457-
.add(new RegisteredActivityInfo().addBeanName(beanName).addClassName(beanClass));
470+
.add(
471+
new RegisteredActivityInfo()
472+
.addBeanName(beanName)
473+
.addClassName(beanClass)
474+
.addMetadata(metadata));
458475
}
459476
}
460477

@@ -481,9 +498,11 @@ public List<RegisteredWorkflowInfo> getRegisteredWorkflowInfo() {
481498
}
482499
}
483500

501+
@Experimental
484502
public static class RegisteredActivityInfo {
485503
private String beanName;
486504
private String className;
505+
private POJOActivityImplMetadata metadata;
487506

488507
public RegisteredActivityInfo addClassName(String className) {
489508
this.className = className;
@@ -495,26 +514,46 @@ public RegisteredActivityInfo addBeanName(String beanName) {
495514
return this;
496515
}
497516

517+
public RegisteredActivityInfo addMetadata(POJOActivityImplMetadata metadata) {
518+
this.metadata = metadata;
519+
return this;
520+
}
521+
498522
public String getClassName() {
499523
return className;
500524
}
501525

502526
public String getBeanName() {
503527
return beanName;
504528
}
529+
530+
public POJOActivityImplMetadata getMetadata() {
531+
return metadata;
532+
}
505533
}
506534

535+
@Experimental
507536
public static class RegisteredWorkflowInfo {
508537
private String className;
538+
private POJOWorkflowImplMetadata metadata;
509539

510540
public RegisteredWorkflowInfo addClassName(String className) {
511541
this.className = className;
512542
return this;
513543
}
514544

545+
public RegisteredWorkflowInfo addMetadata(POJOWorkflowImplMetadata metadata) {
546+
this.metadata = metadata;
547+
return this;
548+
}
549+
515550
public String getClassName() {
516551
return className;
517552
}
553+
554+
public POJOWorkflowImplMetadata getMetadata() {
555+
return metadata;
556+
}
518557
}
519558

520559
private static class Workers {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.*;
24+
25+
import io.temporal.common.metadata.POJOActivityImplMetadata;
26+
import io.temporal.common.metadata.POJOWorkflowImplMetadata;
27+
import io.temporal.spring.boot.autoconfigure.template.WorkersTemplate;
28+
import java.util.Map;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.TestInstance;
32+
import org.junit.jupiter.api.Timeout;
33+
import org.springframework.beans.factory.annotation.Autowired;
34+
import org.springframework.boot.test.context.SpringBootTest;
35+
import org.springframework.context.ConfigurableApplicationContext;
36+
import org.springframework.context.annotation.ComponentScan;
37+
import org.springframework.context.annotation.FilterType;
38+
import org.springframework.test.context.ActiveProfiles;
39+
40+
@SpringBootTest(classes = RegisteredInfoTest.Configuration.class)
41+
@ActiveProfiles(profiles = "auto-discovery-by-worker-name")
42+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
43+
public class RegisteredInfoTest {
44+
45+
@Autowired ConfigurableApplicationContext applicationContext;
46+
47+
@Autowired private WorkersTemplate workersTemplate;
48+
49+
@BeforeEach
50+
void setUp() {
51+
applicationContext.start();
52+
}
53+
54+
@Test
55+
@Timeout(value = 10)
56+
public void testRegisteredInfo() {
57+
assertNotNull(workersTemplate);
58+
assertNotNull(workersTemplate.getRegisteredInfo());
59+
Map<String, WorkersTemplate.RegisteredInfo> registeredInfoMap =
60+
workersTemplate.getRegisteredInfo();
61+
62+
assertEquals(1, registeredInfoMap.size());
63+
registeredInfoMap.forEach(
64+
(taskQueue, info) -> {
65+
assertEquals("UnitTest", taskQueue);
66+
info.getRegisteredWorkflowInfo()
67+
.forEach(
68+
(workflowInfo) -> {
69+
assertNotNull(workflowInfo);
70+
assertEquals(
71+
"io.temporal.spring.boot.autoconfigure.byworkername.TestWorkflow",
72+
workflowInfo.getClassName());
73+
POJOWorkflowImplMetadata metadata = workflowInfo.getMetadata();
74+
assertNotNull(metadata);
75+
assertEquals(1, metadata.getWorkflowMethods().size());
76+
assertEquals(1, metadata.getWorkflowInterfaces().size());
77+
assertEquals(0, metadata.getSignalMethods().size());
78+
});
79+
80+
info.getRegisteredActivityInfo()
81+
.forEach(
82+
(activityInfo) -> {
83+
assertEquals(
84+
"io.temporal.spring.boot.autoconfigure.bytaskqueue.TestActivityImpl",
85+
activityInfo.getClassName());
86+
assertEquals("TestActivityImpl", activityInfo.getBeanName());
87+
POJOActivityImplMetadata metadata = activityInfo.getMetadata();
88+
assertEquals(1, metadata.getActivityInterfaces().size());
89+
assertEquals(1, metadata.getActivityMethods().size());
90+
assertEquals(
91+
"io.temporal.common.metadata.POJOActivityMethodMetadata",
92+
metadata.getActivityMethods().get(0).getClass().getName());
93+
assertEquals(
94+
"Execute", metadata.getActivityMethods().get(0).getActivityTypeName());
95+
assertEquals(
96+
"execute", metadata.getActivityMethods().get(0).getMethod().getName());
97+
});
98+
});
99+
}
100+
101+
@ComponentScan(
102+
excludeFilters =
103+
@ComponentScan.Filter(
104+
pattern = "io\\.temporal\\.spring\\.boot\\.autoconfigure\\.byworkername\\..*",
105+
type = FilterType.REGEX))
106+
public static class Configuration {}
107+
}

0 commit comments

Comments
 (0)