How to schedule delay message in activemq using python stomp.py client

1.1k views Asked by At

I am using sompt.py to access my ActiveMQ server.

I created client and send data. But I have to Schedule Delay Message.

import time
import sys

import stomp

class MyListener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error "%s"' % message)
    def on_message(self, headers, message):
        print('received a message "%s"' % message)

conn = stomp.Connection()
conn.set_listener('', MyListener())
conn.start()
conn.connect('admin', 'password', wait=True)

conn.subscribe(destination='/queue/test', id=1, ack='auto')

conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test')

time.sleep(2)
conn.disconnect()

I check JAVA example, where user can pass headers. send also take headers as argument in stomp.py, but I dont know which key I have to pass in it.

stomp.constants dont have any headers for delay.

Tried the AMQ_SCHEDULED_DELAY headers but seems not working.

import time
import sys

import stomp

class MyListener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error "%s"' % message)
    def on_message(self, headers, message):
        print "Time for message receive: %s", time.strftime('%H:%M:%S')
        print('received a message "%s"' % message)

conn = stomp.Connection()
conn.set_listener('', MyListener())
conn.start()
conn.connect(wait=True)

conn.subscribe(destination='/queue/test', id=1, ack='auto')

print "Time for send message: %s", time.strftime('%H:%M:%S')
conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test', headers={'AMQ_SCHEDULED_DELAY': 100000})

time.sleep(2)
conn.disconnect()

Output:

test@localhost$ python /tmp/test.py this is test
Time for send message: %s 14:03:34
Time for message receive: %s 14:03:34
received a message "this is test"
1

There are 1 answers

8
Tim Bish On

The values for sending scheduled messages over STOMP match the definitions of the values in the Java code for the broker, but as a quick reference you can look at the Apache NMS STOMP site for a quick example.

The values are as follows:

"AMQ_SCHEDULED_DELAY" = delay in milliseconds
"AMQ_SCHEDULED_PERIOD" = repeat period in milliseconds
"AMQ_SCHEDULED_REPEAT" = repeat count
"AMQ_SCHEDULED_CRON" = cron entry such as "0 * * * *"