Skip to content

Commit 426dabc

Browse files
committed
add anyOf
1 parent a3cfd56 commit 426dabc

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

examples/src/main/java/software/amazon/lambda/durable/examples/WaitExample.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.time.Duration;
66
import software.amazon.lambda.durable.DurableContext;
7+
import software.amazon.lambda.durable.DurableFuture;
78
import software.amazon.lambda.durable.DurableHandler;
89

910
/**
@@ -31,11 +32,19 @@ public String handleRequest(GreetingRequest input, DurableContext context) {
3132
context.wait(null, Duration.ofSeconds(10));
3233

3334
// Step 2: Continue processing
34-
var continued =
35-
context.step("continue-processing", String.class, stepCtx -> started + " - continued after 10s");
36-
37-
// Wait 5 seconds
38-
context.wait(null, Duration.ofSeconds(5));
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+
});
43+
44+
// Wait at most seconds
45+
var wait5seconds = context.waitAsync(null, Duration.ofSeconds(5));
46+
47+
DurableFuture.anyOf(continued, wait5seconds);
3948

4049
// Step 3: Complete
4150
var result =

sdk/src/main/java/software/amazon/lambda/durable/DurableFuture.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.util.Arrays;
66
import java.util.List;
7+
import java.util.concurrent.CompletableFuture;
8+
import software.amazon.lambda.durable.operation.BaseDurableOperation;
79

810
/**
911
* A future representing the result of an asynchronous durable operation.
@@ -52,4 +54,17 @@ static <T> List<T> allOf(DurableFuture<T>... futures) {
5254
static <T> List<T> allOf(List<DurableFuture<T>> futures) {
5355
return futures.stream().map(DurableFuture::get).toList();
5456
}
57+
58+
/**
59+
* Waits for any of the provided futures to complete and returns its result.
60+
*
61+
* @param futures the futures to wait for
62+
* @return the result of the first future to complete
63+
*/
64+
static Object anyOf(DurableFuture<?>... futures) {
65+
return CompletableFuture.anyOf(Arrays.stream(futures)
66+
.map(f -> ((BaseDurableOperation) f).getCompletionFuture())
67+
.toArray(CompletableFuture[]::new))
68+
.join();
69+
}
5570
}

sdk/src/main/java/software/amazon/lambda/durable/operation/BaseDurableOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected BaseDurableOperation(OperationIdentifier operationIdentifier, DurableC
6767
executionManager.registerOperation(this);
6868
}
6969

70-
protected CompletableFuture<BaseDurableOperation> getCompletionFuture() {
70+
public CompletableFuture<BaseDurableOperation> getCompletionFuture() {
7171
return completionFuture;
7272
}
7373

0 commit comments

Comments
 (0)