Can Signal trigger Activity and keep strict order?

Yes, it is conceptually possible to have something like @OneByOneSignalMethod. But the whole thing can be implemented in a few lines of code it is not really needed.

Here is an example that doesn’t even need a blocking queue:

  public static class GreetingWorkflowImpl implements GreetingWorkflow {

    private final Queue<String> messageQueue = new ArrayDeque<>();
    private boolean exit = false;

    @Override
    public void getGreetings() {
      List<String> receivedMessages = new ArrayList<>(10);

      while (true) {
        Workflow.await(() -> !messageQueue.isEmpty() || exit);
        if (messageQueue.isEmpty() && exit) {
          break;
        }
        String message = messageQueue.poll();
        // process message 
        ...
      }
    }

    @Override
    public void waitForName(String name) {
      messageQueue.add("Hello " + name + "!");
    }

    @Override
    public void exit() {
      exit = true;
    }
  }

The whole sample is here.

1 Like