|
| 1 | +package com.functions; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | + |
| 5 | +import com.microsoft.azure.functions.ExecutionContext; |
| 6 | +import com.microsoft.azure.functions.HttpMethod; |
| 7 | +import com.microsoft.azure.functions.HttpRequestMessage; |
| 8 | +import com.microsoft.azure.functions.HttpResponseMessage; |
| 9 | +import com.microsoft.azure.functions.annotation.AuthorizationLevel; |
| 10 | +import com.microsoft.azure.functions.annotation.FunctionName; |
| 11 | +import com.microsoft.azure.functions.annotation.HttpTrigger; |
| 12 | +import com.microsoft.durabletask.DurableTaskClient; |
| 13 | +import com.microsoft.durabletask.NewOrchestrationInstanceOptions; |
| 14 | +import com.microsoft.durabletask.NewSubOrchestrationInstanceOptions; |
| 15 | +import com.microsoft.durabletask.TaskOrchestrationContext; |
| 16 | +import com.microsoft.durabletask.azurefunctions.DurableActivityTrigger; |
| 17 | +import com.microsoft.durabletask.azurefunctions.DurableClientContext; |
| 18 | +import com.microsoft.durabletask.azurefunctions.DurableClientInput; |
| 19 | +import com.microsoft.durabletask.azurefunctions.DurableOrchestrationTrigger; |
| 20 | + |
| 21 | +public class Versioning { |
| 22 | + /** |
| 23 | + * This HTTP-triggered function starts the orchestration. |
| 24 | + */ |
| 25 | + @FunctionName("StartVersionedOrchestration") |
| 26 | + public HttpResponseMessage startOrchestration( |
| 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 queryVersion = request.getQueryParameters().get("version"); |
| 34 | + String instanceId = null; |
| 35 | + if (queryVersion == "default") { |
| 36 | + instanceId = client.scheduleNewOrchestrationInstance("VersionedOrchestrator"); |
| 37 | + } else { |
| 38 | + instanceId = client.scheduleNewOrchestrationInstance("VersionedOrchestrator", new NewOrchestrationInstanceOptions().setVersion(queryVersion)); |
| 39 | + } |
| 40 | + context.getLogger().info("Created new Java orchestration with instance ID = " + instanceId); |
| 41 | + return durableContext.createCheckStatusResponse(request, instanceId); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * This HTTP-triggered function starts the orchestration. |
| 46 | + */ |
| 47 | + @FunctionName("StartVersionedSubOrchestration") |
| 48 | + public HttpResponseMessage startSubOrchestration( |
| 49 | + @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 50 | + @DurableClientInput(name = "durableContext") DurableClientContext durableContext, |
| 51 | + final ExecutionContext context) { |
| 52 | + context.getLogger().info("Java HTTP trigger processed a request."); |
| 53 | + |
| 54 | + DurableTaskClient client = durableContext.getClient(); |
| 55 | + String queryVersion = request.getQueryParameters().get("version"); |
| 56 | + String instanceId = null; |
| 57 | + if (queryVersion == "default") { |
| 58 | + instanceId = client.scheduleNewOrchestrationInstance("VersionedSubOrchestrator"); |
| 59 | + } else { |
| 60 | + instanceId = client.scheduleNewOrchestrationInstance("VersionedSubOrchestrator", queryVersion); |
| 61 | + } |
| 62 | + context.getLogger().info("Created new Java orchestration with instance ID = " + instanceId); |
| 63 | + return durableContext.createCheckStatusResponse(request, instanceId); |
| 64 | + } |
| 65 | + |
| 66 | + @FunctionName("VersionedOrchestrator") |
| 67 | + public String versionedOrchestrator( |
| 68 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 69 | + return ctx.callActivity("SayVersion", ctx.getVersion(), String.class).await(); |
| 70 | + } |
| 71 | + |
| 72 | + @FunctionName("VersionedSubOrchestrator") |
| 73 | + public String versionedSubOrchestrator( |
| 74 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 75 | + String subVersion = ctx.getInput(String.class); |
| 76 | + NewSubOrchestrationInstanceOptions options = new NewSubOrchestrationInstanceOptions(); |
| 77 | + if (subVersion != null) { |
| 78 | + options.setVersion(subVersion); |
| 79 | + } |
| 80 | + return ctx.callSubOrchestrator("SayVersion", null, null, options, String.class).await(); |
| 81 | + } |
| 82 | + |
| 83 | + public String subOrchestrator( |
| 84 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 85 | + return ctx.callActivity("SayVersion", ctx.getVersion(), String.class).await(); |
| 86 | + } |
| 87 | + |
| 88 | + @FunctionName("SayVersion") |
| 89 | + public String sayVersion( |
| 90 | + @DurableActivityTrigger(name = "version") String version, |
| 91 | + final ExecutionContext context) { |
| 92 | + context.getLogger().info("Called with version: " + version); |
| 93 | + return "Version: " + version; |
| 94 | + } |
| 95 | +} |
0 commit comments