33package software .amazon .lambda .durable .examples ;
44
55import java .time .Duration ;
6+ import java .util .stream .IntStream ;
7+
68import software .amazon .lambda .durable .DurableContext ;
79import software .amazon .lambda .durable .DurableFuture ;
810import software .amazon .lambda .durable .DurableHandler ;
11+ import software .amazon .lambda .durable .config .MapConfig ;
12+ import software .amazon .lambda .durable .config .WaitForConditionConfig ;
13+ import software .amazon .lambda .durable .model .WaitForConditionResult ;
914
1015/**
1116 * Example demonstrating step execution with wait operations.
@@ -24,35 +29,43 @@ public class WaitExample extends DurableHandler<GreetingRequest, String> {
2429
2530 @ Override
2631 public String handleRequest (GreetingRequest input , DurableContext context ) {
27- // Step 1: Start processing
28- var started =
29- context .step ("start-processing" , String .class , stepCtx -> "Started processing for " + input .getName ());
30-
31- // Wait 10 seconds
32- context .wait (null , Duration .ofSeconds (10 ));
32+ var items =
33+ IntStream .range (0 , 100 ).mapToObj (i -> input .getName () + "-" + i ).toList ();
3334
34- // Step 2: Continue processing
35- var continued = context .stepAsync ("continue-processing" , String .class , stepCtx -> {
36- try {
37- Thread .sleep (10000 );
38- } catch (InterruptedException e ) {
39- throw new RuntimeException (e );
40- }
41- return started + " - continued after 10s" ;
42- });
35+ var config = MapConfig .builder ().maxConcurrency (50 ).build ();
4336
44- // Wait at most seconds
45- var wait5seconds = context .runInChildContextAsync ("wait-5-seconds" , String .class , ctx -> {
46- ctx .wait ("wait-5-seconds" , Duration .ofSeconds (5 ));
37+ var result = context .map (
38+ "process-all" ,
39+ items ,
40+ String .class ,
41+ (item , index , ctx ) -> {
42+ // Step 1: waitForCondition — fails twice, succeeds on 3rd attempt
43+ var conditionResult = ctx .waitForCondition (
44+ "condition-" + index ,
45+ Integer .class ,
46+ (callCount , stepCtx ) -> {
47+ if (callCount % 3 == 0 ) {
48+ return WaitForConditionResult .stopPolling (callCount + 1 );
49+ }
50+ return WaitForConditionResult .continuePolling (callCount + 1 );
51+ },
52+ WaitForConditionConfig .<Integer >builder ().initialState (1 ).build ()
53+ );
4754
48- return started + " - waited 5 seconds" ;
49- });
55+ ctx .getLogger ().info ("Item {} condition resolved: {}" , item , conditionResult );
5056
51- var step2 = DurableFuture .anyOf (continued , wait5seconds );
57+ // Step 2: waitForCallback — wait for external approval
58+ var approved = ctx .waitForCallback ("approval-" + index , String .class , (callbackId , stepCtx ) -> {
59+ var command = String .format (
60+ "aws lambda send-durable-execution-callback-success --callback-id %s --result $(echo -n '\" approved\" ' | base64)" ,
61+ callbackId );
62+ stepCtx .getLogger ().info ("To approve item '{}', run: {}" , item , command );
63+ });
5264
53- // Step 3: Complete
54- var result = context .step ("complete-processing" , String .class , stepCtx -> step2 + " - completed after 5s more" );
65+ return conditionResult + " -> " + approved ;
66+ },
67+ config );
5568
56- return result ;
69+ return String . join ( " | " , result . results ()) ;
5770 }
5871}
0 commit comments