|
| 1 | +package com.functions; |
| 2 | + |
| 3 | +import com.microsoft.azure.functions.ExecutionContext; |
| 4 | +import com.microsoft.azure.functions.HttpMethod; |
| 5 | +import com.microsoft.azure.functions.HttpRequestMessage; |
| 6 | +import com.microsoft.azure.functions.HttpResponseMessage; |
| 7 | +import com.microsoft.azure.functions.annotation.AuthorizationLevel; |
| 8 | +import com.microsoft.azure.functions.annotation.FunctionName; |
| 9 | +import com.microsoft.azure.functions.annotation.HttpTrigger; |
| 10 | +import com.microsoft.durabletask.DurableTaskClient; |
| 11 | +import com.microsoft.durabletask.RetryPolicy; |
| 12 | +import com.microsoft.durabletask.TaskOptions; |
| 13 | +import com.microsoft.durabletask.TaskOrchestrationContext; |
| 14 | +import com.microsoft.durabletask.azurefunctions.DurableActivityTrigger; |
| 15 | +import com.microsoft.durabletask.azurefunctions.DurableClientContext; |
| 16 | +import com.microsoft.durabletask.azurefunctions.DurableClientInput; |
| 17 | +import com.microsoft.durabletask.azurefunctions.DurableOrchestrationTrigger; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | + |
| 23 | +public class RetriableTask { |
| 24 | + private static final AtomicBoolean exceptionThrew = new AtomicBoolean(false); |
| 25 | + @FunctionName("RetriableOrchestration") |
| 26 | + public HttpResponseMessage retriableOrchestration( |
| 27 | + @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 28 | + @DurableClientInput(name = "durableContext") DurableClientContext durableContext, |
| 29 | + final ExecutionContext context) { |
| 30 | + context.getLogger().info("Java HTTP trigger processed a request."); |
| 31 | + |
| 32 | + DurableTaskClient client = durableContext.getClient(); |
| 33 | + String instanceId = client.scheduleNewOrchestrationInstance("RetriableTask"); |
| 34 | + context.getLogger().info("Created new Java orchestration with instance ID = " + instanceId); |
| 35 | + return durableContext.createCheckStatusResponse(request, instanceId); |
| 36 | + } |
| 37 | + |
| 38 | + @FunctionName("RetriableTask") |
| 39 | + public String retriableTask( |
| 40 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 41 | + RetryPolicy retryPolicy = new RetryPolicy(2, Duration.ofSeconds(1)); |
| 42 | + TaskOptions taskOptions = new TaskOptions(retryPolicy); |
| 43 | + return ctx.callActivity("Append", "Test-Input", taskOptions, String.class).await(); |
| 44 | + } |
| 45 | + |
| 46 | + @FunctionName("Append") |
| 47 | + public String append( |
| 48 | + @DurableActivityTrigger(name = "name") String name, |
| 49 | + final ExecutionContext context) { |
| 50 | + if (!exceptionThrew.get()) { |
| 51 | + exceptionThrew.compareAndSet(false, true); |
| 52 | + throw new RuntimeException("Test for retry"); |
| 53 | + } |
| 54 | + context.getLogger().info("Append: " + name); |
| 55 | + return name + "-test"; |
| 56 | + } |
| 57 | +} |
0 commit comments