How to change the sleep duration in cadence workflow based on a signal? Is it a proper way using java client?
private int timeout;
@Override
@WorkflowMethod
public void sleepAndWakeUp(int sleepTimeout) {
this.timeout = sleepTimeout;
sleep();
System.out.println("Woken up after " + this.timeout + " s sleep");
}
private void sleep() {
int currentTimeout = this.timeout;
Workflow.await(Duration.ofSeconds(this.timeout), () -> {
boolean cancelTimer = currentTimeout != this.timeout;
if(cancelTimer) {
sleep();
}
return cancelTimer;
});
}
@Override
@SignalMethod
public void snooze(int sleepTimeout) {
this.timeout = sleepTimeout;
}
I have not found other possibility to cancel and reschedule a workflow sleep timer
Here is the code from the Temporal Java Samples:
public final class UpdatableTimer {
private long wakeUpTime;
private boolean wakeUpTimeUpdated;
public void sleepUntil(long wakeUpTime) {
this.wakeUpTime = wakeUpTime;
do {
wakeUpTimeUpdated = false;
Duration sleepInterval = Duration.ofMillis(this.wakeUpTime - Workflow.currentTimeMillis());
if (!Workflow.await(sleepInterval, () -> wakeUpTimeUpdated)) {
break;
}
} while (wakeUpTimeUpdated);
}
public void updateWakeUpTime(long wakeUpTime) {
this.wakeUpTime = wakeUpTime;
this.wakeUpTimeUpdated = true;
}
public long getWakeUpTime() {
return wakeUpTime;
}
}