Proper way to call ContinueAsNew

I have a workflow that registers tasks in a queue and then triggers these tasks as a new workflow in sequential order. I wanted to use ContinueAsNew to limit the history size of the workflow managing the queue. A snippet of the code is:

public class SerializedIngestWorkflowImpl implements SerializedIngestWorkflow {

private final Queue<QueueEntry> taskQueue = new ArrayDeque<>();

public SerializedIngestWorkflowImpl() {
}

@Override
public void execute() {
	int count = 0;
	System.out.println(String.format("execute() called queue size = %s", taskQueue.size()));
	while(!taskQueue.isEmpty()) {
		QueueEntry entry = taskQueue.poll();
		System.out.println("runTask()");
		runTask(entry);
		
		// make sure that this is restarted to limit the history size
		if(count++ >= 2) {
			System.out.println(String.format("Calling ContinueAsNew queue size = %s", taskQueue.size()));
			Workflow.continueAsNew();
		}
	}
}

I’m finding that after calling Workflow.ContinueAsNew() the execute() method is called but the queue size is being printed as 0. Is there something I’m missing here - my understanding was that the state of the queue would be passed through when the workflow started again. Do I need to pass this information through as an argument to Workflow.continueAsNew()?

Workflow.continueAsNew() continues the current execution as a new run. You can pass the needed arguments to the next run with:

or

You could also look into using child workflow for your periodic logic, and use continueAsNew in the child workflow instead of the parent. From the parent workflow point of view it will be just a single child workflow invocation.

Thanks Tihomir - I’ll give this a go.