2

如何根据信号更改节奏工作流程中的睡眠持续时间?这是使用java客户端的正确方法吗?

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;
}

我还没有找到取消和重新安排工作流睡眠计时器的其他可能性

4

1 回答 1

2

以下是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;
  }
}
于 2020-03-30T23:08:14.993 回答