|
20 | 20 |
|
21 | 21 | package io.temporal.spring.boot.autoconfigure.template; |
22 | 22 |
|
| 23 | +import static io.temporal.serviceclient.CheckedExceptionWrapper.wrap; |
| 24 | + |
23 | 25 | import com.google.common.base.Preconditions; |
24 | 26 | import io.nexusrpc.ServiceDefinition; |
25 | 27 | import io.nexusrpc.handler.ServiceImplInstance; |
26 | 28 | import io.opentracing.Tracer; |
27 | 29 | import io.temporal.client.WorkflowClient; |
28 | 30 | import io.temporal.common.Experimental; |
| 31 | +import io.temporal.common.converter.EncodedValues; |
29 | 32 | import io.temporal.common.metadata.POJOActivityImplMetadata; |
30 | 33 | import io.temporal.common.metadata.POJOWorkflowImplMetadata; |
31 | 34 | import io.temporal.common.metadata.POJOWorkflowMethodMetadata; |
| 35 | +import io.temporal.internal.common.env.ReflectionUtils; |
32 | 36 | import io.temporal.internal.sync.POJOWorkflowImplementationFactory; |
33 | 37 | import io.temporal.spring.boot.ActivityImpl; |
34 | 38 | import io.temporal.spring.boot.NexusServiceImpl; |
|
37 | 41 | import io.temporal.spring.boot.autoconfigure.properties.NamespaceProperties; |
38 | 42 | import io.temporal.spring.boot.autoconfigure.properties.WorkerProperties; |
39 | 43 | import io.temporal.worker.*; |
| 44 | +import io.temporal.workflow.DynamicWorkflow; |
40 | 45 | import java.lang.reflect.Constructor; |
41 | 46 | import java.lang.reflect.InvocationTargetException; |
42 | | -import java.util.ArrayList; |
43 | | -import java.util.Collection; |
44 | | -import java.util.HashMap; |
45 | | -import java.util.HashSet; |
46 | | -import java.util.List; |
47 | | -import java.util.Map; |
48 | | -import java.util.Set; |
| 47 | +import java.lang.reflect.Method; |
| 48 | +import java.util.*; |
49 | 49 | import javax.annotation.Nonnull; |
50 | 50 | import javax.annotation.Nullable; |
51 | 51 | import org.slf4j.Logger; |
@@ -512,6 +512,48 @@ private void configureWorkflowImplementationAutoDiscovery( |
512 | 512 |
|
513 | 513 | @SuppressWarnings("unchecked") |
514 | 514 | private <T> void configureWorkflowImplementation(Worker worker, Class<?> clazz) { |
| 515 | + // Handle dynamic workflows |
| 516 | + if (DynamicWorkflow.class.isAssignableFrom(clazz)) { |
| 517 | + try { |
| 518 | + Method executeMethod = clazz.getMethod("execute", EncodedValues.class); |
| 519 | + Optional<Constructor<?>> ctor = |
| 520 | + ReflectionUtils.getWorkflowInitConstructor( |
| 521 | + clazz, Collections.singletonList(executeMethod)); |
| 522 | + WorkflowImplementationOptions workflowImplementationOptions = |
| 523 | + new WorkflowImplementationOptionsTemplate(workflowImplementationCustomizer) |
| 524 | + .createWorkflowImplementationOptions(); |
| 525 | + worker.registerWorkflowImplementationFactory( |
| 526 | + DynamicWorkflow.class, |
| 527 | + (encodedValues) -> { |
| 528 | + if (ctor.isPresent()) { |
| 529 | + try { |
| 530 | + return (DynamicWorkflow) ctor.get().newInstance(encodedValues); |
| 531 | + } catch (InstantiationException |
| 532 | + | IllegalAccessException |
| 533 | + | InvocationTargetException e) { |
| 534 | + throw wrap(e); |
| 535 | + } |
| 536 | + } else { |
| 537 | + try { |
| 538 | + return (DynamicWorkflow) clazz.getDeclaredConstructor().newInstance(); |
| 539 | + } catch (NoSuchMethodException |
| 540 | + | InstantiationException |
| 541 | + | IllegalAccessException |
| 542 | + | InvocationTargetException e) { |
| 543 | + // Error to fail workflow task as this can be fixed by a new deployment. |
| 544 | + throw new Error( |
| 545 | + "Failure instantiating workflow implementation class " + clazz.getName(), e); |
| 546 | + } |
| 547 | + } |
| 548 | + }, |
| 549 | + workflowImplementationOptions); |
| 550 | + return; |
| 551 | + } catch (NoSuchMethodException e) { |
| 552 | + throw new BeanDefinitionValidationException( |
| 553 | + "Dynamic workflow implementation doesn't have execute method: " + clazz, e); |
| 554 | + } |
| 555 | + } |
| 556 | + |
515 | 557 | POJOWorkflowImplMetadata workflowMetadata = |
516 | 558 | POJOWorkflowImplMetadata.newInstanceForWorkflowFactory(clazz); |
517 | 559 | List<POJOWorkflowMethodMetadata> workflowMethods = workflowMetadata.getWorkflowMethods(); |
|
0 commit comments