Skip to main content

How to set ChildWorkflowOptions in Java

Set Child Workflow specific options with the ChildWorkflowOptions.Builder class and methods.

OptionRequiredType
NamespaceNoString
WorkflowIdNoString
ParentClosePolicyNoChildWorkflowOptions.Builder
WorkflowIdReusePolicyNoWorkflowIdReusePolicy
WorkflowExecutionTimeoutNoDuration
WorkflowRunTimeoutNoDuration
WorkflowTaskTimeoutNoDuration
RetryOptionsNoRetryOptions
CronScheduleNoString
MemoNoString
SearchAttributesNoMap<String, Object>

Namespace

  • Type: String
  • Default: Inherits the namespace value set from the parent Workflow.
public void parentWorkflow() {
ChildWorkflowOptions options = ChildWorkflowOptions.newBuilder()
.setNamespace("childWorkflowNamespace")
.build();
GreetingChild child = Workflow.newChildWorkflowStub(GreetingChild.class, options);
}

See What is a Namespace?

WorkflowId

  • Type: String
  • Default: none
 private void parentWorkflow() {
ChildWorkflowOptions options =
ChildWorkflowOptions.newBuilder()
.setWorkflowId("childWorkflow1")
.build();

// Get the Child Workflow stub
ChildWorkflow child = Workflow.newChildWorkflowStub(ChildWorkflow.class, childWorkflowOptions);
// invoke Child Workflow and wait for it to complete
child.executeChild();
}

See What is a WorkflowId?

ParentClosePolicy

Set Parent Close Policy on an instance of ChildWorkflowOptions using ChildWorkflowOptions.newBuilder().setParentClosePolicy.

  • Type: ChildWorkflowOptions.Builder
  • Default: None.
   public void parentWorkflow() {
ChildWorkflowOptions options =
ChildWorkflowOptions.newBuilder()
.setParentClosePolicy(ParentClosePolicy.PARENT_CLOSE_POLICY_ABANDON)
.build();
MyChildWorkflow child = Workflow.newChildWorkflowStub(MyChildWorkflow.class, options);
Async.procedure(child::<workflowMethod>, <args>...);
Promise<WorkflowExecution> childExecution = Workflow.getWorkflowExecution(child);
// Wait for child to start
childExecution.get()
}

In this example, we are:

  1. Setting ChildWorkflowOptions.ParentClosePolicy to ABANDON when creating a Child Workflow stub.
  2. Starting Child Workflow Execution asynchronously using Async.function or Async.procedure.
  3. Calling Workflow.getWorkflowExecution(…) on the child stub.
  4. Waiting for the Promise returned by getWorkflowExecution to complete. This indicates whether the Child Workflow started successfully (or failed).
  5. Completing parent Workflow Execution asynchronously.

Steps 3 and 4 are needed to ensure that a Child Workflow Execution starts before the parent closes. If the parent initiates a Child Workflow Execution and then completes immediately after, the Child Workflow will never execute.

WorkflowIdReusePolicy

  • Type: WorkflowIdReusePolicy
  • Default: enums.AllowDuplicateFailedOnly is the default value. It means that the Workflow can start a new run if the previous run failed, was canceled, or was terminated.
  • Values: AllowDuplicate allows a new run independently of the previous run closure status. RejectDuplicate doesn't allow a new run independently of the previous run closure status.
 private void parentWorkflow() {
ChildWorkflowOptions options = ChildWorkflowOptions.newBuilder()
.setWorkflowId("MyWorkflowId")
.setWorkflowRunTimeout(Duration.ofSeconds(5))
.setWorkflowIdReusePolicy(
WorkflowIdReusePolicy.WORKFLOW_ID_REUSE_POLICY_REJECT_DUPLICATE
)
.build();
// Get the Child Workflow stub
ChildWorkflow child = Workflow.newChildWorkflowStub(ChildWorkflow.class, childWorkflowOptions);
// invoke Child Workflow and wait for it to complete
child.executeChild();
}

See What is a Workflow Id Reuse Policy?

WorkflowExecutionTimeout

  • Type: time.Duration
  • Default: Unlimited
 private void parentWorkflow() {
ChildWorkflowOptions childWorkflowOptions =
ChildWorkflowOptions.newBuilder()
.setWorkflowExecutionTimeout(Duration.ofSeconds(10))
.build();
// Get the Child Workflow stub
ChildWorkflow child = Workflow.newChildWorkflowStub(ChildWorkflow.class, childWorkflowOptions);
// invoke Child Workflow and wait for it to complete
child.executeChild();
}

See What is a Workflow Execution Timeout?

WorkflowRunTimeout

private void parentWorkflow() {
ChildWorkflowOptions childWorkflowOptions =
ChildWorkflowOptions.newBuilder()
.setWorkflowRunTimeout(Duration.ofSeconds(4))
.build();
// Get the Child Workflow stub
ChildWorkflow child = Workflow.newChildWorkflowStub(ChildWorkflow.class, childWorkflowOptions);
// invoke Child Workflow and wait for it to complete
child.executeChild();
}

See What is a Workflow Run Timeout?

WorkflowTaskTimeout

  • Type: time.Duration
  • Default: 10 seconds.
  • Values: Maximum accepted value is 60 seconds.
 private void parentWorkflow() {
ChildWorkflowOptions childWorkflowOptions =
ChildWorkflowOptions.newBuilder()
.setWorkflowTaskTimeout(Duration.ofSeconds(10))
.build();
// Get the Child Workflow stub
ChildWorkflow child = Workflow.newChildWorkflowStub(ChildWorkflow.class, childWorkflowOptions);
// invoke Child Workflow and wait for it to complete
child.executeChild();
}

See What is a Workflow Task Timeout?

RetryOptions

  • Type: RetryOptions
  • Default: Null which means no retries will be attempted.
private static void parentWorkflow() {
ChildWorkflowOptions childworkflowOptions =
ChildWorkflowOptions.newBuilder()
.setWorkflowExecutionTimeout(Duration.ofSeconds(10)
.setRetryOptions(RetryOptions.newBuilder()
.build())
.build();
ChildWorkflow child = Workflow.newChildWorkflowStub(ChildWorkflow.class, ChildworkflowOptions);
child.executeChild();

See What is a Retry Policy?

CronSchedule

  • Type: String
  • Default: None
private static void parentWorkflow() {
ChildWorkflowOptions childworkflowOptions =
ChildWorkflowOptions.newBuilder()
.setCronSchedule("@every 10s")
.build();
ChildWorkflow child = Workflow.newChildWorkflowStub(ChildWorkflow.class, ChildworkflowOptions);
child.executeChild();

See Cron Schedules

Memo

  • Type: String
  • Default: None
private static void parentWorkflow() {
ChildWorkflowOptions childworkflowOptions =
ChildWorkflowOptions.newBuilder()
// You can set additional non-indexed info via Memo
.setMemo(ImmutableMap.of(
"memoKey", "memoValue"
))
.build();

See What is a Memo?

SearchAttributes

  • Type: Map<String, Object>
  • Default: None
private static void parentWorkflow() {
ChildWorkflowOptions childworkflowOptions =
ChildWorkflowOptions.newBuilder()
// You can set search attributes just like in WorkflowOptions
// make sure that these search attributes were added before
.setSearchAttributes(ImmutableMap.of("MySearchAttributeNAme", "value"))
.build();

See What is a Search Attribute?