celery / kombu

Messaging library for Python.
http://kombu.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
2.87k stars 928 forks source link

Producer.publish ignores exchange parameter (patch included) #394

Open ceremcem opened 10 years ago

ceremcem commented 10 years ago

As of Kombu '3.0.21', following official example code does not work:

    """

    Example producer that sends a single message and exits.

    You can use `complete_receive.py` to receive the message sent.

    """
    from kombu import Connection, Producer, Exchange, Queue

    #: By default messages sent to exchanges are persistent (delivery_mode=2),
    #: and queues and exchanges are durable.
    exchange = Exchange('kombu_demo', type='direct')
    queue = Queue('kombu_demo', exchange, routing_key='kombu_demo')

    with Connection(conn_str) as connection:

        #: Producers are used to publish messages.
        #: a default exchange and routing key can also be specifed
        #: as arguments the Producer, but we rather specify this explicitly
        #: at the publish call.
        producer = Producer(connection)

        #: Publish the message using the json serializer (which is the default),
        #: and zlib compression.  The kombu consumer will automatically detect
        #: encoding, serialization and compression used and decode accordingly.
        producer.publish({'hello': 'world'},
                         exchange=exchange,
                         routing_key='kombu_demo',
                         serializer='json', compression='zlib')

I changed some library code, here is my patch:

/usr/local/lib/python2.7/dist-packages/kombu$ diff messaging.py messaging.py.cca-bak 
148,154c148
< 
<         #cca
<         self.exchange = exchange or self.exchange
<         exchange = self.exchange
< 
<         if self._channel:
<             self.revive(self._channel)

---
>         exchange = exchange or self.exchange

The example code is working now.

ask commented 10 years ago

Thanks! Not sure I understand this patch, could you use 'diff -u'?

ceremcem commented 10 years ago

You welcome. Here it is:

ceremcem@cca-erik:/usr/local/lib/python2.7/dist-packages/kombu$ diff -u messaging.py.orig messaging.py
--- messaging.py.orig   2014-08-21 11:55:12.724325610 +0300
+++ messaging.py    2014-08-21 16:04:36.186257202 +0300
@@ -145,7 +145,15 @@
         retry_policy = {} if retry_policy is None else retry_policy
         routing_key = self.routing_key if routing_key is None else routing_key
         compression = self.compression if compression is None else compression
-        exchange = exchange or self.exchange
+
+        #cca
+        self.exchange = exchange or self.exchange
+        exchange = self.exchange
+
+        if self._channel:
+            self.revive(self._channel)
+
+        #/cca

         if isinstance(exchange, Exchange):
             delivery_mode = delivery_mode or exchange.delivery_mode
ask commented 10 years ago

Hmm. Why would Producer.init have any effect on .publish, when you only specify an exchange for the latter?

ask commented 10 years ago

Oh, I was confused. I'm sorry.

I don't see that offending line in the tag for this version: https://github.com/celery/kombu/blob/v3.0.21/kombu/messaging.py