|
4 | 4 |
|
5 | 5 | import com.azure.core.credential.TokenCredential; |
6 | 6 | import com.microsoft.durabletask.*; |
7 | | - |
8 | | -import com.microsoft.durabletask.client.azuremanaged.DurableTaskSchedulerClientExtensions; |
9 | | -import com.microsoft.durabletask.worker.azuremanaged.DurableTaskSchedulerWorkerExtensions; |
| 7 | +import com.microsoft.durabletask.azuremanaged.client.DurableTaskSchedulerClientExtensions; |
| 8 | +import com.microsoft.durabletask.azuremanaged.worker.DurableTaskSchedulerWorkerExtensions; |
10 | 9 | import org.springframework.boot.SpringApplication; |
11 | 10 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
12 | 11 | import org.springframework.web.bind.annotation.*; |
@@ -65,53 +64,85 @@ public DurableTaskGrpcWorker durableTaskWorker( |
65 | 64 | properties.getTaskHubName(), |
66 | 65 | tokenCredential); |
67 | 66 |
|
68 | | - // Add orchestrations |
69 | | - workerBuilder.addOrchestration("ProcessOrderOrchestration", ctx -> { |
70 | | - // Get the order input as JSON string |
71 | | - String orderJson = ctx.getInput(String.class); |
72 | | - |
73 | | - // Process the order through multiple activities |
74 | | - boolean isValid = ctx.callActivity("ValidateOrder", orderJson, Boolean.class).await(); |
75 | | - if (!isValid) { |
76 | | - ctx.complete("{\"status\": \"FAILED\", \"message\": \"Order validation failed\"}"); |
77 | | - return; |
78 | | - } |
79 | | - |
80 | | - // Process payment |
81 | | - String paymentResult = ctx.callActivity("ProcessPayment", orderJson, String.class).await(); |
82 | | - if (!paymentResult.contains("\"success\":true")) { |
83 | | - ctx.complete("{\"status\": \"FAILED\", \"message\": \"Payment processing failed\"}"); |
84 | | - return; |
| 67 | + // Add orchestrations using the factory pattern |
| 68 | + workerBuilder.addOrchestration(new TaskOrchestrationFactory() { |
| 69 | + @Override |
| 70 | + public String getName() { return "ProcessOrderOrchestration"; } |
| 71 | + |
| 72 | + @Override |
| 73 | + public TaskOrchestration create() { |
| 74 | + return ctx -> { |
| 75 | + // Get the order input as JSON string |
| 76 | + String orderJson = ctx.getInput(String.class); |
| 77 | + |
| 78 | + // Process the order through multiple activities |
| 79 | + boolean isValid = ctx.callActivity("ValidateOrder", orderJson, Boolean.class).await(); |
| 80 | + if (!isValid) { |
| 81 | + ctx.complete("{\"status\": \"FAILED\", \"message\": \"Order validation failed\"}"); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + // Process payment |
| 86 | + String paymentResult = ctx.callActivity("ProcessPayment", orderJson, String.class).await(); |
| 87 | + if (!paymentResult.contains("\"success\":true")) { |
| 88 | + ctx.complete("{\"status\": \"FAILED\", \"message\": \"Payment processing failed\"}"); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + // Ship order |
| 93 | + String shipmentResult = ctx.callActivity("ShipOrder", orderJson, String.class).await(); |
| 94 | + |
| 95 | + // Return the final result |
| 96 | + ctx.complete("{\"status\": \"SUCCESS\", " + |
| 97 | + "\"payment\": " + paymentResult + ", " + |
| 98 | + "\"shipment\": " + shipmentResult + "}"); |
| 99 | + }; |
85 | 100 | } |
86 | | - |
87 | | - // Ship order |
88 | | - String shipmentResult = ctx.callActivity("ShipOrder", orderJson, String.class).await(); |
89 | | - |
90 | | - // Return the final result |
91 | | - ctx.complete("{\"status\": \"SUCCESS\", " + |
92 | | - "\"payment\": " + paymentResult + ", " + |
93 | | - "\"shipment\": " + shipmentResult + "}"); |
94 | 101 | }); |
95 | 102 |
|
96 | | - // Add activity implementations |
97 | | - workerBuilder.addActivity("ValidateOrder", ctx -> { |
98 | | - String orderJson = ctx.getInput(String.class); |
99 | | - // Simple validation - check if order contains amount and it's greater than 0 |
100 | | - return orderJson.contains("\"amount\"") && !orderJson.contains("\"amount\":0"); |
| 103 | + // Add activities using the factory pattern |
| 104 | + workerBuilder.addActivity(new TaskActivityFactory() { |
| 105 | + @Override |
| 106 | + public String getName() { return "ValidateOrder"; } |
| 107 | + |
| 108 | + @Override |
| 109 | + public TaskActivity create() { |
| 110 | + return ctx -> { |
| 111 | + String orderJson = ctx.getInput(String.class); |
| 112 | + // Simple validation - check if order contains amount and it's greater than 0 |
| 113 | + return orderJson.contains("\"amount\"") && !orderJson.contains("\"amount\":0"); |
| 114 | + }; |
| 115 | + } |
101 | 116 | }); |
102 | 117 |
|
103 | | - workerBuilder.addActivity("ProcessPayment", ctx -> { |
104 | | - String orderJson = ctx.getInput(String.class); |
105 | | - // Simulate payment processing |
106 | | - sleep(1000); // Simulate processing time |
107 | | - return "{\"success\":true, \"transactionId\":\"TXN" + System.currentTimeMillis() + "\"}"; |
| 118 | + workerBuilder.addActivity(new TaskActivityFactory() { |
| 119 | + @Override |
| 120 | + public String getName() { return "ProcessPayment"; } |
| 121 | + |
| 122 | + @Override |
| 123 | + public TaskActivity create() { |
| 124 | + return ctx -> { |
| 125 | + String orderJson = ctx.getInput(String.class); |
| 126 | + // Simulate payment processing |
| 127 | + sleep(1000); // Simulate processing time |
| 128 | + return "{\"success\":true, \"transactionId\":\"TXN" + System.currentTimeMillis() + "\"}"; |
| 129 | + }; |
| 130 | + } |
108 | 131 | }); |
109 | 132 |
|
110 | | - workerBuilder.addActivity("ShipOrder", ctx -> { |
111 | | - String orderJson = ctx.getInput(String.class); |
112 | | - // Simulate shipping process |
113 | | - sleep(1000); // Simulate processing time |
114 | | - return "{\"trackingNumber\":\"TRACK" + System.currentTimeMillis() + "\"}"; |
| 133 | + workerBuilder.addActivity(new TaskActivityFactory() { |
| 134 | + @Override |
| 135 | + public String getName() { return "ShipOrder"; } |
| 136 | + |
| 137 | + @Override |
| 138 | + public TaskActivity create() { |
| 139 | + return ctx -> { |
| 140 | + String orderJson = ctx.getInput(String.class); |
| 141 | + // Simulate shipping process |
| 142 | + sleep(1000); // Simulate processing time |
| 143 | + return "{\"trackingNumber\":\"TRACK" + System.currentTimeMillis() + "\"}"; |
| 144 | + }; |
| 145 | + } |
115 | 146 | }); |
116 | 147 |
|
117 | 148 | return workerBuilder.build(); |
|
0 commit comments