|
| 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.Task; |
| 12 | +import com.microsoft.durabletask.TaskOrchestrationContext; |
| 13 | +import com.microsoft.durabletask.azurefunctions.DurableClientContext; |
| 14 | +import com.microsoft.durabletask.azurefunctions.DurableClientInput; |
| 15 | +import com.microsoft.durabletask.azurefunctions.DurableOrchestrationTrigger; |
| 16 | + |
| 17 | +import java.time.Duration; |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +public class ContinueAsNew { |
| 21 | + @FunctionName("ContinueAsNew") |
| 22 | + public HttpResponseMessage continueAsNew( |
| 23 | + @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 24 | + @DurableClientInput(name = "durableContext") DurableClientContext durableContext, |
| 25 | + final ExecutionContext context) { |
| 26 | + context.getLogger().info("Java HTTP trigger processed a request."); |
| 27 | + |
| 28 | + DurableTaskClient client = durableContext.getClient(); |
| 29 | + String instanceId = client.scheduleNewOrchestrationInstance("EternalOrchestrator"); |
| 30 | + context.getLogger().info("Created new Java orchestration with instance ID = " + instanceId); |
| 31 | + return durableContext.createCheckStatusResponse(request, instanceId); |
| 32 | + } |
| 33 | + |
| 34 | + @FunctionName("EternalOrchestrator") |
| 35 | + public void eternalOrchestrator(@DurableOrchestrationTrigger(name = "runtimeState") TaskOrchestrationContext ctx) |
| 36 | + { |
| 37 | + System.out.println("Processing stuff..."); |
| 38 | + ctx.createTimer(Duration.ofSeconds(2)).await(); |
| 39 | + ctx.continueAsNew(null); |
| 40 | + } |
| 41 | + |
| 42 | + @FunctionName("ContinueAsNewExternalEvent") |
| 43 | + public HttpResponseMessage continueAsNewExternalEvent( |
| 44 | + @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 45 | + @DurableClientInput(name = "durableContext") DurableClientContext durableContext, |
| 46 | + final ExecutionContext context) { |
| 47 | + context.getLogger().info("Java HTTP trigger processed a request."); |
| 48 | + |
| 49 | + DurableTaskClient client = durableContext.getClient(); |
| 50 | + String instanceId = client.scheduleNewOrchestrationInstance("EternalEvent"); |
| 51 | + context.getLogger().info("Created new Java orchestration with instance ID = " + instanceId); |
| 52 | + return durableContext.createCheckStatusResponse(request, instanceId); |
| 53 | + } |
| 54 | + |
| 55 | + @FunctionName("EternalEvent") |
| 56 | + public void eternalEvent(@DurableOrchestrationTrigger(name = "runtimeState") TaskOrchestrationContext ctx) |
| 57 | + { |
| 58 | + System.out.println("Waiting external event..."); |
| 59 | + Task<Void> event = ctx.waitForExternalEvent("event"); |
| 60 | + Task<Void> timer = ctx.createTimer(Duration.ofSeconds(10)); |
| 61 | + Task<?> result = ctx.anyOf(event, timer).await(); |
| 62 | + if (result == event) { |
| 63 | + ctx.continueAsNew(null); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments