I use for that stompserver. For example: lets you have three frontends.
In asynchronous function before self.write([message]) I send message to
stompserver via stom_sender function:
def stomp_sender(queue, message):
hosts = [(settings.STOMP_HOST, settings.STOMP_PORT)]
try:
conn = stomp.Connection(host_and_ports=hosts)
conn.start()
conn.connect()
# send the message
conn.send(message, destination=queue)
message = json.loads(message)
logging.info("Message with ID %s was send to stomp server",
message["id"]) conn.disconnect()
except socket.error, e:
logging.error("Message was not send, error is: %s", e)
On all frontends I have such class, that listen stomp and send messages
to each frontend:
class MainListener(ConnectionListener, PostMixin):
def __init__(self, event_id=None):
if event_id:
self.event_id = event_id
self.queues = []
def on_error(self, headers, message):
logging.error("Recieved an error for message %s", message)
def on_message(self, headers, message):
message = json.loads(message)
logging.info("Recieved message with ID: %s", message["id"])
self.new_messages([message])
And main() function that start this listener via tornado start:
def main():
tornado.options.parse_command_line()
#Start Stomp listener
try:
conn = stomp.Connection(host_and_ports=[(options.stomp_host,
options.stomp_port)]) conn.set_listener('', MainListener())
conn.start()
conn.connect()
#FIXME: Destinations should be taken from DB
conn.subscribe(destination='/blog/updates', ack='auto')
except socket.error:
logging.error("Listeer can't start, connection error is: %s",
socket.error)
tornado.locale.load_gettext_translations(os.path.join(os.path.dirname(__file__),
"translations"), 'liveblog') #Start Tornado
http_server = tornado.httpserver.HTTPServer(app.Application())
http_server.listen(options.port)
...
I hope this helps you.
--
Всего наилучшего!