title: When to send ACK or NACK on queue listener in STOMP
body: <p>The question is when to send the <code>ACK</code> or <code>NACK</code> frames to the broker. The <a href="https://stomp.github.io/stomp-specification-1.2.html#ACK" rel="nofollow noreferrer">STOMP specification</a> says:</p>
  
  <blockquote>
    <p>ACK is used to acknowledge consumption of a message from a
    subscription using client or client-individual acknowledgment. Any
    messages received from such a subscription will not be considered to
    have been consumed until the message has been acknowledged via an ACK.</p>
  </blockquote>
  
  <p>"Consumption" would mean for me "after received and processed". So I can imagine two scenarios. </p>
  
  <p>Scenario <strong>A</strong> after receiving the message:</p>
  
  <pre><code>function on_message(message)
      message.ack()
      heavy_processing(message)
  </code></pre>
  
  <p>or Scenario <strong>B</strong> after the message has been processed:</p>
  
  <pre><code>function on_message(message)
      heavy_processing(message)
      message.ack()
  </code></pre>
  
  <p>As I understood the <code>NACK</code> is to tell the broker that this listener for example has been temporary marked as inactive. And as I understood as well, the <code>NACK</code> is not to to mark a message as unprocessed because of an exception. </p>
  
  <p>So the following pseudo code would handle <code>ACK</code>, <code>NACK</code>, and exceptions correctly from my understanding:</p>
  
  <pre><code>function on_message(message)
      if online(): # checks resources etc
           message.ack()
      else:
           message.nack()
           return
      try:
          heavy_processing(message) # processing takes 5-10 minutes
      catch Exception: # could be problem with this Listener or malformed message
          message.put_to_dlq() # putting to dlq is a "manual" process
      return
  </code></pre>
  
  <p>FYI the system I'm talking about is build in Python 3.7.x with Stomp.py module and ActiveMQ.</p>