Skip to content

Commit 677f8b0

Browse files
committed
Example for tasks interaction
1 parent a87db56 commit 677f8b0

23 files changed

+593
-752
lines changed

core/src/main/java/io/temporal/samples/asyncchild/ParentWorkflowImpl.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,40 @@
2525
import io.temporal.workflow.ChildWorkflowOptions;
2626
import io.temporal.workflow.Promise;
2727
import io.temporal.workflow.Workflow;
28+
import java.util.ArrayList;
29+
import java.util.List;
2830

2931
public class ParentWorkflowImpl implements ParentWorkflow {
3032
@Override
3133
public WorkflowExecution executeParent() {
3234

33-
// We set the parentClosePolicy to "Abandon"
34-
// This will allow child workflow to continue execution after parent completes
35-
ChildWorkflowOptions childWorkflowOptions =
36-
ChildWorkflowOptions.newBuilder()
37-
.setWorkflowId("childWorkflow")
38-
.setParentClosePolicy(ParentClosePolicy.PARENT_CLOSE_POLICY_ABANDON)
39-
.build();
40-
41-
// Get the child workflow stub
42-
ChildWorkflow child = Workflow.newChildWorkflowStub(ChildWorkflow.class, childWorkflowOptions);
43-
// Start the child workflow async
44-
Async.function(child::executeChild);
45-
// Get the child workflow execution promise
46-
Promise<WorkflowExecution> childExecution = Workflow.getWorkflowExecution(child);
47-
// Call .get on the promise. This will block until the child workflow starts execution (or start
48-
// fails)
49-
return childExecution.get();
35+
final List<Promise<WorkflowExecution>> promises = new ArrayList<>();
36+
37+
for (int i = 0; i < 1000; i++) {
38+
// We set the parentClosePolicy to "Abandon"
39+
// This will allow child workflow to continue execution after parent completes
40+
ChildWorkflowOptions childWorkflowOptions =
41+
ChildWorkflowOptions.newBuilder()
42+
.setWorkflowId("childWorkflow" + Math.random())
43+
.setParentClosePolicy(ParentClosePolicy.PARENT_CLOSE_POLICY_ABANDON)
44+
.build();
45+
46+
// Get the child workflow stub
47+
ChildWorkflow child =
48+
Workflow.newChildWorkflowStub(ChildWorkflow.class, childWorkflowOptions);
49+
// Start the child workflow async
50+
Async.function(child::executeChild);
51+
// Get the child workflow execution promise
52+
Promise<WorkflowExecution> childExecution = Workflow.getWorkflowExecution(child);
53+
// Call .get on the promise. This will block until the child workflow starts execution (or
54+
// start
55+
// fails)
56+
57+
promises.add(childExecution);
58+
}
59+
60+
Promise.allOf(promises).get();
61+
62+
return promises.get(0).get();
5063
}
5164
}

core/src/main/java/io/temporal/samples/hello/HelloActivity.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.temporal.activity.ActivityMethod;
2424
import io.temporal.activity.ActivityOptions;
2525
import io.temporal.client.WorkflowClient;
26+
import io.temporal.client.WorkflowOptions;
2627
import io.temporal.serviceclient.WorkflowServiceStubs;
2728
import io.temporal.worker.Worker;
2829
import io.temporal.worker.WorkerFactory;
@@ -78,7 +79,7 @@ public interface GreetingActivities {
7879

7980
// Define your activity method which can be called during workflow execution
8081
@ActivityMethod(name = "greet")
81-
int composeGreeting(String greeting, String name);
82+
String composeGreeting(String greeting, String name);
8283
}
8384

8485
// Define the workflow implementation which implements our getGreeting workflow method.
@@ -102,12 +103,7 @@ public static class GreetingWorkflowImpl implements GreetingWorkflow {
102103
@Override
103104
public String getGreeting(String name) {
104105
// This is a blocking call that returns only after the activity has completed.
105-
106-
activities.composeGreeting("Hello", name);
107-
108-
Workflow.sleep(Duration.ofSeconds(20));
109-
110-
return "hello";
106+
return activities.composeGreeting("Hello", name);
111107
}
112108
}
113109

@@ -116,10 +112,9 @@ static class GreetingActivitiesImpl implements GreetingActivities {
116112
private static final Logger log = LoggerFactory.getLogger(GreetingActivitiesImpl.class);
117113

118114
@Override
119-
public int composeGreeting(String greeting, String name) {
115+
public String composeGreeting(String greeting, String name) {
120116
log.info("Composing greeting...");
121-
122-
return 1;
117+
return greeting + " " + name + "!";
123118
}
124119
}
125120

@@ -166,5 +161,27 @@ public static void main(String[] args) {
166161
* The started workers then start polling for workflows and activities.
167162
*/
168163
factory.start();
164+
165+
// Create the workflow client stub. It is used to start our workflow execution.
166+
GreetingWorkflow workflow =
167+
client.newWorkflowStub(
168+
GreetingWorkflow.class,
169+
WorkflowOptions.newBuilder()
170+
.setWorkflowId(WORKFLOW_ID)
171+
.setTaskQueue(TASK_QUEUE)
172+
.build());
173+
174+
/*
175+
* Execute our workflow and wait for it to complete. The call to our getGreeting method is
176+
* synchronous.
177+
*
178+
* See {@link io.temporal.samples.hello.HelloSignal} for an example of starting workflow
179+
* without waiting synchronously for its result.
180+
*/
181+
String greeting = workflow.getGreeting("World");
182+
183+
// Display workflow execution results
184+
System.out.println(greeting);
185+
System.exit(0);
169186
}
170187
}

core/src/main/java/io/temporal/samples/hello/HelloActivityCancel.java

Lines changed: 0 additions & 238 deletions
This file was deleted.

0 commit comments

Comments
 (0)