-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathOrderProcessor.java
More file actions
63 lines (53 loc) · 2.5 KB
/
OrderProcessor.java
File metadata and controls
63 lines (53 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.example;
import software.amazon.lambda.durable.DurableContext;
import software.amazon.lambda.durable.DurableHandler;
import java.time.Duration;
import java.util.Map;
import java.util.UUID;
/**
* Durable order processing workflow that demonstrates automatic checkpointing,
* wait operations, and multi-step orchestration using the Java Durable Execution SDK.
*
* Each step is checkpointed — if the function is interrupted, it resumes from
* the last completed step without re-executing previous work.
*/
public class OrderProcessor extends DurableHandler<Map<String, Object>, Map<String, Object>> {
@Override
public Map<String, Object> handleRequest(Map<String, Object> input, DurableContext ctx) {
String orderId = (String) input.getOrDefault("orderId", UUID.randomUUID().toString());
double amount = ((Number) input.getOrDefault("amount", 99.99)).doubleValue();
// Step 1: Validate order
String validation = ctx.step("validate-order", String.class, stepCtx -> {
System.out.println("Validating order " + orderId);
if (amount <= 0) {
throw new IllegalArgumentException("Invalid order amount: " + amount);
}
return "VALIDATED";
});
// Step 2: Reserve inventory
String reservationId = ctx.step("reserve-inventory", String.class, stepCtx -> {
System.out.println("Reserving inventory for order " + orderId);
return "RES-" + UUID.randomUUID().toString().substring(0, 8);
});
// Step 3: Process payment
String paymentId = ctx.step("process-payment", String.class, stepCtx -> {
System.out.println("Processing payment of $" + amount + " for order " + orderId);
return "PAY-" + UUID.randomUUID().toString().substring(0, 8);
});
// Wait for warehouse processing (no compute charges during wait)
ctx.wait("warehouse-processing", Duration.ofSeconds(5));
// Step 4: Confirm shipment
String trackingNumber = ctx.step("confirm-shipment", String.class, stepCtx -> {
System.out.println("Confirming shipment for order " + orderId);
return "TRACK-" + UUID.randomUUID().toString().substring(0, 8);
});
return Map.of(
"orderId", orderId,
"status", "COMPLETED",
"validation", validation,
"reservationId", reservationId,
"paymentId", paymentId,
"trackingNumber", trackingNumber
);
}
}