Using MapMessage with ActiveMQ with a Python Stomp.py consumer

Out of the box a STOMP consumer on an ActiveMQ broker will be able to receive TextMessages, but MapMessages will arrive without content. This is because we need to specify a converter to ActiveMQ(?) which we can do in the subscription setup, in a similar way that this thread discusses sending MapMessages from the Python end. Unfortunately there is a little bit of manual handling, because the best the client library is do is deliver a string, so you’ll have to handle the deserialisation.

The following example uses JSON as the encoding. If you change the transformation to jms-map-xml you can get the encoding as XML.

import getopt
import time
import logging

class SimpleListener(object):
    def on_error(self, headers, message):
        print('received an error %s'%message)
    def on_message(self, headers, message):
        print('received a message %s (%s)'%(message, headers))

def listen():
    con = stomp.Connection(host_and_ports=[('localhost', 61613)])
    con.set_listener('', SimpleListener())
    con.start()
    con.connect()
    
    con.subscribe(destination='/queue/test', id=1, ack='auto', headers={'transformation' : 'jms-map-json'})

There is no simple way to automatically detect the encoding, so we rely on the data travelling through a given topic or queue to be in a consistent format. Also, the JSON encoding is a little strange, but not incomprehensible.

Broker config, sender example, and python sample code is available here.

This entry was posted in python, Uncategorized. Bookmark the permalink.

Leave a Reply