Skip to main content

Continue-As-New

View Markdown

This page covers the following for Java developers:

What is Continue-As-New?​

Continue-As-New lets a Workflow Execution close successfully and creates a new Workflow Execution. You can think of it as a checkpoint when your Workflow gets too long or approaches certain scaling limits.

The new Workflow Execution is in the same chain; it keeps the same Workflow Id but gets a new Run Id and a fresh Event History. It also receives your Workflow's usual parameters.

Use Continue-As-New with the Java SDK​

First, design your Workflow parameters so that you can pass in the "current state" when you Continue-As-New into the next Workflow run. This state is typically Optional.empty() for the original caller of the Workflow.

java-sandbox/workflows/continue-as-new/src/main/java/io/temporal/docs/workflows/continueasnew/ClusterManagerWorkflow.java

class ClusterManagerInput {
private final Optional<ClusterManagerState> state;
private final boolean testContinueAsNew;

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public ClusterManagerInput(
@JsonProperty("state") Optional<ClusterManagerState> state,
@JsonProperty("test_continue_as_new") boolean testContinueAsNew) {
this.state = state;
this.testContinueAsNew = testContinueAsNew;
}

@JsonProperty("state")
public Optional<ClusterManagerState> getState() {
return state;
}

@JsonProperty("test_continue_as_new")
public boolean isTestContinueAsNew() {
return testContinueAsNew;
}
}

@WorkflowMethod
ClusterManagerResult run(ClusterManagerInput input);

The test hook in the above snippet is covered below.

Inside your Workflow, call the continueAsNew() function with the same type. This stops the Workflow right away and starts a new one.

java-sandbox/workflows/continue-as-new/src/main/java/io/temporal/docs/workflows/continueasnew/ClusterManagerWorkflowImpl.java

Workflow.continueAsNew(
new ClusterManagerInput(Optional.of(state), input.isTestContinueAsNew()));

Considerations for Workflows with message handlers​

If you use Updates or Signals, don't call Continue-as-New from the handlers. Instead, wait for your handlers to finish in your main Workflow before you run continueAsNew.

When is it right to Continue-As-New with the Java SDK?​

Use Continue-as-New when your Workflow might hit Event History Limits.

Temporal tracks your Workflow's progress against these limits to let you know when you should Continue-as-New. Call Workflow.getInfo().isContinueAsNewSuggested() to check if it's time.

Test Continue-As-New with the Java SDK​

Testing Workflows that naturally Continue-as-New may be time-consuming and resource-intensive. Instead, add a test hook to check your Workflow's Continue-as-New behavior faster in automated tests.

For example, when testContinueAsNew == true, this sample creates a test-only variable called maxHistoryLength and sets it to a small value. A helper method in the Workflow checks it each time it considers using Continue-as-New:

java-sandbox/workflows/continue-as-new/src/main/java/io/temporal/docs/workflows/continueasnew/ClusterManagerWorkflowImpl.java

private boolean shouldContinueAsNew() {
if (Workflow.getInfo().isContinueAsNewSuggested()) {
return true;
}
// This is just for ease-of-testing. In production, we trust temporal to tell us when to
// continue as new.
if (maxHistoryLength > 0 && Workflow.getInfo().getHistoryLength() > maxHistoryLength) {
return true;
}
return false;
}