microsoft / durabletask-java

Java SDK for Durable Functions and the Durable Task Framework
MIT License
13 stars 7 forks source link

Added new allOf convenience overload #43

Closed cgillum closed 2 years ago

cgillum commented 2 years ago

This is a convenience overload of anyOf that allows a cleaner syntax when you have a fixed number of activities for fan-out/fan-in. I decided to add this after working on documentation and code samples. Here's the sample that uses this overload:

@FunctionName("NewBuildingPermit")
public String newBuildingPermit(
    @DurableOrchestrationTrigger(name = "runtimeState") String runtimeState) {
        return OrchestrationRunner.loadAndRun(runtimeState, ctx -> {
            String applicationId = ctx.getInput(String.class);

            Task<Void> gate1 = ctx.waitForExternalEvent("CityPlanningApproval");
            Task<Void> gate2 = ctx.waitForExternalEvent("FireDeptApproval");
            Task<Void> gate3 = ctx.waitForExternalEvent("BuildingDeptApproval");

            // all three departments must grant approval before a permit can be issued
            ctx.allOf(gate1, gate2, gate3).get();

            ctx.callActivity("IssueBuildingPermit", applicationId).get();
        });
}

Basically, it removes the need to wrap the list of tasks in something like Arrays.asList(...).

The other reason to add this is for consistency with anyOf, which already has a similar overload.

kaibocai commented 2 years ago

should it be allOf instead of anyOf in the pr title.

cgillum commented 2 years ago

I fixed the title. The branch name is wrong too, but the code is right. 😅