Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package io.temporal.spring.boot.autoconfigure.template;

import static io.temporal.serviceclient.CheckedExceptionWrapper.wrap;

import com.google.common.base.Preconditions;
import io.nexusrpc.ServiceDefinition;
import io.nexusrpc.handler.ServiceImplInstance;
import io.opentracing.Tracer;
import io.temporal.client.WorkflowClient;
import io.temporal.common.Experimental;
import io.temporal.common.converter.EncodedValues;
import io.temporal.common.metadata.POJOActivityImplMetadata;
import io.temporal.common.metadata.POJOWorkflowImplMetadata;
import io.temporal.common.metadata.POJOWorkflowMethodMetadata;
import io.temporal.internal.common.env.ReflectionUtils;
import io.temporal.internal.sync.POJOWorkflowImplementationFactory;
import io.temporal.spring.boot.ActivityImpl;
import io.temporal.spring.boot.NexusServiceImpl;
Expand All @@ -17,15 +21,11 @@
import io.temporal.spring.boot.autoconfigure.properties.NamespaceProperties;
import io.temporal.spring.boot.autoconfigure.properties.WorkerProperties;
import io.temporal.worker.*;
import io.temporal.workflow.DynamicWorkflow;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.lang.reflect.Method;
import java.util.*;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -492,6 +492,48 @@ private void configureWorkflowImplementationAutoDiscovery(

@SuppressWarnings("unchecked")
private <T> void configureWorkflowImplementation(Worker worker, Class<?> clazz) {
// Handle dynamic workflows
if (DynamicWorkflow.class.isAssignableFrom(clazz)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figure lots of this comes from existing code? I'm guessing there's not a reasonable way to reuse (i.e. maybe we don't want to reference internal packages of another JAR)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we don't want to reference internal, it will break Java modules.

try {
Method executeMethod = clazz.getMethod("execute", EncodedValues.class);
Optional<Constructor<?>> ctor =
ReflectionUtils.getWorkflowInitConstructor(
clazz, Collections.singletonList(executeMethod));
WorkflowImplementationOptions workflowImplementationOptions =
new WorkflowImplementationOptionsTemplate(workflowImplementationCustomizer)
.createWorkflowImplementationOptions();
worker.registerWorkflowImplementationFactory(
DynamicWorkflow.class,
(encodedValues) -> {
if (ctor.isPresent()) {
try {
return (DynamicWorkflow) ctor.get().newInstance(encodedValues);
} catch (InstantiationException
| IllegalAccessException
| InvocationTargetException e) {
throw wrap(e);
}
} else {
try {
return (DynamicWorkflow) clazz.getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException
| InstantiationException
| IllegalAccessException
| InvocationTargetException e) {
// Error to fail workflow task as this can be fixed by a new deployment.
throw new Error(
"Failure instantiating workflow implementation class " + clazz.getName(), e);
}
}
},
workflowImplementationOptions);
return;
} catch (NoSuchMethodException e) {
throw new BeanDefinitionValidationException(
"Dynamic workflow implementation doesn't have execute method: " + clazz, e);
}
}

POJOWorkflowImplMetadata workflowMetadata =
POJOWorkflowImplMetadata.newInstanceForWorkflowFactory(clazz);
List<POJOWorkflowMethodMetadata> workflowMethods = workflowMetadata.getWorkflowMethods();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.temporal.api.nexus.v1.Endpoint;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import io.temporal.client.WorkflowStub;
import io.temporal.spring.boot.autoconfigure.bytaskqueue.TestWorkflow;
import io.temporal.testing.TestWorkflowEnvironment;
import org.junit.jupiter.api.*;
Expand Down Expand Up @@ -44,6 +45,12 @@ public void testAutoDiscovery() {
workflowClient.newWorkflowStub(
TestWorkflow.class, WorkflowOptions.newBuilder().setTaskQueue("UnitTest").build());
testWorkflow.execute("nexus");

WorkflowStub dynamicStub =
workflowClient.newUntypedWorkflowStub(
"DynamicWorkflow", WorkflowOptions.newBuilder().setTaskQueue("UnitTest").build());
dynamicStub.start();
Assertions.assertEquals("hello from dynamic workflow", dynamicStub.getResult(String.class));
}

@ComponentScan(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.temporal.spring.boot.autoconfigure.byworkername;

import io.temporal.common.converter.EncodedValues;
import io.temporal.spring.boot.WorkflowImpl;
import io.temporal.workflow.DynamicWorkflow;

@WorkflowImpl(workers = "mainWorker")
public class TestDynamicWorkflowImpl implements DynamicWorkflow {
@Override
public Object execute(EncodedValues args) {
return "hello from dynamic workflow";
}
}
Loading