|
| 1 | +package io.temporal.nexus; |
| 2 | + |
| 3 | +import io.nexusrpc.handler.OperationContext; |
| 4 | +import io.nexusrpc.handler.OperationStartDetails; |
| 5 | +import io.temporal.client.WorkflowClient; |
| 6 | +import io.temporal.client.WorkflowOptions; |
| 7 | +import io.temporal.client.WorkflowStub; |
| 8 | +import io.temporal.common.Experimental; |
| 9 | +import io.temporal.internal.client.NexusStartWorkflowResponse; |
| 10 | +import io.temporal.internal.nexus.NexusStartWorkflowHelper; |
| 11 | +import io.temporal.workflow.Functions; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.function.Consumer; |
| 14 | + |
| 15 | +/** |
| 16 | + * Nexus-aware client wrapping {@link WorkflowClient}. Provides methods for interacting with |
| 17 | + * Temporal workflows from within a Nexus operation handler. |
| 18 | + * |
| 19 | + * <p>Obtained via the {@link TemporalOperationHandler.StartFunction} parameter. The client creates |
| 20 | + * workflow stubs internally — users pass the workflow class, a lambda that calls the workflow |
| 21 | + * method, and workflow options. |
| 22 | + * |
| 23 | + * <p>Usage example: |
| 24 | + * |
| 25 | + * <pre>{@code |
| 26 | + * @OperationImpl |
| 27 | + * public OperationHandler<OrderInput, OrderResult> createOrder() { |
| 28 | + * return TemporalOperationHandler.from((context, client, input) -> { |
| 29 | + * return client.startWorkflow( |
| 30 | + * OrderWorkflow.class, |
| 31 | + * wf -> wf.processOrder(input), |
| 32 | + * WorkflowOptions.newBuilder() |
| 33 | + * .setWorkflowId("order-" + context.getRequestId()) |
| 34 | + * .build()); |
| 35 | + * }); |
| 36 | + * } |
| 37 | + * }</pre> |
| 38 | + * |
| 39 | + * <p>For advanced use cases, the underlying {@link WorkflowClient} can be accessed via {@link |
| 40 | + * #getWorkflowClient()}. For example, to send a signal and return a synchronous result: |
| 41 | + * |
| 42 | + * <pre>{@code |
| 43 | + * @OperationImpl |
| 44 | + * public OperationHandler<CancelOrderInput, Void> cancelOrder() { |
| 45 | + * return TemporalOperationHandler.from((context, client, input) -> { |
| 46 | + * client.getWorkflowClient() |
| 47 | + * .newUntypedWorkflowStub("order-" + input.getOrderId()) |
| 48 | + * .signal("requestCancellation", input); |
| 49 | + * return TemporalOperationResult.sync(null); |
| 50 | + * }); |
| 51 | + * } |
| 52 | + * }</pre> |
| 53 | + */ |
| 54 | +@Experimental |
| 55 | +public final class TemporalNexusClient { |
| 56 | + |
| 57 | + private final WorkflowClient client; |
| 58 | + private final OperationContext operationContext; |
| 59 | + private final OperationStartDetails operationStartDetails; |
| 60 | + |
| 61 | + TemporalNexusClient( |
| 62 | + WorkflowClient client, |
| 63 | + OperationContext operationContext, |
| 64 | + OperationStartDetails operationStartDetails) { |
| 65 | + this.client = Objects.requireNonNull(client); |
| 66 | + this.operationContext = Objects.requireNonNull(operationContext); |
| 67 | + this.operationStartDetails = Objects.requireNonNull(operationStartDetails); |
| 68 | + } |
| 69 | + |
| 70 | + /** Returns the underlying {@link WorkflowClient} for advanced use cases. */ |
| 71 | + public WorkflowClient getWorkflowClient() { |
| 72 | + return client; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Starts a workflow by invoking a method on a workflow stub. The client creates the stub from the |
| 77 | + * given class and options, then passes it to the provided consumer which should call exactly one |
| 78 | + * workflow method. Works for both returning and void workflow methods. |
| 79 | + * |
| 80 | + * <p>Example (returning): |
| 81 | + * |
| 82 | + * <pre>{@code |
| 83 | + * client.startWorkflow(MyWorkflow.class, wf -> wf.run(input), options) |
| 84 | + * }</pre> |
| 85 | + * |
| 86 | + * <p>Example (void): |
| 87 | + * |
| 88 | + * <pre>{@code |
| 89 | + * client.startWorkflow(MyWorkflow.class, wf -> wf.execute(input), options) |
| 90 | + * }</pre> |
| 91 | + * |
| 92 | + * @param workflowClass the workflow interface class |
| 93 | + * @param workflowInvocation receives the workflow stub and calls exactly one workflow method |
| 94 | + * @param options workflow start options (must include workflowId) |
| 95 | + * @param <T> the workflow interface type |
| 96 | + * @param <R> the workflow return type (inferred from calling context) |
| 97 | + * @return an async {@link TemporalOperationResult} with the workflow-run operation token |
| 98 | + */ |
| 99 | + public <T, R> TemporalOperationResult<R> startWorkflow( |
| 100 | + Class<T> workflowClass, Consumer<T> workflowInvocation, WorkflowOptions options) { |
| 101 | + T stub = client.newWorkflowStub(workflowClass, options); |
| 102 | + Functions.Proc bound = () -> workflowInvocation.accept(stub); |
| 103 | + return invokeAndReturn(WorkflowHandle.fromWorkflowMethod(bound)); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Starts a workflow using an untyped workflow type name. |
| 108 | + * |
| 109 | + * @param workflowType the workflow type name string |
| 110 | + * @param resultClass the expected result class |
| 111 | + * @param args workflow arguments |
| 112 | + * @param options workflow start options (must include workflowId) |
| 113 | + * @param <R> the workflow return type |
| 114 | + * @return an async {@link TemporalOperationResult} with the workflow-run operation token |
| 115 | + */ |
| 116 | + public <R> TemporalOperationResult<R> startWorkflow( |
| 117 | + String workflowType, Class<R> resultClass, Object[] args, WorkflowOptions options) { |
| 118 | + WorkflowStub stub = client.newUntypedWorkflowStub(workflowType, options); |
| 119 | + WorkflowHandle<R> handle = WorkflowHandle.fromWorkflowStub(stub, resultClass, args); |
| 120 | + return invokeAndReturn(handle); |
| 121 | + } |
| 122 | + |
| 123 | + private <R> TemporalOperationResult<R> invokeAndReturn(WorkflowHandle<?> handle) { |
| 124 | + NexusStartWorkflowResponse response = |
| 125 | + NexusStartWorkflowHelper.startWorkflowAndAttachLinks( |
| 126 | + operationContext, |
| 127 | + operationStartDetails, |
| 128 | + request -> handle.getInvoker().invoke(request)); |
| 129 | + return TemporalOperationResult.async(response.getOperationToken()); |
| 130 | + } |
| 131 | +} |
0 commit comments