class Rabbit_send(object):
    def __init__(self):
        self.__rabbit = Rabbit(host="localhost")

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.__rabbit.close()

    def simple_mode(self, body, queue_name="simple", is_msg_durable=False):
        self.__rabbit.declare_queue(queue_name=queue_name,
                                    durable=is_msg_durable)  # 队列持久化
        self.__rabbit.publish(body=str(body),
                              exchange_name="",
                              routing_key=queue_name,
                              is_msg_durable=is_msg_durable)  #消息持久化

    def fanout_mode(self, body, exchange_name):
        self.__rabbit.declare_exchange(exchange_name=exchange_name,
                                       exchange_type="fanout")
        self.__rabbit.publish(body=str(body),
                              routing_key="",
                              exchange_name=exchange_name)

    def direct_mode(self, body, exchange_name, routing_key):
        self.__rabbit.declare_exchange(exchange_name=exchange_name,
                                       exchange_type="direct")
        self.__rabbit.publish(body=str(body),
                              exchange_name=exchange_name,
                              routing_key=routing_key)

    def rpc_mode(self, callback=None):
        result = self.__rabbit.declare_queue_exclusive()
        self.callback_queue = result.method.queue
        receive_callback = callback if callback is not None else self.on_rpc_response
        self.__rabbit.consume(receive_callback,
                              queue_name=self.callback_queue,
                              no_ack=True)

    def on_rpc_response(self, ch, method, props, body):
        if self.corr_id == props.correlation_id:
            self.__rabbit.response = body

    def rpc_call(self, body, routing_key="rpc_queue"):
        self.corr_id = str(uuid.uuid4())
        return self.__rabbit.call(self.corr_id,
                                  self.callback_queue,
                                  body,
                                  routing_key=routing_key)
Exemple #2
0
class Rabbit_receive(object):
    def __init__(self):
        self.__rabbit = Rabbit(host="localhost")

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.__rabbit.close()

    def simple_mode(self, callback=None, queue_name="simple", durable=False):
        self.__rabbit.declare_queue(queue_name=queue_name, durable=durable)
        receive_callback = callback if callback is not None else self.on_simple_mode_callback
        self.__rabbit.consume(receive_callback,
                              queue_name=queue_name,
                              no_ack=True)
        self.__rabbit.start()

    def on_simple_mode_callback(self, ch, method, properties, body):
        print(" [x] Received %r" % body)

    def fanout_mode(self, callback=None, exchange_name="logs"):
        self.__rabbit.declare_exchange(exchange_name=exchange_name)
        result = self.__rabbit.declare_queue_exclusive()
        queue_name = result.method.queue
        self.__rabbit.bind(exchange=exchange_name, queue_name=queue_name)
        receive_callback = callback if callback is not None else self.on_simple_mode_callback
        self.__rabbit.consume(receive_callback,
                              queue_name=queue_name,
                              no_ack=True)
        self.__rabbit.start()

    def direct_mode(self,
                    callback=None,
                    exchange_name="direct_logs",
                    binding_keys=[]):
        self.__rabbit.declare_exchange(exchange_name=exchange_name,
                                       exchange_type="direct")
        result = self.__rabbit.declare_queue_exclusive()
        queue_name = result.method.queue
        for binding_key in binding_keys:
            self.__rabbit.bind(exchange=exchange_name,
                               queue_name=queue_name,
                               routing_key=binding_key)
        receive_callback = callback if callback is not None else self.on_simple_mode_callback
        self.__rabbit.consume(receive_callback,
                              queue_name=queue_name,
                              no_ack=True)
        self.__rabbit.start()

    def rpc_mode(self, callback=None, queue_name="rpc_queue"):
        self.__rabbit.declare_queue(queue_name=queue_name)
        self.__rabbit.qos()
        receive_callback = callback if callback is not None else self.on_rpc_mode_callback
        self.__rabbit.consume(receive_callback, queue_name=queue_name)
        self.__rabbit.start()

    def on_rpc_mode_callback(self, ch, method, props, body):
        print(" [.] received one msg (%s)" % body)
        response = body
        ch.basic_publish(exchange='',
                         routing_key=props.reply_to,
                         properties=pika.BasicProperties(
                             correlation_id=props.correlation_id),
                         body=str(response))
        ch.basic_ack(delivery_tag=method.delivery_tag)