This repo contains the Java SDK for the Durable Task Framework as well as classes and annotations to support running Azure Durable Functions for Java. With this SDK, you can define, schedule, and manage durable orchestrations using ordinary Java code.
// *** Simple, fault-tolerant, sequential orchestration ***Stringresult = "";
result += ctx.callActivity("SayHello", "Tokyo", String.class).await() + ", ";
result += ctx.callActivity("SayHello", "London", String.class).await() + ", ";
result += ctx.callActivity("SayHello", "Seattle", String.class).await();
returnresult;Orchestrator code re-executes while rebuilding state from history. Wrap an existing
java.util.logging.Logger to suppress log output during those replay segments:
Loggerlogger = ctx.createReplaySafeLogger(
Logger.getLogger(MyOrchestration.class.getName()));
logger.info(() -> "Starting orchestration " + ctx.getInstanceId());
Stringresult = ctx.callActivity("ProcessItem", input, String.class).await();
logger.info(() -> "Activity returned: " + result);In Azure Functions, pass ExecutionContext.getLogger() instead of creating a named
logger so the output retains its invocation ID and normal host routing:
Loggerlogger = ctx.createReplaySafeLogger(executionContext.getLogger());Replay-safe logging suppresses calls made while replaying; it does not guarantee exactly-once log delivery across failed or retried live orchestration turns.
// Get the list of work-items to processList<?> batch = ctx.callActivity("GetWorkBatch", List.class).await();
// Schedule each task to run in parallelList<Task<Integer>> parallelTasks = batch.stream()
.map(item -> ctx.callActivity("ProcessItem", item, Integer.class))
.collect(Collectors.toList());
// Wait for all tasks to complete, then return the aggregated sum of the resultsList<Integer> results = ctx.allOf(parallelTasks).await();
returnresults.stream().reduce(0, Integer::sum);ApprovalInfoapprovalInfo = ctx.getInput(ApprovalInfo.class);
ctx.callActivity("RequestApproval", approvalInfo).await();
Durationtimeout = Duration.ofHours(72);
try {
// Wait for an approval. A TaskCanceledException will be thrown if the timeout expires.booleanapproved = ctx.waitForExternalEvent("ApprovalEvent", timeout, boolean.class).await();
approvalInfo.setApproved(approved);
ctx.callActivity("ProcessApproval", approvalInfo).await();
} catch (TaskCanceledExceptiontimeoutEx) {
ctx.callActivity("Escalate", approvalInfo).await();
}JobInfojobInfo = ctx.getInput(JobInfo.class);
StringjobId = jobInfo.getJobId();
Stringstatus = ctx.callActivity("GetJobStatus", jobId, String.class).await();
if (status.equals("Completed")) {
// The job is done - we can exit nowctx.callActivity("SendAlert", jobId).await();
} else {
// wait N minutes before doing the next pollDurationpollingDelay = jobInfo.getPollingDelay();
ctx.createTimer(pollingDelay).await();
// restart from the beginningctx.continueAsNew(jobInfo);
}
returnnull;The following packages are produced from this repo.
| Package | Latest version |
|---|---|
| Durable Task - Client | |
| Durable Task - Azure Functions |
For information about how to get started with Durable Functions for Java, see the Azure Functions README.md content.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.