Skip to content

Commit

Permalink
SpringBoot - add workflow and activity metadata to RegisteredInfo (#1995
Browse files Browse the repository at this point in the history
)

* SpringBoot - add workflow and activity metadata to RegisteredInfo

Signed-off-by: Tihomir Surdilovic <[email protected]>

* marked register classes as experimental

Signed-off-by: Tihomir Surdilovic <[email protected]>

* adding test

Signed-off-by: Tihomir Surdilovic <[email protected]>

---------

Signed-off-by: Tihomir Surdilovic <[email protected]>
  • Loading branch information
tsurdilo authored Apr 4, 2024
1 parent e603fd8 commit 26a8595
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.opentracing.Tracer;
import io.temporal.client.WorkflowClient;
import io.temporal.common.Experimental;
import io.temporal.common.metadata.POJOActivityImplMetadata;
import io.temporal.common.metadata.POJOWorkflowImplMetadata;
import io.temporal.common.metadata.POJOWorkflowMethodMetadata;
import io.temporal.spring.boot.ActivityImpl;
Expand Down Expand Up @@ -314,7 +315,10 @@ private void createWorkerFromAnExplicitConfig(
AopUtils.getTargetClass(bean),
taskQueue);
worker.registerActivitiesImplementations(bean);
addRegisteredActivityImpl(worker, beanName, bean.getClass().getName());
POJOActivityImplMetadata activityImplMetadata =
POJOActivityImplMetadata.newInstance(AopUtils.getTargetClass(bean));
addRegisteredActivityImpl(
worker, beanName, bean.getClass().getName(), activityImplMetadata);
});
}
}
Expand All @@ -328,7 +332,9 @@ private void configureActivityImplementationAutoDiscovery(
Workers workers) {
try {
worker.registerActivitiesImplementations(bean);
addRegisteredActivityImpl(worker, beanName, bean.getClass().getName());
POJOActivityImplMetadata activityImplMetadata =
POJOActivityImplMetadata.newInstance(AopUtils.getTargetClass(bean));
addRegisteredActivityImpl(worker, beanName, bean.getClass().getName(), activityImplMetadata);
if (log.isInfoEnabled()) {
log.info(
"Registering auto-discovered activity bean '{}' of class {} on a worker {}with a task queue '{}'",
Expand Down Expand Up @@ -396,7 +402,8 @@ private <T> void configureWorkflowImplementation(Worker worker, Class<?> clazz)
(Class<T>) workflowMethod.getWorkflowInterface(),
() -> (T) beanFactory.createBean(clazz),
workflowImplementationOptions);
addRegisteredWorkflowImpl(worker, workflowMethod.getWorkflowInterface().getName());
addRegisteredWorkflowImpl(
worker, workflowMethod.getWorkflowInterface().getName(), workflowMetadata);
}
}

Expand Down Expand Up @@ -429,32 +436,42 @@ private Worker createNewWorker(
return worker;
}

private void addRegisteredWorkflowImpl(Worker worker, String workflowClass) {
private void addRegisteredWorkflowImpl(
Worker worker, String workflowClass, POJOWorkflowImplMetadata metadata) {
if (!registeredInfo.containsKey(worker.getTaskQueue())) {
registeredInfo.put(
worker.getTaskQueue(),
new RegisteredInfo()
.addWorkflowInfo(new RegisteredWorkflowInfo().addClassName(workflowClass)));
.addWorkflowInfo(
new RegisteredWorkflowInfo().addClassName(workflowClass).addMetadata(metadata)));
} else {
registeredInfo
.get(worker.getTaskQueue())
.getRegisteredWorkflowInfo()
.add(new RegisteredWorkflowInfo().addClassName(workflowClass));
.add(new RegisteredWorkflowInfo().addClassName(workflowClass).addMetadata(metadata));
}
}

private void addRegisteredActivityImpl(Worker worker, String beanName, String beanClass) {
private void addRegisteredActivityImpl(
Worker worker, String beanName, String beanClass, POJOActivityImplMetadata metadata) {
if (!registeredInfo.containsKey(worker.getTaskQueue())) {
registeredInfo.put(
worker.getTaskQueue(),
new RegisteredInfo()
.addActivityInfo(
new RegisteredActivityInfo().addBeanName(beanName).addClassName(beanClass)));
new RegisteredActivityInfo()
.addBeanName(beanName)
.addClassName(beanClass)
.addMetadata(metadata)));
} else {
registeredInfo
.get(worker.getTaskQueue())
.getRegisteredActivityInfo()
.add(new RegisteredActivityInfo().addBeanName(beanName).addClassName(beanClass));
.add(
new RegisteredActivityInfo()
.addBeanName(beanName)
.addClassName(beanClass)
.addMetadata(metadata));
}
}

