Search code examples
python-3.xrabbitmqtask-queuepikaconnexion

Rabbitmq keep request after stopping rabitmq procces and queue


I make a connection app with rabbitmq, it works fine but when I stop rabbitmq process all of my request get lost, I want even after killing rabitmq service, my requests get saved and after restart rabitmq service, all of my request return to their own places.

Here is my rabitmq.py:

import pika
import SimilarURLs


data = ''

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()


def rabit_mq_start(Parameter):
    channel.queue_declare(queue='req')
    a = (take(datas=Parameter.decode()))
    channel.basic_publish(exchange='',
                          routing_key='req',
                          body=str(a))
    print(" [x] Sent {}".format(a))
    return a


channel.start_consuming()


def take(datas):
    returns = SimilarURLs.start(data=datas)
    return returns

In addition, I'm sorry for writing mistakes in my question.


Solution

  • You need to enable publisher confirms (via the confirm_delivery method on your channel object). Then your application must keep track of what messages have been confirmed as published, and what messages have not. You will have to implement this yourself. When RabbitMQ is stopped and started again, your application can re-publish the messages that weren't confirmed.

    It would be best to use the asynchronous publisher example as a guide. If you use BlockingConnection you won't get the async notifications when a message is confirmed, defeating their purpose.