diff --git a/core/src/main/java/io/temporal/samples/hello/HelloSignalWithTimer.java b/core/src/main/java/io/temporal/samples/hello/HelloSignalWithTimer.java index 9aa738e3..c95149d6 100644 --- a/core/src/main/java/io/temporal/samples/hello/HelloSignalWithTimer.java +++ b/core/src/main/java/io/temporal/samples/hello/HelloSignalWithTimer.java @@ -28,13 +28,11 @@ import io.temporal.worker.WorkerFactory; import io.temporal.workflow.*; import java.time.Duration; -import java.util.ArrayList; -import java.util.List; import org.slf4j.Logger; /** * Sample Temporal workflow that shows receiving signals for a specific time period and then process - * last one received and continueasnew. + * last one received and continue as new. */ public class HelloSignalWithTimer { static final String TASK_QUEUE = "HelloSignalWithTimerTaskQueue"; @@ -46,7 +44,7 @@ public interface SignalWithTimerWorkflow { void execute(); @SignalMethod - void doUpdate(String value); + void newValue(String value); @SignalMethod void exit(); @@ -55,13 +53,17 @@ public interface SignalWithTimerWorkflow { public static class SignalWithTimerWorkflowImpl implements SignalWithTimerWorkflow { private Logger logger = Workflow.getLogger(SignalWithTimerWorkflowImpl.class); - private List updates = new ArrayList<>(); + private String lastValue = ""; private CancellationScope timerScope; private boolean exit = false; private boolean processedLast = false; @Override public void execute() { + // Just in case if exit signal is sent as soon as execution is started + if (exit) { + return; + } // Start timer in cancellation scope so we can cancel it on exit signal received timerScope = Workflow.newCancellationScope( @@ -84,7 +86,7 @@ public void execute() { // Note you would here call an activity to process last signal value received // For sample we just log it in workflow rather than a dummy activity try { - logger.info("Workflow processing last value received: " + updates.get(updates.size() - 1)); + logger.info("Workflow processing last value received: " + lastValue); processedLast = true; } catch (IndexOutOfBoundsException e) { logger.info("No updates received, nothing to process"); @@ -100,7 +102,7 @@ public void execute() { } @Override - public void doUpdate(String value) { + public void newValue(String value) { // Note that we can receive a signal at the same time workflow is trying to complete or // ContinueAsNew. This would cause workflow task failure with UnhandledCommand // in order to deliver this signal to our execution. @@ -108,7 +110,7 @@ public void doUpdate(String value) { // For this sample we just ignore it, alternative could be to process it or carry it over // to the continued execution if needed. if (!processedLast) { - updates.add(value); + lastValue = value; } } @@ -143,7 +145,7 @@ public static void main(String[] args) { // Send signals 2s apart 12 times (to simulate cancellation on last ContinueAsNew) for (int i = 0; i < 12; i++) { - workflow.doUpdate("Update " + i); + workflow.newValue("Value " + i); sleep(2); } sleep(1); diff --git a/core/src/test/java/io/temporal/samples/hello/HelloSignalWithTimerTest.java b/core/src/test/java/io/temporal/samples/hello/HelloSignalWithTimerTest.java index 1fa8c04b..80d5cb48 100644 --- a/core/src/test/java/io/temporal/samples/hello/HelloSignalWithTimerTest.java +++ b/core/src/test/java/io/temporal/samples/hello/HelloSignalWithTimerTest.java @@ -53,8 +53,8 @@ public void testSignalWithTimer() { .build()); WorkflowClient.start(workflow::execute); - workflow.doUpdate("1"); - workflow.doUpdate("2"); + workflow.newValue("1"); + workflow.newValue("2"); workflow.exit(); WorkflowStub.fromTyped(workflow).getResult(Void.class);