1 Reply Latest reply on Jun 26, 2019 2:14 PM by Paul Ferraro

    CommandDispatcher message ordering

    Greg Brown Newbie

      Does CommandDispatcher make any guarantees about asynchronous message ordering? In other words, if I call executeOnMember() multiple times in a row, is the receiving node guaranteed to receive the messages in the order they were sent, or is the order arbitrary (i.e. random)?

       

      Thanks,
      Greg

        • 1. Re: CommandDispatcher message ordering
          Paul Ferraro Master

          Commands issued by a single thread are sent in order, but because message reception is not single threaded, ordering on the receiver is not inherently maintained.

          You can enforce ordering per sender thread only if the sender waits for an acknowledgement that the command was received before sending the next.

          e.g.

          public class QueueCommand implements Command<Void, Queue<Object>> {
              private final Object message;
          
              Void execute(Queue<Object> queue) {
                 queue.add(this.message);
                 return null;
              }
          }
          
          Queue<Object> receivedMessages = new BlockingQueue<>();
          try (CommandDispatcher<Queue<Object>> dispatcher = factory.createDispatcher("foo", receivedMessages)) {
             for (Object message : messages) {
                dispatcher.executeOnMember(new QueueCommand<>(message), member).toCompletableFuture().join();
             }
          }