Skip to content

Commit 78e37a6

Browse files
authored
SpringBoot - Add registered workflow and activity impl info to workers template (#1986)
* add registered workflow and activity impl info to workers template Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * update to add object type returns for activity and workflow info Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * update to add javadoc to getRegisteredInfo method Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * added experimental annotation to getRegisteredInfo Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> --------- Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io>
1 parent f4a572a commit 78e37a6

File tree

1 file changed

+113
-6
lines changed
  • temporal-spring-boot-autoconfigure-alpha/src/main/java/io/temporal/spring/boot/autoconfigure/template

1 file changed

+113
-6
lines changed

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

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.base.Preconditions;
2424
import io.opentracing.Tracer;
2525
import io.temporal.client.WorkflowClient;
26+
import io.temporal.common.Experimental;
2627
import io.temporal.common.metadata.POJOWorkflowImplMetadata;
2728
import io.temporal.common.metadata.POJOWorkflowMethodMetadata;
2829
import io.temporal.spring.boot.ActivityImpl;
@@ -74,6 +75,7 @@ public class WorkersTemplate implements BeanFactoryAware, EnvironmentAware {
7475

7576
private WorkerFactory workerFactory;
7677
private Collection<Worker> workers;
78+
private final Map<String, RegisteredInfo> registeredInfo = new HashMap<>();
7779

7880
public WorkersTemplate(
7981
@Nonnull TemporalProperties properties,
@@ -111,6 +113,15 @@ public Collection<Worker> getWorkers() {
111113
return workers;
112114
}
113115

116+
/** Return information on registered workflow and activity types per task queue */
117+
@Experimental
118+
public Map<String, RegisteredInfo> getRegisteredInfo() {
119+
if (workers == null) {
120+
this.workers = createWorkers(getWorkerFactory());
121+
}
122+
return registeredInfo;
123+
}
124+
114125
WorkerFactory createWorkerFactory(WorkflowClient workflowClient) {
115126
if (testWorkflowEnvironment != null) {
116127
return testWorkflowEnvironment.getWorkerFactory();
@@ -170,7 +181,7 @@ private void configureWorkflowImplementationsByTaskQueue(
170181
worker = createNewWorker(taskQueue, null, workers);
171182
}
172183

173-
configureWorkflowImplementationAutoDiscovery(worker, clazz, null);
184+
configureWorkflowImplementationAutoDiscovery(worker, clazz, null, workers);
174185
}
175186
}
176187
}
@@ -197,7 +208,7 @@ private void configureActivityBeansByTaskQueue(
197208
}
198209

199210
configureActivityImplementationAutoDiscovery(
200-
worker, bean, beanName, targetClass, null);
211+
worker, bean, beanName, targetClass, null, workers);
201212
}
202213
}
203214
});
@@ -218,7 +229,7 @@ private void configureWorkflowImplementationsByWorkerName(
218229
+ clazz);
219230
}
220231

221-
configureWorkflowImplementationAutoDiscovery(worker, clazz, workerName);
232+
configureWorkflowImplementationAutoDiscovery(worker, clazz, workerName, workers);
222233
}
223234
}
224235
}
@@ -241,7 +252,7 @@ private void configureActivityBeansByWorkerName(
241252
}
242253

243254
configureActivityImplementationAutoDiscovery(
244-
worker, bean, beanName, targetClass, workerName);
255+
worker, bean, beanName, targetClass, workerName, workers);
245256
}
246257
}
247258
});
@@ -303,14 +314,21 @@ private void createWorkerFromAnExplicitConfig(
303314
AopUtils.getTargetClass(bean),
304315
taskQueue);
305316
worker.registerActivitiesImplementations(bean);
317+
addRegisteredActivityImpl(worker, beanName, bean.getClass().getName());
306318
});
307319
}
308320
}
309321

