Search code examples
pythonactivemq-classicamqpqpid

Connecting to durable consumer - Qpid Proton Python


I'm trying to get a Qpid Proton Python client to subscribe to an ActiveMQ broker over AMQP v1.0 as a durable consumer.

I can create a durable subscription but don't quite understand how to pick up the same subscription the next time my script runs.

After each run, the ActiveMQ admin console shows that a durable subscription has been created, and it is named according to my script's input, but the next run just creates another rather than connects to the one previously created.

I think I might be missing the creation of a "ClientID" (as ActiveMQ calls it) but can't see (in the Qpid Proton documentation) how to set it.

Here's my code:-

def on_start(self, event):
    if self.subscription_name:
        logging.debug("Naming durable subscription " + self.subscription_name)
        durable = DurableSubscription()
    else:
        logging.debug("Subscription will not be durable")
        durable = None

    messaging_connection = event.container.connect(self.url)
    logging.info(messaging_connection.clientID)
    event.container.create_receiver(
        messaging_connection,
        self.resource,
        name=self.subscription_name,
        options=durable
    )
    logging.debug("Connected to " + self.url + "/" + self.resource)

Solution

  • I've solved this (with help from the Qpid users mailing list).

    As suspected, I needed to name the client connection. This is done by adding the following line of code before calling the connect method.

    event.container.container_id = __file__
    

    (NB I simply gave the connection the same name as the script)

    Another gotcha was that the event.receiver.close() method, called once my script had processed all messages, destroys the durable subscription. To leave it in place after the script has ended, use event.receiver.detach() instead.

    Mailing list thread here -> http://qpid.2158936.n2.nabble.com/Connecting-to-durable-consumer-Qpid-Proton-Python-td7659185.html