diff --git a/temporal-sdk/src/main/java/io/temporal/internal/sync/POJOWorkflowImplementationFactory.java b/temporal-sdk/src/main/java/io/temporal/internal/sync/POJOWorkflowImplementationFactory.java index 60003de893..fb20638296 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/sync/POJOWorkflowImplementationFactory.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/sync/POJOWorkflowImplementationFactory.java @@ -77,6 +77,8 @@ public final class POJOWorkflowImplementationFactory implements ReplayWorkflowFa private Functions.Func1 dynamicWorkflowImplementationFactory; + @Nullable private WorkflowImplementationOptions dynamicWorkflowImplementationOptions; + private final Map implementationOptions = Collections.synchronizedMap(new HashMap<>()); @@ -136,6 +138,7 @@ public void addWorkflowImplementationFactory( } dynamicWorkflowImplementationFactory = (Functions.Func1) factory; + dynamicWorkflowImplementationOptions = options; return; } workflowInstanceFactories.put(clazz, factory); @@ -213,6 +216,7 @@ private void registerWorkflowImplementationType( } } }; + dynamicWorkflowImplementationOptions = options; return; } catch (NoSuchMethodException e) { throw new IllegalArgumentException( @@ -285,8 +289,11 @@ private SyncWorkflowDefinition getWorkflowDefinition( public ReplayWorkflow getWorkflow( WorkflowType workflowType, WorkflowExecution workflowExecution) { SyncWorkflowDefinition workflow = getWorkflowDefinition(workflowType, workflowExecution); + boolean isDynamicWorkflow = !workflowDefinitions.containsKey(workflowType.getName()); WorkflowImplementationOptions workflowImplementationOptions = - implementationOptions.get(workflowType.getName()); + isDynamicWorkflow + ? dynamicWorkflowImplementationOptions + : implementationOptions.get(workflowType.getName()); DataConverter dataConverterWithWorkflowContext = dataConverter.withContext( new WorkflowSerializationContext(namespace, workflowExecution.getWorkflowId())); diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/DynamicWorkflowTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/DynamicWorkflowTest.java index 2cb0d39ec3..34e770b2f8 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/DynamicWorkflowTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/DynamicWorkflowTest.java @@ -1,7 +1,6 @@ package io.temporal.workflow; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import io.temporal.activity.Activity; import io.temporal.activity.ActivityOptions; @@ -14,6 +13,7 @@ import io.temporal.failure.ApplicationFailure; import io.temporal.testing.TestWorkflowEnvironment; import io.temporal.testing.internal.SDKTestWorkflowRule; +import io.temporal.worker.WorkflowImplementationOptions; import java.time.Duration; import java.util.ArrayList; import java.util.List; @@ -74,6 +74,13 @@ public Object execute(EncodedValues args) { } } + public static class FailingDynamicWorkflowImpl implements DynamicWorkflow { + @Override + public Object execute(EncodedValues args) { + throw new NullPointerException("simulated"); + } + } + @Test public void testDynamicWorkflow() { TestWorkflowEnvironment testEnvironment = testWorkflowRule.getTestEnvironment(); @@ -124,4 +131,41 @@ public void testDynamicWorkflowFailure() { workflow.start("startArg0", true /* fail */); workflow.getResult(String.class); } + + @Test(expected = WorkflowFailedException.class) + public void testDynamicWorkflowFailureTypes() { + TestWorkflowEnvironment testEnvironment = testWorkflowRule.getTestEnvironment(); + testEnvironment + .getWorkerFactory() + .getWorker(testWorkflowRule.getTaskQueue()) + .registerWorkflowImplementationTypes( + WorkflowImplementationOptions.newBuilder() + .setFailWorkflowExceptionTypes(NullPointerException.class) + .build(), + FailingDynamicWorkflowImpl.class); + testEnvironment.start(); + + WorkflowStub workflow = testWorkflowRule.newUntypedWorkflowStub("workflowFoo"); + workflow.start(); + workflow.getResult(String.class); + } + + @Test(expected = WorkflowFailedException.class) + public void testDynamicWorkflowFactoryFailureTypes() { + TestWorkflowEnvironment testEnvironment = testWorkflowRule.getTestEnvironment(); + testEnvironment + .getWorkerFactory() + .getWorker(testWorkflowRule.getTaskQueue()) + .registerWorkflowImplementationFactory( + FailingDynamicWorkflowImpl.class, + FailingDynamicWorkflowImpl::new, + WorkflowImplementationOptions.newBuilder() + .setFailWorkflowExceptionTypes(NullPointerException.class) + .build()); + testEnvironment.start(); + + WorkflowStub workflow = testWorkflowRule.newUntypedWorkflowStub("workflowFoo"); + workflow.start(); + workflow.getResult(String.class); + } }