|
13 | 13 |
|
14 | 14 | package io.dapr.examples.workflows;
|
15 | 15 |
|
| 16 | +import com.microsoft.durabletask.CompositeTaskFailedException; |
| 17 | +import com.microsoft.durabletask.Task; |
16 | 18 | import com.microsoft.durabletask.TaskCanceledException;
|
17 | 19 | import io.dapr.workflows.Workflow;
|
18 | 20 | import io.dapr.workflows.WorkflowStub;
|
19 | 21 |
|
20 | 22 | import java.time.Duration;
|
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
21 | 25 |
|
22 | 26 | /**
|
23 | 27 | * Implementation of the DemoWorkflow for the server side.
|
24 | 28 | */
|
25 | 29 | public class DemoWorkflow extends Workflow {
|
26 |
| - |
27 | 30 | @Override
|
28 | 31 | public WorkflowStub create() {
|
29 | 32 | return ctx -> {
|
30 | 33 | ctx.getLogger().info("Starting Workflow: " + ctx.getName());
|
31 | 34 | ctx.getLogger().info("Instance ID: " + ctx.getInstanceId());
|
32 |
| - ctx.getLogger().info("Waiting for event: 'myEvent'..."); |
| 35 | + ctx.getLogger().info("Current Orchestration Time: " + ctx.getCurrentInstant()); |
| 36 | + ctx.getLogger().info("Waiting for event: 'TimedOutEvent'..."); |
| 37 | + try { |
| 38 | + ctx.waitForExternalEvent("TimedOutEvent", Duration.ofSeconds(10)).await(); |
| 39 | + } catch (TaskCanceledException e) { |
| 40 | + ctx.getLogger().warn("Timed out"); |
| 41 | + ctx.getLogger().warn(e.getMessage()); |
| 42 | + } |
| 43 | + |
| 44 | + ctx.getLogger().info("Waiting for event: 'TestEvent'..."); |
| 45 | + try { |
| 46 | + ctx.waitForExternalEvent("TestEvent", Duration.ofSeconds(10)).await(); |
| 47 | + ctx.getLogger().info("Received TestEvent"); |
| 48 | + } catch (TaskCanceledException e) { |
| 49 | + ctx.getLogger().warn("Timed out"); |
| 50 | + ctx.getLogger().warn(e.getMessage()); |
| 51 | + } |
| 52 | + |
| 53 | + ctx.getLogger().info("Parallel Execution - Waiting for all tasks to finish..."); |
| 54 | + try { |
| 55 | + Task<String> t1 = ctx.waitForExternalEvent("event1", Duration.ofSeconds(5), String.class); |
| 56 | + Task<String> t2 = ctx.waitForExternalEvent("event2", Duration.ofSeconds(5), String.class); |
| 57 | + Task<String> t3 = ctx.waitForExternalEvent("event3", Duration.ofSeconds(5), String.class); |
| 58 | + |
| 59 | + List<String> results = ctx.allOf(Arrays.asList(t1, t2, t3)).await(); |
| 60 | + results.forEach(t -> ctx.getLogger().info("finished task: " + t)); |
| 61 | + ctx.getLogger().info("All tasks finished!"); |
| 62 | + |
| 63 | + } catch (CompositeTaskFailedException e) { |
| 64 | + ctx.getLogger().warn(e.getMessage()); |
| 65 | + List<Exception> exceptions = e.getExceptions(); |
| 66 | + exceptions.forEach(ex -> ctx.getLogger().warn(ex.getMessage())); |
| 67 | + } |
| 68 | + |
| 69 | + ctx.getLogger().info("Parallel Execution - Waiting for any task to finish..."); |
33 | 70 | try {
|
34 |
| - ctx.waitForExternalEvent("myEvent", Duration.ofSeconds(10)).await(); |
35 |
| - ctx.getLogger().info("Received!"); |
| 71 | + Task<String> e1 = ctx.waitForExternalEvent("e1", Duration.ofSeconds(5), String.class); |
| 72 | + Task<String> e2 = ctx.waitForExternalEvent("e2", Duration.ofSeconds(5), String.class); |
| 73 | + Task<String> e3 = ctx.waitForExternalEvent("e3", Duration.ofSeconds(5), String.class); |
| 74 | + Task<Void> timeoutTask = ctx.createTimer(Duration.ofSeconds(1)); |
| 75 | + |
| 76 | + Task<?> winner = ctx.anyOf(Arrays.asList(e1, e2, e3, timeoutTask)).await(); |
| 77 | + if (winner == timeoutTask) { |
| 78 | + ctx.getLogger().info("All tasks timed out!"); |
| 79 | + } else { |
| 80 | + ctx.getLogger().info("One of the tasks finished!"); |
| 81 | + } |
36 | 82 | } catch (TaskCanceledException e) {
|
37 | 83 | ctx.getLogger().warn("Timed out");
|
38 | 84 | ctx.getLogger().warn(e.getMessage());
|
39 | 85 | }
|
40 |
| - ctx.complete("finished"); |
| 86 | + |
| 87 | + ctx.getLogger().info("Calling Activity..."); |
| 88 | + var input = new DemoActivityInput("Hello Activity!"); |
| 89 | + var output = ctx.callActivity(DemoWorkflowActivity.class.getName(), input, DemoActivityOutput.class).await(); |
| 90 | + |
| 91 | + ctx.getLogger().info("Activity returned: " + output); |
| 92 | + ctx.getLogger().info("Activity returned: " + output.getNewMessage()); |
| 93 | + ctx.getLogger().info("Activity returned: " + output.getOriginalMessage()); |
| 94 | + |
| 95 | + |
| 96 | + boolean shouldComplete = true; |
| 97 | + ctx.getLogger().info("Waiting for event: 'RestartEvent'..."); |
| 98 | + try { |
| 99 | + ctx.waitForExternalEvent("RestartEvent", Duration.ofSeconds(10)).await(); |
| 100 | + ctx.getLogger().info("Received RestartEvent"); |
| 101 | + ctx.getLogger().info("Restarting Workflow by calling continueAsNew..."); |
| 102 | + ctx.continueAsNew("TestInputRestart", false); |
| 103 | + shouldComplete = false; |
| 104 | + } catch (TaskCanceledException e) { |
| 105 | + ctx.getLogger().warn("Restart Timed out"); |
| 106 | + ctx.getLogger().warn(e.getMessage()); |
| 107 | + } |
| 108 | + |
| 109 | + if (shouldComplete) { |
| 110 | + ctx.getLogger().info("Child-Workflow> Calling ChildWorkflow..."); |
| 111 | + var childWorkflowInput = "Hello ChildWorkflow!"; |
| 112 | + var childWorkflowOutput = |
| 113 | + ctx.callSubWorkflow(DemoSubWorkflow.class.getName(), childWorkflowInput, String.class).await(); |
| 114 | + |
| 115 | + ctx.getLogger().info("Child-Workflow> returned: " + childWorkflowOutput); |
| 116 | + |
| 117 | + ctx.getLogger().info("Workflow finished"); |
| 118 | + ctx.complete("finished"); |
| 119 | + |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + ctx.getLogger().info("Workflow restarted"); |
41 | 124 | };
|
42 | 125 | }
|
43 | 126 | }
|
0 commit comments