Skip to content

Commit daaf8c4

Browse files
Add CancellationType to nexus cancellation sample (temporalio#741)
Add CancellationType to nexus cancellation sample
1 parent a650078 commit daaf8c4

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

โ€Žcore/src/main/java/io/temporal/samples/nexuscancellation/README.MDโ€Ž

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Nexus Cancellation
22

3-
This sample shows how to cancel a Nexus operation from a caller workflow.
3+
This sample shows how to cancel a Nexus operation from a caller workflow and specify a cancellation type. In this sample we will show using the `WAIT_REQUESTED` cancellation type, which allows the caller to return after the handler workflow has received the requested to be cancelled, but does not wait for the handler workflow to finish processing the cancellation request.
44

55
To run this sample, set up your environment following the instructions in the main [Nexus Sample](../nexus/README.md).
66

@@ -29,8 +29,19 @@ Next, in separate terminal windows:
2929

3030
### Output
3131

32-
which should result in:
32+
which should result in on the caller side:
3333
```
34-
INFO i.t.s.n.caller.CallerStarter - Started workflow workflowId: 326732dd-a2b1-4de7-9ddd-dcee4f9f0229 runId: d580499f-79d5-461d-bd49-6248b4e522ae
35-
INFO i.t.s.n.caller.CallerStarter - Workflow result: Hallo Nexus ๐Ÿ‘‹
34+
14:33:52.810 i.t.s.n.caller.CallerStarter - Started workflow workflowId: 87e97bf0-ca8a-4ae6-a9dc-ae97e5c0ac41 runId: 01976b36-a524-71a1-b848-8eb385fec2c3
35+
14:33:54.250 i.t.s.n.caller.CallerStarter - Workflow result: Hallo Nexus ๐Ÿ‘‹
3636
```
37+
38+
on the handler side:
39+
40+
```
41+
14:33:54.177 INFO i.t.s.n.h.HelloHandlerWorkflowImpl - HelloHandlerWorkflow was cancelled successfully.
42+
14:33:56.167 INFO i.t.s.n.h.HelloHandlerWorkflowImpl - HelloHandlerWorkflow was cancelled successfully.
43+
14:33:57.172 INFO i.t.s.n.h.HelloHandlerWorkflowImpl - HelloHandlerWorkflow was cancelled successfully.
44+
14:33:57.176 INFO i.t.s.n.h.HelloHandlerWorkflowImpl - HelloHandlerWorkflow was cancelled successfully.
45+
```
46+
47+
Notice the timing, the caller workflow returned before the handler workflow was cancelled. This is because of the use of `WAIT_REQUESTED` as the cancellation type in the Nexus operation. This means the caller didn't have to wait for the handler workflow to finish, but still guarantees the handler workflow will receive the cancellation request.

โ€Žcore/src/main/java/io/temporal/samples/nexuscancellation/caller/HelloCallerWorkflowImpl.javaโ€Ž

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public class HelloCallerWorkflowImpl implements HelloCallerWorkflow {
2222
.setOperationOptions(
2323
NexusOperationOptions.newBuilder()
2424
.setScheduleToCloseTimeout(Duration.ofSeconds(10))
25+
// Set the cancellation type to WAIT_REQUESTED. This means that the caller
26+
// will wait for the cancellation request to be received by the handler before
27+
// proceeding with the cancellation.
28+
//
29+
// By default, the caller would wait until the operation is completed.
30+
.setCancellationType(NexusOperationCancellationType.WAIT_REQUESTED)
2531
.build())
2632
.build());
2733

@@ -55,10 +61,12 @@ public String hello(String message) {
5561
// Trigger cancellation of all uncompleted nexus operations invocations within the cancellation
5662
// scope
5763
scope.cancel();
58-
// Optionally, wait for all nexus operations to complete
64+
// Wait for all nexus operations to receive a cancellation request before
65+
// proceeding.
5966
//
6067
// Note: Once the workflow completes any pending cancellation requests are dropped by the
61-
// server.
68+
// server. In general, it is a good practice to wait for all cancellation requests to be
69+
// processed before completing the workflow.
6270
for (Promise<NexusService.HelloOutput> promise : results) {
6371
try {
6472
promise.get();
Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
package io.temporal.samples.nexuscancellation.handler;
22

33
import io.temporal.failure.ApplicationFailure;
4+
import io.temporal.failure.CanceledFailure;
45
import io.temporal.samples.nexus.handler.HelloHandlerWorkflow;
56
import io.temporal.samples.nexus.service.NexusService;
67
import io.temporal.workflow.Workflow;
78
import java.time.Duration;
9+
import org.slf4j.Logger;
810

911
public class HelloHandlerWorkflowImpl implements HelloHandlerWorkflow {
12+
public static final Logger log = Workflow.getLogger(HelloHandlerWorkflowImpl.class);
13+
1014
@Override
1115
public NexusService.HelloOutput hello(NexusService.HelloInput input) {
1216
// Sleep for a random duration to simulate some work
13-
Workflow.sleep(Duration.ofSeconds(Workflow.newRandom().nextInt(5)));
14-
switch (input.getLanguage()) {
15-
case EN:
16-
return new NexusService.HelloOutput("Hello " + input.getName() + " ๐Ÿ‘‹");
17-
case FR:
18-
return new NexusService.HelloOutput("Bonjour " + input.getName() + " ๐Ÿ‘‹");
19-
case DE:
20-
return new NexusService.HelloOutput("Hallo " + input.getName() + " ๐Ÿ‘‹");
21-
case ES:
22-
return new NexusService.HelloOutput("ยกHola! " + input.getName() + " ๐Ÿ‘‹");
23-
case TR:
24-
return new NexusService.HelloOutput("Merhaba " + input.getName() + " ๐Ÿ‘‹");
17+
try {
18+
Workflow.sleep(Duration.ofSeconds(Workflow.newRandom().nextInt(5)));
19+
switch (input.getLanguage()) {
20+
case EN:
21+
return new NexusService.HelloOutput("Hello " + input.getName() + " ๐Ÿ‘‹");
22+
case FR:
23+
return new NexusService.HelloOutput("Bonjour " + input.getName() + " ๐Ÿ‘‹");
24+
case DE:
25+
return new NexusService.HelloOutput("Hallo " + input.getName() + " ๐Ÿ‘‹");
26+
case ES:
27+
return new NexusService.HelloOutput("ยกHola! " + input.getName() + " ๐Ÿ‘‹");
28+
case TR:
29+
return new NexusService.HelloOutput("Merhaba " + input.getName() + " ๐Ÿ‘‹");
30+
}
31+
throw ApplicationFailure.newFailure(
32+
"Unsupported language: " + input.getLanguage(), "UNSUPPORTED_LANGUAGE");
33+
} catch (CanceledFailure e) {
34+
// Simulate some work after cancellation is requested
35+
Workflow.newDetachedCancellationScope(
36+
() -> Workflow.sleep(Duration.ofSeconds(Workflow.newRandom().nextInt(5))))
37+
.run();
38+
log.info("HelloHandlerWorkflow was cancelled successfully.");
39+
throw e;
2540
}
26-
throw ApplicationFailure.newFailure(
27-
"Unsupported language: " + input.getLanguage(), "UNSUPPORTED_LANGUAGE");
2841
}
2942
}

0 commit comments

Comments
ย (0)