Skip to content

Latest commit

History

150 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Durable Task Client SDK for Java

BuildLicense: MIT

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 sequences

// *** 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;

Replay-safe orchestration logging

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.

Reliable fan-out / fan-in orchestration pattern

// 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);

Long-running human interaction pattern (approval workflow)

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();
}

Eternal monitoring orchestration

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;

Maven Central packages

The following packages are produced from this repo.

PackageLatest version
Durable Task - ClientMaven Central
Durable Task - Azure FunctionsMaven Central

Getting started with Azure Functions

For information about how to get started with Durable Functions for Java, see the Azure Functions README.md content.

Contributing

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.

Trademarks

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.

About

Java SDK for Durable Functions and the Durable Task Framework

Resources

Code of conduct

Contributing

Security policy

Stars

29 stars

Watchers

6 watching

Forks

Releases

Packages

Used by

Contributors

Languages