Skip to content

Commit 90b11d6

Browse files
committed
docs: Task execution keys
Signed-off-by: Javier Aliaga <[email protected]>
1 parent 3434802 commit 90b11d6

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

daprdocs/content/en/java-sdk-docs/java-workflow/java-workflow-howto.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ weight: 20000
66
description: How to get up and running with workflows using the Dapr Java SDK
77
---
88

9-
Lets create a Dapr workflow and invoke it using the console. With the [provided workflow example](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/workflows), you will:
9+
Let's create a Dapr workflow and invoke it using the console. With the [provided workflow example](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/workflows), you will:
1010

1111
- Execute the workflow instance using the [Java workflow worker](https://github.com/dapr/java-sdk/blob/master/examples/src/main/java/io/dapr/examples/workflows/DemoWorkflowWorker.java)
1212
- Utilize the Java workflow client and API calls to [start and terminate workflow instances](https://github.com/dapr/java-sdk/blob/master/examples/src/main/java/io/dapr/examples/workflows/DemoWorkflowClient.java)
@@ -85,11 +85,10 @@ You're up and running! Both Dapr and your app logs will appear here.
8585
== APP == INFO: Durable Task worker is connecting to sidecar at 127.0.0.1:50001.
8686
```
8787

88-
## Run the `DemoWorkflowClient
88+
## Run the `DemoWorkflowClient`
8989

9090
The `DemoWorkflowClient` starts instances of workflows that have been registered with Dapr.
9191

92-
9392
```java
9493
public class DemoWorkflowClient {
9594

@@ -246,4 +245,45 @@ Exiting DemoWorkflowClient.
246245

247246
## Next steps
248247
- [Learn more about Dapr workflow]({{< ref workflow-overview.md >}})
249-
- [Workflow API reference]({{< ref workflow_api.md >}})
248+
- [Workflow API reference]({{< ref workflow_api.md >}})
249+
250+
## Advanced features
251+
252+
### Task Execution Keys
253+
254+
Task execution keys are unique identifiers generated by the durabletask-java library. They are stored in the `WorkflowActivityContext` and can be used to track and manage the execution of workflow activities. They are particularly useful for:
255+
256+
1. **Idempotency**: Ensuring activities are not executed multiple times for the same task
257+
2. **State Management**: Tracking the state of activity execution
258+
3. **Error Handling**: Managing retries and failures in a controlled manner
259+
260+
Here's an example of how to use task execution keys in your workflow activities:
261+
262+
```java
263+
public class TaskExecutionKeyActivity implements WorkflowActivity {
264+
@Override
265+
public Object run(WorkflowActivityContext ctx) {
266+
// Get the task execution key for this activity
267+
String taskExecutionKey = ctx.getTaskExecutionKey();
268+
269+
// Use the key to implement idempotency or state management
270+
// For example, check if this task has already been executed
271+
if (isTaskAlreadyExecuted(taskExecutionKey)) {
272+
return getPreviousResult(taskExecutionKey);
273+
}
274+
275+
// Execute the activity logic
276+
Object result = executeActivityLogic();
277+
278+
// Store the result with the task execution key
279+
storeResult(taskExecutionKey, result);
280+
281+
return result;
282+
}
283+
}
284+
```
285+
286+
287+
288+
289+

sdk-tests/src/test/java/io/dapr/it/testcontainers/TestExecutionKeysWorkflow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 The Dapr Authors
2+
* Copyright 2025 The Dapr Authors
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at

0 commit comments

Comments
 (0)