|
1 | 1 | package io.temporal.spring.boot.autoconfigure.template;
|
2 | 2 |
|
| 3 | +import static io.temporal.serviceclient.CheckedExceptionWrapper.wrap; |
| 4 | + |
3 | 5 | import com.google.common.base.Preconditions;
|
4 | 6 | import io.nexusrpc.ServiceDefinition;
|
5 | 7 | import io.nexusrpc.handler.ServiceImplInstance;
|
6 | 8 | import io.opentracing.Tracer;
|
7 | 9 | import io.temporal.client.WorkflowClient;
|
8 | 10 | import io.temporal.common.Experimental;
|
| 11 | +import io.temporal.common.converter.EncodedValues; |
9 | 12 | import io.temporal.common.metadata.POJOActivityImplMetadata;
|
10 | 13 | import io.temporal.common.metadata.POJOWorkflowImplMetadata;
|
11 | 14 | import io.temporal.common.metadata.POJOWorkflowMethodMetadata;
|
| 15 | +import io.temporal.internal.common.env.ReflectionUtils; |
12 | 16 | import io.temporal.internal.sync.POJOWorkflowImplementationFactory;
|
13 | 17 | import io.temporal.spring.boot.ActivityImpl;
|
14 | 18 | import io.temporal.spring.boot.NexusServiceImpl;
|
|
17 | 21 | import io.temporal.spring.boot.autoconfigure.properties.NamespaceProperties;
|
18 | 22 | import io.temporal.spring.boot.autoconfigure.properties.WorkerProperties;
|
19 | 23 | import io.temporal.worker.*;
|
| 24 | +import io.temporal.workflow.DynamicWorkflow; |
20 | 25 | import java.lang.reflect.Constructor;
|
21 | 26 | import java.lang.reflect.InvocationTargetException;
|
22 |
| -import java.util.ArrayList; |
23 |
| -import java.util.Collection; |
24 |
| -import java.util.HashMap; |
25 |
| -import java.util.HashSet; |
26 |
| -import java.util.List; |
27 |
| -import java.util.Map; |
28 |
| -import java.util.Set; |
| 27 | +import java.lang.reflect.Method; |
| 28 | +import java.util.*; |
29 | 29 | import javax.annotation.Nonnull;
|
30 | 30 | import javax.annotation.Nullable;
|
31 | 31 | import org.slf4j.Logger;
|
@@ -492,6 +492,48 @@ private void configureWorkflowImplementationAutoDiscovery(
|
492 | 492 |
|
493 | 493 | @SuppressWarnings("unchecked")
|
494 | 494 | private <T> void configureWorkflowImplementation(Worker worker, Class<?> clazz) {
|
| 495 | + // Handle dynamic workflows |
| 496 | + if (DynamicWorkflow.class.isAssignableFrom(clazz)) { |
| 497 | + try { |
| 498 | + Method executeMethod = clazz.getMethod("execute", EncodedValues.class); |
| 499 | + Optional<Constructor<?>> ctor = |
| 500 | + ReflectionUtils.getWorkflowInitConstructor( |
| 501 | + clazz, Collections.singletonList(executeMethod)); |
| 502 | + WorkflowImplementationOptions workflowImplementationOptions = |
| 503 | + new WorkflowImplementationOptionsTemplate(workflowImplementationCustomizer) |
| 504 | + .createWorkflowImplementationOptions(); |
| 505 | + worker.registerWorkflowImplementationFactory( |
| 506 | + DynamicWorkflow.class, |
| 507 | + (encodedValues) -> { |
| 508 | + if (ctor.isPresent()) { |
| 509 | + try { |
| 510 | + return (DynamicWorkflow) ctor.get().newInstance(encodedValues); |
| 511 | + } catch (InstantiationException |
| 512 | + | IllegalAccessException |
| 513 | + | InvocationTargetException e) { |
| 514 | + throw wrap(e); |
| 515 | + } |
| 516 | + } else { |
| 517 | + try { |
| 518 | + return (DynamicWorkflow) clazz.getDeclaredConstructor().newInstance(); |
| 519 | + } catch (NoSuchMethodException |
| 520 | + | InstantiationException |
| 521 | + | IllegalAccessException |
| 522 | + | InvocationTargetException e) { |
| 523 | + // Error to fail workflow task as this can be fixed by a new deployment. |
| 524 | + throw new Error( |
| 525 | + "Failure instantiating workflow implementation class " + clazz.getName(), e); |
| 526 | + } |
| 527 | + } |
| 528 | + }, |
| 529 | + workflowImplementationOptions); |
| 530 | + return; |
| 531 | + } catch (NoSuchMethodException e) { |
| 532 | + throw new BeanDefinitionValidationException( |
| 533 | + "Dynamic workflow implementation doesn't have execute method: " + clazz, e); |
| 534 | + } |
| 535 | + } |
| 536 | + |
495 | 537 | POJOWorkflowImplMetadata workflowMetadata =
|
496 | 538 | POJOWorkflowImplMetadata.newInstanceForWorkflowFactory(clazz);
|
497 | 539 | List<POJOWorkflowMethodMetadata> workflowMethods = workflowMetadata.getWorkflowMethods();
|
|
0 commit comments