|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package software.amazon.lambda.durable.examples.callback; |
| 4 | + |
| 5 | +import software.amazon.lambda.durable.DurableContext; |
| 6 | +import software.amazon.lambda.durable.DurableHandler; |
| 7 | +import software.amazon.lambda.durable.TypeToken; |
| 8 | +import software.amazon.lambda.durable.config.StepConfig; |
| 9 | +import software.amazon.lambda.durable.config.WaitForCallbackConfig; |
| 10 | +import software.amazon.lambda.durable.examples.types.ApprovalRequest; |
| 11 | +import software.amazon.lambda.durable.exception.SerDesException; |
| 12 | +import software.amazon.lambda.durable.serde.JacksonSerDes; |
| 13 | + |
| 14 | +public class WaitForCallbackFailedExample extends DurableHandler<ApprovalRequest, String> { |
| 15 | + |
| 16 | + @Override |
| 17 | + public String handleRequest(ApprovalRequest input, DurableContext context) { |
| 18 | + |
| 19 | + String approvalResult; |
| 20 | + |
| 21 | + try { |
| 22 | + approvalResult = context.waitForCallback( |
| 23 | + "preapproval", |
| 24 | + String.class, |
| 25 | + (callbackId, ctx) -> { |
| 26 | + ctx.getLogger().info("Sending callback {} to preapproval system", callbackId); |
| 27 | + throw new RuntimeException("Submitter failed with an exception"); |
| 28 | + }, |
| 29 | + WaitForCallbackConfig.builder() |
| 30 | + .stepConfig(StepConfig.builder() |
| 31 | + .serDes(new FailedSerDes()) |
| 32 | + .build()) |
| 33 | + .build()); |
| 34 | + } catch (Exception ex) { |
| 35 | + return ex.getClass().getSimpleName() + ":" + ex.getMessage(); |
| 36 | + } |
| 37 | + |
| 38 | + return approvalResult; |
| 39 | + } |
| 40 | + |
| 41 | + private static class FailedSerDes extends JacksonSerDes { |
| 42 | + @Override |
| 43 | + public <T> T deserialize(String json, TypeToken<T> typeToken) { |
| 44 | + T result = super.deserialize(json, typeToken); |
| 45 | + if (result instanceof RuntimeException ex) { |
| 46 | + throw new SerDesException("Deserialization failed", ex); |
| 47 | + } |
| 48 | + return result; |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments