Skip to content

Commit 8749cb5

Browse files
committed
moved taskToken mothod to a class
1 parent 0f431f7 commit 8749cb5

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

core/src/main/java/io/temporal/samples/taskinteraction/TaskWorkflowImpl.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import io.temporal.workflow.Workflow;
2525
import java.time.Duration;
2626
import java.util.Arrays;
27-
import java.util.concurrent.atomic.AtomicInteger;
2827
import org.slf4j.Logger;
2928

3029
public class TaskWorkflowImpl implements TaskWorkflow {
@@ -40,14 +39,15 @@ public class TaskWorkflowImpl implements TaskWorkflow {
4039

4140
@Override
4241
public void execute() {
42+
final TaskToken taskToken = new TaskToken();
4343

4444
// Schedule two "tasks" in parallel. The last parameter is the token the client needs
4545
// to change the task state, and ultimately to complete the task
4646
logger.info("About to create async tasks");
4747
final Promise<String> task1 =
48-
taskService.executeTaskAsync(() -> activity.createTask("TODO 1"), taskToken());
48+
taskService.executeTaskAsync(() -> activity.createTask("TODO 1"), taskToken.getNext());
4949
final Promise<String> task2 =
50-
taskService.executeTaskAsync(() -> activity.createTask("TODO 2"), taskToken());
50+
taskService.executeTaskAsync(() -> activity.createTask("TODO 2"), taskToken.getNext());
5151

5252
logger.info("Awaiting for two tasks to get completed");
5353
// Block execution until both tasks complete
@@ -56,18 +56,22 @@ public void execute() {
5656

5757
logger.info("About to create one blocking task");
5858
// Blocking invocation
59-
taskService.executeTask(() -> activity.createTask("TODO 3"), taskToken());
59+
taskService.executeTask(() -> activity.createTask("TODO 3"), taskToken.getNext());
6060
logger.info("Task completed");
6161
logger.info("Completing workflow");
6262
}
6363

64-
private final AtomicInteger atomicInteger = new AtomicInteger(1);
64+
private static class TaskToken {
6565

66-
private String taskToken() {
67-
return Workflow.getInfo().getWorkflowId()
68-
+ "-"
69-
+ Workflow.currentTimeMillis()
70-
+ "-"
71-
+ atomicInteger.getAndIncrement();
66+
private int taskToken = 1;
67+
68+
public String getNext() {
69+
70+
return Workflow.getInfo().getWorkflowId()
71+
+ "-"
72+
+ Workflow.currentTimeMillis()
73+
+ "-"
74+
+ taskToken++;
75+
}
7276
}
7377
}

0 commit comments

Comments
 (0)