def _connect(self): assert self.active_connections == 0 self.active_connections = 1 qs = urllib.parse.urlencode({ 'cacertfile': self.config['sslopts']['ca_certs'], 'certfile': self.config['sslopts']['certfile'], 'keyfile': self.config['sslopts']['keyfile'], 'verify': 'ignore', 'heartbeat': self.config['heartbeat'], 'connection_timeout': self.config['socket_timeout'], }) uri = '{scheme}://{username}:{password}@{host}:{port}/{virtual_host}?{query_str}'.format( scheme='amqps', username=self.config['userid'], password=self.config['password'], host=self.config['host'].split(":")[0], port=self.config['host'].split(":")[1], virtual_host=self.config['virtual_host'], query_str=qs, ) self.log.info("Initializing AMQP connection.") self.connection = rabbitpy.Connection(uri) # self.connection.connect() self.log.info("Connected. Creating channel.") # Channel and exchange setup self.channel = rabbitpy.AMQP( self.connection.channel(blocking_read=True)) self.log.info("Setting QoS.") self.log.info("Connection established. Setting up consumer.") if self.config['flush_queues']: self.log.info("Flushing items in queue.") self.channel.queue_purge(self.config['task_queue_name']) self.channel.queue_purge(self.config['response_queue_name']) self.log.info("Configuring queues.") self._setupQueues() if self.config['master']: self.in_queue = self.config['response_queue_name'] else: self.in_queue = self.config['task_queue_name'] qchan = self.connection.channel() qchan.prefetch_count(self.config['prefetch']) self.in_q = rabbitpy.Queue(qchan, self.in_queue)
def creat_a_channel(self) -> rabbitpy.AMQP: return rabbitpy.AMQP(self.connection.channel()) # 使用适配器,使rabbitpy包的公有方法几乎接近pika包的channel的方法。
def __init__(self, conn_string: str): self.__amqp = rabbitpy.AMQP(rabbitpy.Connection(conn_string).channel())
def _connect(self): assert self.active_connections.value == 0 self.active_connections.value = 1 self.connected.value = 1 self.log.info("Initializing AMQP connection.") # Connect to server # self.connection = amqp.connection.Connection(host = self.config['host'], # userid = self.config['userid'], # password = self.config['password'], # virtual_host = self.config['virtual_host'], # heartbeat = self.config['heartbeat'], # ssl = self.config['sslopts'], # connect_timeout = self.config['socket_timeout'], # read_timeout = self.config['socket_timeout'], # write_timeout = self.config['socket_timeout']) # "cert_reqs" : ssl.CERT_REQUIRED, # "ca_certs" : caCert, # "keyfile" : keyf, # "certfile" : cert, qs = urllib.parse.urlencode({ 'cacertfile' :self.config['sslopts']['ca_certs'], 'certfile' :self.config['sslopts']['certfile'], 'keyfile' :self.config['sslopts']['keyfile'], 'verify' : 'ignore', 'heartbeat' : self.config['heartbeat'], 'connection_timeout' : self.config['socket_timeout'], }) uri = '{scheme}://{username}:{password}@{host}:{port}/{virtual_host}?{query_str}'.format( scheme = 'amqps', username = self.config['userid'], password = self.config['password'], host = self.config['host'].split(":")[0], port = self.config['host'].split(":")[1], virtual_host = self.config['virtual_host'], query_str = qs, ) self.connection = rabbitpy.Connection(uri) # self.connection.connect() # Channel and exchange setup self.channel = rabbitpy.AMQP(self.connection.channel(blocking_read = True)) self.channel.basic_qos( prefetch_size = 0, prefetch_count = self.config['prefetch'], global_flag = False ) self.log.info("Connection established. Setting up consumer.") if self.config['flush_queues']: self.log.info("Flushing items in queue.") self.channel.queue_purge(self.config['task_queue_name']) self.channel.queue_purge(self.config['response_queue_name']) self.log.info("Configuring queues.") self._setupQueues() self.no_ack = not self.config['ack_rx']