ebin123456 / py-amqplib

Automatically exported from code.google.com/p/py-amqplib
GNU Lesser General Public License v2.1
0 stars 0 forks source link

Feature Request: Add ability to serialize/deserialize message.body #27

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Which version of amqplib are you using?
latest

Have you checked to see if there is a newer version in the "Featured
Downloads" section of the front page of this project?
Yes

Which broker are you using (RabbitMQ?) which version?
RabbitMQ 1.7.2  and/also 2.2

Which version of Python?
2.6.5

What steps will reproduce the problem?
Not all messages are text and/or Unicode.  It is straight forward to wrap 
basic_consume with your own to add some serialization (json, yaml, pickle, et 
al)  However, the interface to basic_consumer does not afford the same chance 
to deserialize the message body prior to passing the Message to the callback 
function.  This means the callback function needs to know how to deserialize 
the message body (if/when required).

Yes, I could write a wrapper/decorator for the callback function but that seems 
so sloppy and unDRY.  However, I see you not wanting to deal with the 
support/insanity that this may bring.  However, if you/we could shim 
basic_consumer to allow for a deserialization function.  Then it would be up to 
the user/developer to specify and implement their own.

Does this feature request interest you?  

Original issue reported on code.google.com by dunde...@gmail.com on 15 Feb 2011 at 6:15

GoogleCodeExporter commented 9 years ago
Sorry, but now that I had a night to sleep on it, this isn't really necessary.  
I handled it in my wrapper class.  I just shimmed the basic_consumer to call my 
__deserialize function and that function calls the users call back when it is 
finished.  i.e.

    def start_consuming(self, callback, queue_name=None, consumer_tag='consumer'):
        if hasattr(self, 'queue_name') or queue_name:
            self.callbacks[consumer_tag] = callback    
            self.channel.basic_consume(queue=getattr(self, 'queue_name', queue_name),
                                     callback=self.__deserialize,
                                     consumer_tag=consumer_tag)

    def __deserialize(self, msg):
        '''Do any necessary deserialization necesary, then call the  user
        requested call back'''

        if msg.properties['content_type'] == u'text/yaml':
            msg.body = yaml.load(msg.body)

        # Now hand the msg object off to the user specified call back
        return self.callbacks[msg.delivery_info['consumer_tag']](msg)

Now this is handled by my wrapper class and not the user callback function.

Thanks for the library!  

Original comment by dunde...@gmail.com on 16 Feb 2011 at 4:16

GoogleCodeExporter commented 9 years ago
Great, glad you got that to work.

Original comment by barry.pe...@gmail.com on 28 Mar 2011 at 7:08