我的扭曲必须不时向rabbitmq发布消息。但在测试过程中,出现了以下问题:
ConnectionClosed: timeout
ConnectionClosed: (-1, "ConnectionResetError(54, 'Connection reset by peer')")
ChannelClosed
mq = MessageQueue()
mq.declare_direct_exchange("exchange")
mq.publish_message(message)
此连接需要满足以下要求:
不会断开连接。
断开连接时可以自动重新连接。
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")
我将通道设置为类属性导致以后创建的mq实例不必重新创建连接。
var test = [ '/', '/login', '//', '/login/login' ];
var reForFail = /(\/(?:login)?)\1$/;
var reForMatch = /\/(?:login)?$/;
console.log(test.map(e => !reForFail.test(e) && reForMatch.test(e)));