Example #1
0
class GraysonAMQP (object):

    def __init__(self, amqpSettings):
        logger.debug ("amqpsettings: %s", amqpSettings)
        self.amqpSettings = amqpSettings

    def connect (self, hostname="127.0.0.1"):
        self.channel = None
        logger.info ("grayson:amqp:connect host: %s, port: %s",
                     self.amqpSettings.hostname,
                     self.amqpSettings.port)
        connectionParameters = ConnectionParameters (host         = self.amqpSettings.hostname,
                                                     virtual_host = self.amqpSettings.vhost,
                                                     port         = self.amqpSettings.port)
        self.connection = AsyncoreConnection (connectionParameters, self.on_connected)

    def setSettings (self, settings):
        self.amqpSettings = settings

    def setQueueDeclaredCallback (self, queueDeclaredCallback):
        self.queueDeclaredCallback = queueDeclaredCallback

    def ioloop (self):
        self.connect ()
        try:
            self.connection.ioloop.start ()
        except KeyboardInterrupt:
            self.connection.close ()
            self.connection.ioloop.start ()

    def on_connected (self, connection):
        connection.channel (self.on_channel_open)

    def on_channel_open (self, channel_):
        self.channel = channel_
        logger.info ("grayson:amqp:open-channel: (%s)", self.amqpSettings.queue.name)
        try:
            self.channel.queue_declare (queue       = self.amqpSettings.queue.name,
                                        passive     = False,
                                        durable     = False,
                                        exclusive   = False,
                                        auto_delete = False,
                                        callback    = self.on_queue_declared)
        except:
            traceback.print_exc ()

    def on_queue_declared (self, frame):
        try:
            logger.info ("grayson:amqp:queue-declared: %s", self.amqpSettings.queue)
            self.queueDeclaredCallback (frame)
        except:
            trackeback.print_exc ()
        finally:
            pass
Example #2
0
 def connect (self, hostname="127.0.0.1"):
     self.channel = None
     logger.info ("grayson:amqp:connect host: %s, port: %s",
                  self.amqpSettings.hostname,
                  self.amqpSettings.port)
     connectionParameters = ConnectionParameters (host         = self.amqpSettings.hostname,
                                                  virtual_host = self.amqpSettings.vhost,
                                                  port         = self.amqpSettings.port)
     self.connection = AsyncoreConnection (connectionParameters, self.on_connected)