简体   繁体   中英

Acking activemq using python and stomp

I have an activemq set up with a ton of api calls to Zendesk. I need to retrieve those calls and then remove them from the queue entirely. conn.ack doesn't seem to be working!

I'm using python3 and the most recent version of stomp. I used this to make the initial connection script: https://github.com/jasonrbriggs/stomp.py/wiki/Simple-Example

https://jasonrbriggs.github.io/stomp.py/api.html In this doc, it looks like you have to label the "id" tag of the .subscribe method. You call conn.ack with that id, but you also add the message id as an argument. I found out that the headers of the message are retrieved with the listener function. I printed those out and they look like this:

ID:[my workstation id].local-49557-1560302581785-5:58:-1:1:61592

I tried regex'ing out the whole string after ID:, and then I tried regexing out just the number on the very end of the string (it looks like possibly a unique number), but when I do conn.ack (matchObj.group(1), 4), the queue count doesn't change and I get no feedback as to why it doesn't.

The connection works absolutely fine so far-- I just can't send those acks.

import stomp
import time
import re

class SampleListener(object):
    def on_message(self, headers, msg):
      regex = r"ID:.*1:1:(.*)"
      print(msg)
      print(headers['message-id'])

      matchObj = re.match ( regex, headers['message-id'], re.M|re.I)
      print (matchObj.group(1))

      conn.ack (matchObj.group(1), 4)

conn = stomp.Connection10()

conn.set_listener('SampleListener', SampleListener())

conn.start()

conn.connect()

conn.subscribe('zendeskqueue', id=4, ack='client')

time.sleep(1) # secs

conn.disconnect()

The code above has no error, it just exists without output.

Acknowledging messages with stomp.py and ActiveMQ works using ack id headers['ack'] .

And you can get more verbose output by implementing on_error on your listener, and enabling debug logging.

With both, your code will look like that:

import stomp
import time
import logging

class SampleListener(object):
  def on_message(self, headers, msg):
    conn.ack(headers['ack'])

  def on_error(self, headers, body):
    print("An error occurred: headers=" + str(headers) + " ; body=" + str(body))


logging.basicConfig(level=logging.INFO)
logging.getLogger('stomp').setLevel(logging.DEBUG)

conn = stomp.Connection12()

conn.set_listener('SampleListener', SampleListener())

conn.start()

conn.connect()

conn.subscribe('zendeskqueue', id=4, ack='client')

time.sleep(1) # secs

conn.disconnect()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM