|
1 | 1 | package io.temporal.samples.nexuscancellation.handler; |
2 | 2 |
|
3 | 3 | import io.temporal.failure.ApplicationFailure; |
| 4 | +import io.temporal.failure.CanceledFailure; |
4 | 5 | import io.temporal.samples.nexus.handler.HelloHandlerWorkflow; |
5 | 6 | import io.temporal.samples.nexus.service.NexusService; |
6 | 7 | import io.temporal.workflow.Workflow; |
7 | 8 | import java.time.Duration; |
| 9 | +import org.slf4j.Logger; |
8 | 10 |
|
9 | 11 | public class HelloHandlerWorkflowImpl implements HelloHandlerWorkflow { |
| 12 | + public static final Logger log = Workflow.getLogger(HelloHandlerWorkflowImpl.class); |
| 13 | + |
10 | 14 | @Override |
11 | 15 | public NexusService.HelloOutput hello(NexusService.HelloInput input) { |
12 | 16 | // 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; |
25 | 40 | } |
26 | | - throw ApplicationFailure.newFailure( |
27 | | - "Unsupported language: " + input.getLanguage(), "UNSUPPORTED_LANGUAGE"); |
28 | 41 | } |
29 | 42 | } |
0 commit comments