-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathOrderProcessor.java
More file actions
66 lines (56 loc) · 2.69 KB
/
OrderProcessor.java
File metadata and controls
66 lines (56 loc) · 2.69 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
64
65
66
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) {
// Wrap UUID generation in a step to ensure determinism on replay
String orderId = input.containsKey("orderId")
? (String) input.get("orderId")
: ctx.step("generate-order-id", String.class, stepCtx -> 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 -> {
stepCtx.getLogger().info("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 -> {
stepCtx.getLogger().info("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 -> {
stepCtx.getLogger().info("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 -> {
stepCtx.getLogger().info("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
);
}
}