My favorites | Sign in
Logo
                
Search
for
Updated May 30, 2009 by jasonrbriggs
SimpleExample  
A simple example of using stomp.py.

A simple example of using stomp.py is:

import time
import sys

import stomp

class MyListener(object):
   
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()

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

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

time
.sleep(2)
conn
.disconnect()

Comment by jayrodau, Jun 08, 2009

nice!

Comment by teaplanet, Jul 02, 2009

I got error. I modified the code.

conn.set_listener('', MyListener())  # NG
conn
.add_listener(MyListener())      # OK
Comment by jasonrbriggs, Jul 10, 2009

set_listener is in the newer release (for Python 3). add_listener is the older method (Python 2)

Comment by dbaktiar, Oct 20, 2009

But this code will throw error when you run it on Python 3.x. You also need to change the print statement to use the bracket syntax. e.g.

     print 'received an error %s' % message

to

     print('received an error %s' % message)
Comment by cjalmeida, Jan 21, 2010

For STOMP newbies like myself, if you want to reliably send a message you must use receipt. Check the on_receipt listener method.

Comment by a.falanga, Feb 14, 2010

In order to can compile and run the sample you must : 1. Add in import section : import logging 2. Before : conn = stomp.Connection() add line : logging.basicConfig()


Sign in to add a comment
Powered by Google Project Hosting