Skip to content

Commit f95c716

Browse files
authored
Merge pull request #77 from uber/async-child
Add example to start child workflow in async style
2 parents 4000fff + 9e5e415 commit f95c716

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/main/java/com/uber/cadence/samples/spring/workflows/impl/ParentWorkflowImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

1818
package com.uber.cadence.samples.spring.workflows.impl;
1919

20+
import com.uber.cadence.WorkflowExecution;
2021
import com.uber.cadence.samples.spring.models.SampleMessage;
2122
import com.uber.cadence.samples.spring.workflows.ChildWorkflow;
2223
import com.uber.cadence.samples.spring.workflows.ParentWorkflow;
24+
import com.uber.cadence.workflow.Async;
25+
import com.uber.cadence.workflow.Promise;
2326
import com.uber.cadence.workflow.Workflow;
2427

2528
public class ParentWorkflowImpl implements ParentWorkflow {
@@ -29,4 +32,20 @@ public String getGreetingInParent(SampleMessage sampleMessage) {
2932
ChildWorkflow childWorkflow = Workflow.newChildWorkflowStub(ChildWorkflow.class);
3033
return childWorkflow.greetInChild(sampleMessage.GetMessage());
3134
}
35+
36+
// This example allows parent workflow returns first and leave child workflow keep running.
37+
private String runChildWorkflowAsync(SampleMessage sampleMessage) {
38+
ChildWorkflow childWorkflow = Workflow.newChildWorkflowStub(ChildWorkflow.class);
39+
// non-blocking call that starts the child workflow
40+
Async.function(childWorkflow::greetInChild, sampleMessage.GetMessage());
41+
42+
// Directly invoking child workflow method is a blocking call. It will block the thread
43+
// until child workflow completes. In this example, parent workflow will immediately
44+
// and keep child workflow running.
45+
Promise<WorkflowExecution> childWorkflowPromise = Workflow.getWorkflowExecution(childWorkflow);
46+
// block until child workflow started, otherwise parent workflow may completes first
47+
// before child workflow even starts.
48+
childWorkflowPromise.get();
49+
return "child can keep running, parent return first";
50+
}
3251
}

0 commit comments

Comments
 (0)