310322
private void configureActivityImplementationAutoDiscovery(
311-
Worker worker, Object bean, String beanName, Class<?> targetClass, String byWorkerName) {
323+
Worker worker,
324+
Object bean,
325+
String beanName,
326+
Class<?> targetClass,
327+
String byWorkerName,
328+
Workers workers) {
312329
try {
313330
worker.registerActivitiesImplementations(bean);
331+
addRegisteredActivityImpl(worker, beanName, bean.getClass().getName());
314332
if (log.isInfoEnabled()) {
315333
log.info(
316334
"Registering auto-discovered activity bean '{}' of class {} on a worker {}with a task queue '{}'",
@@ -334,7 +352,7 @@ private void configureActivityImplementationAutoDiscovery(
334352
}
335353

336354
private void configureWorkflowImplementationAutoDiscovery(
337-
Worker worker, Class<?> clazz, String byWorkerName) {
355+
Worker worker, Class<?> clazz, String byWorkerName, Workers workers) {
338356
try {
339357
configureWorkflowImplementation(worker, clazz);
340358
if (log.isInfoEnabled()) {
@@ -378,6 +396,7 @@ private <T> void configureWorkflowImplementation(Worker worker, Class<?> clazz)
378396
(Class<T>) workflowMethod.getWorkflowInterface(),
379397
() -> (T) beanFactory.createBean(clazz),
380398
workflowImplementationOptions);
399+
addRegisteredWorkflowImpl(worker, workflowMethod.getWorkflowInterface().getName());
381400
}
382401
}
383402

@@ -410,6 +429,94 @@ private Worker createNewWorker(
410429
return worker;
411430
}
412431

432+
private void addRegisteredWorkflowImpl(Worker worker, String workflowClass) {
433+
if (!registeredInfo.containsKey(worker.getTaskQueue())) {
434+
registeredInfo.put(
435+
worker.getTaskQueue(),
436+
new RegisteredInfo()
437+
.addWorkflowInfo(new RegisteredWorkflowInfo().addClassName(workflowClass)));
438+
} else {
439+
registeredInfo
440+
.get(worker.getTaskQueue())
441+
.getRegisteredWorkflowInfo()
442+
.add(new RegisteredWorkflowInfo().addClassName(workflowClass));
443+
}
444+
}
445+
446+
private void addRegisteredActivityImpl(Worker worker, String beanName, String beanClass) {
447+
if (!registeredInfo.containsKey(worker.getTaskQueue())) {
448+
registeredInfo.put(
449+
worker.getTaskQueue(),
450+
new RegisteredInfo()
451+
.addActivityInfo(
452+
new RegisteredActivityInfo().addBeanName(beanName).addClassName(beanClass)));
453+
} else {
454+
registeredInfo
455+
.get(worker.getTaskQueue())
456+
.getRegisteredActivityInfo()
457+
.add(new RegisteredActivityInfo().addBeanName(beanName).addClassName(beanClass));
458+
}
459+
}
460+
461+
public static class RegisteredInfo {
462+
private final List<RegisteredActivityInfo> registeredActivityInfo = new ArrayList<>();
463+
private final List<RegisteredWorkflowInfo> registeredWorkflowInfo = new ArrayList<>();
464+
465+
public RegisteredInfo addActivityInfo(RegisteredActivityInfo activityInfo) {
466+
registeredActivityInfo.add(activityInfo);
467+
return this;
468+
}
469+
470+
public RegisteredInfo addWorkflowInfo(RegisteredWorkflowInfo workflowInfo) {
471+
registeredWorkflowInfo.add(workflowInfo);
472+
return this;
473+
}
474+
475+
public List<RegisteredActivityInfo> getRegisteredActivityInfo() {
476+
return registeredActivityInfo;
477+
}
478+
479+
public List<RegisteredWorkflowInfo> getRegisteredWorkflowInfo() {
480+
return registeredWorkflowInfo;
481+
}
482+
}
483+
484+
public static class RegisteredActivityInfo {
485+
private String beanName;
486+
private String className;
487+
488+
public RegisteredActivityInfo addClassName(String className) {
489+
this.className = className;
490+
return this;
491+
}
492+
493+
public RegisteredActivityInfo addBeanName(String beanName) {
494+
this.beanName = beanName;
495+
return this;
496+
}
497+
498+
public String getClassName() {
499+
return className;
500+
}
501+
502+
public String getBeanName() {
503+
return beanName;
504+
}
505+
}
506+
507+
public static class RegisteredWorkflowInfo {
508+
private String className;
509+
510+
public RegisteredWorkflowInfo addClassName(String className) {
511+
this.className = className;
512+
return this;
513+
}
514+
515+
public String getClassName() {
516+
return className;
517+
}
518+
}
519+
413520
private static class Workers {
414521
private final Map<String, Worker> workersByName = new HashMap<>();
415522
private final Map<String, Worker> workersByTaskQueue = new HashMap<>();

0 commit comments

Comments
 (0)