皮卡:如何在生产者和经纪人之间建立理想的联系?

时间:2017-10-18 05:15:29

标签: python rabbitmq keep-alive pika

问题的根源

我的扭曲必须不时向rabbitmq发布消息。但在测试过程中,出现了以下问题:

  1. ConnectionClosed: timeout

  2. ConnectionClosed: (-1, "ConnectionResetError(54, 'Connection reset by peer')")

  3. ChannelClosed

  4. mq = MessageQueue() mq.declare_direct_exchange("exchange") mq.publish_message(message)

  5. 要求

    此连接需要满足以下要求:

    1. 不会断开连接。

    2. 断开连接时可以自动重新连接。

    3. 调用代码

      class MessageQueue():
      channel = None
      
      def __init__(self):
          if not MessageQueue.channel:
              credentials = pika.PlainCredentials("name", "password")
              conn_params = pika.ConnectionParameters(ip,
                                                      credentials = credentials,
                                                      heartbeat=10)
              conn_broker = pika.BlockingConnection(conn_params)
      
              MessageQueue.channel = conn_broker.channel()
      
          self.exchange_declared = False
          self.exchange_name = ""
      
      def declare_direct_exchange(self, exchange_name):
          self.exchange_name = exchange_name
          self.channel.exchange_declare(exchange=exchange_name,
                                        exchange_type="direct",
                                        passive=False,
                                        durable=True,
                                        auto_delete=False)
          self.exchange_declared = True
      
      def publish_message(self, message=""):
          if not self.exchange_declared:
              raise ExchangeNotDeclaredException("Exchange must be declared "
                                                 "before publish messages.")
          msg_props = pika.BasicProperties()
          msg_props.content_type = "text/plain"
      
          self.channel.basic_publish(body=message,
                                     exchange=self.exchange_name,
                                     properties=msg_props,
                                     routing_key="hola")
      

      MessageQueue Class

      我将通道设置为类属性导致以后创建的mq实例不必重新创建连接。

      var test = [ '/', '/login', '//', '/login/login' ];
      
      var reForFail = /(\/(?:login)?)\1$/;
      var reForMatch = /\/(?:login)?$/;
      
      console.log(test.map(e => !reForFail.test(e) && reForMatch.test(e)));
      

0 个答案:

没有答案