How to set ChildWorkflowOptions in Java
Set Child Workflow specific options with the ChildWorkflowOptions.Builder class and methods.
| Option | Required | Type | 
|---|---|---|
| Namespace | No | String | 
| WorkflowId | No | String | 
| ParentClosePolicy | No | ChildWorkflowOptions.Builder | 
| WorkflowIdReusePolicy | No | WorkflowIdReusePolicy | 
| WorkflowExecutionTimeout | No | Duration | 
| WorkflowRunTimeout | No | Duration | 
| WorkflowTaskTimeout | No | Duration | 
| RetryOptions | No | RetryOptions | 
| CronSchedule | No | String | 
| Memo | No | String | 
| SearchAttributes | No | Map<String, Object> | 
Namespace
- Type: String
- Default: Inherits the namespacevalue set from the parent Workflow.
public void parentWorkflow() {
   ChildWorkflowOptions options = ChildWorkflowOptions.newBuilder()
        .setNamespace("childWorkflowNamespace")
        .build();
   GreetingChild child = Workflow.newChildWorkflowStub(GreetingChild.class, options);
  }
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();
    }
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:
- Setting ChildWorkflowOptions.ParentClosePolicytoABANDONwhen creating a Child Workflow stub.
- Starting Child Workflow Execution asynchronously using Async.functionorAsync.procedure.
- Calling Workflow.getWorkflowExecution(…)on the child stub.
- Waiting for the Promisereturned bygetWorkflowExecutionto complete. This indicates whether the Child Workflow started successfully (or failed).
- 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.AllowDuplicateFailedOnlyis the default value. It means that the Workflow can start a new run if the previous run failed, was canceled, or was terminated.
- Values: AllowDuplicateallows a new run independently of the previous run closure status.RejectDuplicatedoesn't allow a new run independently of the previous run closure status.
 private void parentWorkflow() {
        ChildWorkflowOptions options = ChildWorkflowOptions.newBuilder()
        .setWorkflowId("YourWorkflowId")
        .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
- Type: time.Duration
- Default: Same as WorkflowExecutionTimeout.
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: Nullwhich 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();
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();