import stomp
import time
queue_name = '/queue/PMQ Python Queue'
topic_name = '/topic/PMQ Python Topic'
listener_name = 'SampleListener'
class SampleListener(object):
def on_message(self, headers, message):
print 'headers: %s' % headers
print 'message: %s' % message
def send_to_queue(msg):
conn = stomp.Connection10([('127.0.0.1',61613)])
conn.start()
conn.connect()
conn.send(queue_name, msg)
conn.disconnect()
def send_to_topic(msg):
conn = stomp.Connection10([('127.0.0.1',61613)])
conn.start()
conn.connect()
conn.send(topic_name, msg)
conn.disconnect()
def receive_from_queue():
conn = stomp.Connection10([('127.0.0.1',61613)])
conn.set_listener(listener_name, SampleListener())
conn.start()
conn.connect()
conn.subscribe(queue_name)
time.sleep(1)
conn.disconnect()
def receive_from_topic():
conn = stomp.Connection10([('127.0.0.1',61613)])
conn.set_listener(listener_name, SampleListener())
conn.start()
conn.connect()
conn.subscribe(topic_name)
while 1:
send_to_topic('PMQ Python topic')
time.sleep(3)
conn.disconnect()
if __name__=='__main__':
send_to_queue('PMQ Python queue')
receive_from_queue()
receive_from_topic()