Пример #1
0
from __future__ import absolute_import, unicode_literals

import datetime

from kombu import Connection, Exchange, Queue
from kombu.simple import SimpleQueue
from amqp_buffer import AmqpBuffer

with Connection('amqp://*****:*****@192.168.157.128:5672/') as conn:
    #simple_queue = AmqpBuffer(conn, 'simple_queue')
    for i in range(2):
        queue_name = "que"+str(i)
        exchange = Exchange(name="event1", type="direct")
        queue = Queue(queue_name, exchange=exchange, routing_key=queue_name)
        simple_queue = SimpleQueue(conn, queue)
        message = 'helloworld, sent at {0}'.format(datetime.datetime.today())
        simple_queue.put(message)
        print(queue_name,'Sent: {0}'.format(message))
        simple_queue.close()
Пример #2
0
class KombuQueueReceiver(KombuMessageQueue):
    def __init__(self, dc, exchange, exchange_type, queue, binding_key,
                 **kwargs):
        if self.class_name is None:
            self.class_name = "KombuQueueReceiver"
        KombuMessageQueue.__init__(self, dc, exchange, exchange_type, queue,
                                   binding_key, **kwargs)
        self.queue_obj = SimpleQueue(
            self.channel,
            self.Queue_obj,
        )

        self.uuid = gen_uuid()
        if not self.queue:
            raise RuntimeError("queue is required")

    def start_consuming(self):
        pass

    def dump_cache(self):
        return self.queue_obj.clear()

    def stop_consuming(self):
        self.queue_obj.close()

    @recover_connect
    def get_cached_number(self):
        return self.queue_obj.qsize()

    @recover_connect
    def get_nowait(self):
        try:
            log_message = self.queue_obj.get_nowait()
        except Empty:
            return None
        log_message.ack()
        #        p = log_message.payload # deserialized data.
        #        if isinstance(p, buffer):
        #            p = unicode(p)
        return log_message.body

    @recover_connect
    def get(self, block=True, timeout=None):
        #        logger.debug("Enter Message get callback method..")
        try:
            log_message = self.queue_obj.get(block=True, timeout=timeout)
            #            logger.debug("get return : %s, dir: %s, type: %s", log_message, dir(log_message), type(log_message))
            debug_message(log_message)
        except Empty:
            logger.debug("Empty error when get @todo infos..")
            raise Empty("{cn}[{id:#x}] is raise Empty when fetch.")


#        p = log_message.payload # deserialized data.
        log_message.ack()  # remove message from queue
        #        if isinstance(p, buffer):
        #            p = unicode(p)
        return log_message.body

    def __del__(self):
        if not self.closed:
            logger.debug(
                "=============== {class_name}[{id:#x}] Enter closing... ".
                format(class_name=self.class_name, id=id(self)))
            self.close()

    def close(self):
        logger.debug(
            "{cn}[{id:#x}] is calling KombuQueueReceiver close method".format(
                cn=self.class_name, id=id(self)))
        try:
            logger.debug(
                "{cn}[{id:#x}] is about to closing.. Queue size: {qsize}".
                format(cn=self.class_name,
                       id=id(self),
                       qsize=self.get_cached_number()))
            # clean Rabbitmq Queue
            self.dump_cache()
            logger.debug(
                "{cn}[{id:#x}] is cleared. Queue size: {qsize}".format(
                    cn=self.class_name,
                    id=id(self),
                    qsize=self.get_cached_number()))
            # stop all active consumers
            self.stop_consuming()
            # how to measure stop all consumers @todo
            # delete Rabbitmq Queue
            r = self.Queue_obj.delete()
            logger.debug(
                "{cn}[{id:#x}] delete Queue[{qid:#x}] return: {r}".format(
                    cn=self.class_name,
                    id=id(self),
                    qid=id(self.Queue_obj),
                    r=r))
        except ChannelError as e:
            if '404' not in e.message:
                logger.error("Error when {cn}[{id:#x}] close: {msg}".format(
                    cn=self.class_name,
                    id=id(self),
                    msg=traceback.format_exc()))
        KombuMessageQueue.close(self)