Skip to content

Commit 6641b14

Browse files
Update
1 parent 5863dba commit 6641b14

3 files changed

Lines changed: 22 additions & 18 deletions

File tree

temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import io.temporal.internal.nexus.NexusStartWorkflowHelper;
1111
import io.temporal.workflow.Functions;
1212
import java.util.Objects;
13-
import java.util.function.Consumer;
1413

1514
/**
1615
* Nexus-aware client wrapping {@link WorkflowClient}. Provides methods for interacting with
@@ -73,33 +72,33 @@ public WorkflowClient getWorkflowClient() {
7372
}
7473

7574
/**
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.
75+
* Starts a workflow by invoking a returning method on a workflow stub. The client creates the
76+
* stub from the given class and options, then invokes the workflow method via the provided
77+
* function.
7978
*
80-
* <p>Example (returning):
79+
* <p>Example:
8180
*
8281
* <pre>{@code
8382
* client.startWorkflow(MyWorkflow.class, wf -> wf.run(input), options)
8483
* }</pre>
8584
*
86-
* <p>Example (void):
85+
* <p>For void-returning workflow methods, use a block lambda that returns null:
8786
*
8887
* <pre>{@code
89-
* client.startWorkflow(MyWorkflow.class, wf -> wf.execute(input), options)
88+
* client.startWorkflow(MyWorkflow.class, wf -> { wf.execute(input); return null; }, options)
9089
* }</pre>
9190
*
9291
* @param workflowClass the workflow interface class
93-
* @param workflowInvocation receives the workflow stub and calls exactly one workflow method
92+
* @param workflowMethod receives the workflow stub and calls exactly one workflow method
9493
* @param options workflow start options (must include workflowId)
9594
* @param <T> the workflow interface type
96-
* @param <R> the workflow return type (inferred from calling context)
95+
* @param <R> the workflow return type
9796
* @return an async {@link TemporalOperationResult} with the workflow-run operation token
9897
*/
9998
public <T, R> TemporalOperationResult<R> startWorkflow(
100-
Class<T> workflowClass, Consumer<T> workflowInvocation, WorkflowOptions options) {
99+
Class<T> workflowClass, Functions.Func1<T, R> workflowMethod, WorkflowOptions options) {
101100
T stub = client.newWorkflowStub(workflowClass, options);
102-
Functions.Proc bound = () -> workflowInvocation.accept(stub);
101+
Functions.Func<R> bound = () -> workflowMethod.apply(stub);
103102
return invokeAndReturn(WorkflowHandle.fromWorkflowMethod(bound));
104103
}
105104

@@ -120,7 +119,7 @@ public <R> TemporalOperationResult<R> startWorkflow(
120119
return invokeAndReturn(handle);
121120
}
122121

123-
private <R> TemporalOperationResult<R> invokeAndReturn(WorkflowHandle<?> handle) {
122+
private <R> TemporalOperationResult<R> invokeAndReturn(WorkflowHandle<R> handle) {
124123
NexusStartWorkflowResponse response =
125124
NexusStartWorkflowHelper.startWorkflowAndAttachLinks(
126125
operationContext,

temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
* }
3131
* }</pre>
3232
*
33-
* <p>The cancel behavior is overridable. By default, canceling an operation backed by a
34-
* workflow-run token cancels the underlying workflow.
33+
* <p>This class supports subclassing to customize cancel behavior. Override {@link
34+
* #cancelWorkflowRun} to change how workflow-run cancellations are handled. The {@link #start} and
35+
* {@link #cancel} methods should not be overridden — they contain the core dispatch logic.
3536
*
3637
* @param <T> the input type
3738
* @param <R> the result type
@@ -53,17 +54,18 @@ TemporalOperationResult<R> apply(
5354

5455
private final StartFunction<T, R> startFunction;
5556

56-
private TemporalOperationHandler(StartFunction<T, R> startFunction) {
57+
protected TemporalOperationHandler(StartFunction<T, R> startFunction) {
5758
this.startFunction = startFunction;
5859
}
5960

6061
/**
61-
* Creates an {@link OperationHandler} from a start function.
62+
* Creates a {@link TemporalOperationHandler} from a start function. Subclass and override {@link
63+
* #cancelWorkflowRun} to customize cancel behavior.
6264
*
6365
* @param startFunction the function to invoke on start operation requests
6466
* @return an operation handler backed by the given start function
6567
*/
66-
public static <T, R> OperationHandler<T, R> from(StartFunction<T, R> startFunction) {
68+
public static <T, R> TemporalOperationHandler<T, R> from(StartFunction<T, R> startFunction) {
6769
return new TemporalOperationHandler<>(startFunction);
6870
}
6971

temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ public OperationHandler<String, Void> operation() {
6464
(context, client, input) ->
6565
client.startWorkflow(
6666
TestMultiArgWorkflowFunctions.TestNoArgsWorkflowProc.class,
67-
wf -> wf.proc(),
67+
wf -> {
68+
wf.proc();
69+
return null;
70+
},
6871
WorkflowOptions.newBuilder()
6972
.setWorkflowId(
7073
"generic-handler-proc-"

0 commit comments

Comments
 (0)