jasonrbriggs / stomp.py

“stomp.py” is a Python client library for accessing messaging servers (such as ActiveMQ or RabbitMQ) using the STOMP protocol (versions 1.0, 1.1 and 1.2). It can also be run as a standalone, command-line client for testing.
Apache License 2.0
492 stars 167 forks source link

Support for MapMessage and ObjectMessage #217

Closed aparamon closed 5 years ago

aparamon commented 6 years ago

It is proposed that stomp.py supports MapMessage and ObjectMessage formats, which correspond naturally to Python dict and (pickleable) object structures.

This can be most easily achieved by specifying header transformation=jms-map-json and applying the following serialization/deserialization code:

def to_map_json(obj):
    return json.dumps({'map':
                       {'entry':
                        [{'string': [str(key), str(value)]}
                         for key, value in obj.items()]}})

def from_map_json(body):
    return {i[0]: i[1]
            for item in json.loads(body)['map']['entry']
            for i in item.values()}
jasonrbriggs commented 5 years ago

This is activemq specific so not something I'd look to add -- particularly since it's easily added by the client app using a listener. Example here: https://github.com/jasonrbriggs/stomp.py/blob/master/stomp/test/misc_test.py#L15

gkawamoto commented 4 years ago

Just in case anyone tumbles on this issue looking for answers about why your ActiveMQ is not transforming your JSON to MapMessage when you read in Java the message you sent using stomp.py (even though you’re correctly setting the transformation header), try setting the transformation='jms-map-json' in the send method and auto_content_length=False in the Connection constructor, something like this:

import stomp

def main():
    conn = stomp.Connection(('localhost', 61616,), auto_content_length=False)
    conn.connect('user', 'password', wait=True)
    conn.send(destination='/queue/TEST', body=to_map_json({'id': '123'}), transformations='jms-map-json')

def to_map_json(obj):
    return json.dumps({'map':
                       {'entry':
                        [{'string': [str(key), str(value)]}
                         for key, value in obj.items()]}})

This works for me in the following environment:

ardtieboy commented 1 year ago

Had a similar issue here however the transformations parameter should be transformation.