Skip to content

Commit

Permalink
updated per comments
Browse files Browse the repository at this point in the history
Signed-off-by: Tihomir Surdilovic <[email protected]>
  • Loading branch information
tsurdilo committed Feb 15, 2024
1 parent b9ac7ce commit 94be251
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -46,7 +44,7 @@ public interface SignalWithTimerWorkflow {
void execute();

@SignalMethod
void doUpdate(String value);
void newValue(String value);

@SignalMethod
void exit();
Expand All @@ -55,13 +53,17 @@ public interface SignalWithTimerWorkflow {
public static class SignalWithTimerWorkflowImpl implements SignalWithTimerWorkflow {

private Logger logger = Workflow.getLogger(SignalWithTimerWorkflowImpl.class);
private List<String> 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(
Expand All @@ -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");
Expand All @@ -100,15 +102,15 @@ 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.
// You can choose what to do in this case depending on business logic.
// 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;
}
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 94be251

Please sign in to comment.