Expand All @@ -481,9 +498,11 @@ public List<RegisteredWorkflowInfo> getRegisteredWorkflowInfo() {
}
}

@Experimental
public static class RegisteredActivityInfo {
private String beanName;
private String className;
private POJOActivityImplMetadata metadata;

public RegisteredActivityInfo addClassName(String className) {
this.className = className;
Expand All @@ -495,26 +514,46 @@ public RegisteredActivityInfo addBeanName(String beanName) {
return this;
}

public RegisteredActivityInfo addMetadata(POJOActivityImplMetadata metadata) {
this.metadata = metadata;
return this;
}

public String getClassName() {
return className;
}

public String getBeanName() {
return beanName;
}

public POJOActivityImplMetadata getMetadata() {
return metadata;
}
}

@Experimental
public static class RegisteredWorkflowInfo {
private String className;
private POJOWorkflowImplMetadata metadata;

public RegisteredWorkflowInfo addClassName(String className) {
this.className = className;
return this;
}

public RegisteredWorkflowInfo addMetadata(POJOWorkflowImplMetadata metadata) {
this.metadata = metadata;
return this;
}

public String getClassName() {
return className;
}

public POJOWorkflowImplMetadata getMetadata() {
return metadata;
}
}

private static class Workers {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this material except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.temporal.spring.boot.autoconfigure;

import static org.junit.jupiter.api.Assertions.*;

import io.temporal.common.metadata.POJOActivityImplMetadata;
import io.temporal.common.metadata.POJOWorkflowImplMetadata;
import io.temporal.spring.boot.autoconfigure.template.WorkersTemplate;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.Timeout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest(classes = RegisteredInfoTest.Configuration.class)
@ActiveProfiles(profiles = "auto-discovery-by-worker-name")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class RegisteredInfoTest {

@Autowired ConfigurableApplicationContext applicationContext;

@Autowired private WorkersTemplate workersTemplate;

@BeforeEach
void setUp() {
applicationContext.start();
}

@Test
@Timeout(value = 10)
public void testRegisteredInfo() {
assertNotNull(workersTemplate);
assertNotNull(workersTemplate.getRegisteredInfo());
Map<String, WorkersTemplate.RegisteredInfo> registeredInfoMap =
workersTemplate.getRegisteredInfo();

assertEquals(1, registeredInfoMap.size());
registeredInfoMap.forEach(
(taskQueue, info) -> {
assertEquals("UnitTest", taskQueue);
info.getRegisteredWorkflowInfo()
.forEach(
(workflowInfo) -> {
assertNotNull(workflowInfo);
assertEquals(
"io.temporal.spring.boot.autoconfigure.byworkername.TestWorkflow",
workflowInfo.getClassName());
POJOWorkflowImplMetadata metadata = workflowInfo.getMetadata();
assertNotNull(metadata);
assertEquals(1, metadata.getWorkflowMethods().size());
assertEquals(1, metadata.getWorkflowInterfaces().size());
assertEquals(0, metadata.getSignalMethods().size());
});

info.getRegisteredActivityInfo()
.forEach(
(activityInfo) -> {
assertEquals(
"io.temporal.spring.boot.autoconfigure.bytaskqueue.TestActivityImpl",
activityInfo.getClassName());
assertEquals("TestActivityImpl", activityInfo.getBeanName());
POJOActivityImplMetadata metadata = activityInfo.getMetadata();
assertEquals(1, metadata.getActivityInterfaces().size());
assertEquals(1, metadata.getActivityMethods().size());
assertEquals(
"io.temporal.common.metadata.POJOActivityMethodMetadata",
metadata.getActivityMethods().get(0).getClass().getName());
assertEquals(
"Execute", metadata.getActivityMethods().get(0).getActivityTypeName());
assertEquals(
"execute", metadata.getActivityMethods().get(0).getMethod().getName());
});
});
}

@ComponentScan(
excludeFilters =
@ComponentScan.Filter(
pattern = "io\\.temporal\\.spring\\.boot\\.autoconfigure\\.byworkername\\..*",
type = FilterType.REGEX))
public static class Configuration {}
}

0 comments on commit 26a8595

Please sign in to comment.