Search code examples
mqttactivemq-artemis

ActiveMQ Artemis: prefer available consumers in MQTT shared subscriptions


I have an ActiveMQ Artemis broker with two MQTT consumers subscribed to topic $share/tasks, with all parts configured to use QoS 2. Messages are distributed among the consumers following the round-robin algorithm, without regards for consumer availability. As such, the following problematic situation can happen:

  • Broker sends 1 slow task, then 2 quick tasks
  • Consumer #1 receives and begins the slow task
  • Consumer #2 receives and completes a quick task
  • The remaining quick task is assigned to consumer #1, so even though consumer #2 is free, the quick task is blocked

I was hoping manual acknowledgment would be the answer here, only acknowledging the slow task after completion, but messages keep being assigned to consumer #1 even if it has not acknowledged prior ones.


Solution

  • The messages keep being assigned to consumer #1 even if it has not acknowledged prior ones might have the following reasons:

    1. You are using QoS 0 and manual acknowledgment only works with QoS 1 and 2. (based on the https://hivemq.github.io/hivemq-mqtt-client/docs/advanced-usage/ reference you provided)
    2. You have "receiveMaximum" > 1 (receiveMaximum is the maximum number of unacknowledged PUBLISH messages the client can accommodate).

    By using QoS 1 or 2 and receiveMaximum=1 you can ensure a consumer does not receive another message until it finishes the current one.