Exemple #1
0
def setup_rabbitmq(args):
    """
    Set up the AMQP server.

    :param args: will be ignored
    """
    logger.info("Connecting to RabbitMQ")
    conn = util.create_amqp_connection()
    channel = amqp.Channel(conn)
    channel.open()

    logger.info("Declaring exchanes")

    edecl = partial(channel.exchange_declare,
                    auto_delete=False,
                    durable=True)

    edecl("search", "direct")
    edecl("search.retry", "fanout")
    edecl("search.failed", "fanout")

    logger.info("Declaring queues")

    qdecl = partial(channel.queue_declare,
                    auto_delete=False,
                    durable=True)

    delq, _, _ = qdecl("search.delete")
    indq, _, _ = qdecl("search.index")
    retrq, _, _ = qdecl("search.retry", arguments={
                        "x-message-ttl":
                        4 * 60 * 60 * 1000,
                        "x-dead-letter-exchange":
                        "search"}
                        )
    failq, _, _ = qdecl("search.failed")

    logger.info("Binding queues")
    channel.queue_bind(delq, "search", "delete")
    channel.queue_bind(indq, "search", "index")
    channel.queue_bind(indq, "search", "update")
    channel.queue_bind(retrq, "search.retry")
    channel.queue_bind(failq, "search.failed")

    logger.info("Done")

    conn.close()
Exemple #2
0
 def amqp_setup(self):
     # TODO: initialize these variables in __init__
     self.amqp_conn = amqp.connection.Connection(**settings.AMQP)
     self.amqp_channel = amqp.Channel(self.amqp_conn)
     self.amqp_exchange = self.amqp_channel.exchange_declare(
         exchange=self.amqp_exchange_name,
         type=self.amqp_exchange_properties['type'],
         durable=self.amqp_exchange_properties['durable'],
         auto_delete=self.amqp_exchange_properties['auto_delete'])
     self.amqp_queue = self.amqp_channel.queue_declare(
         self.amqp_queue_name,
         durable=self.amqp_queue_properties['durable'],
         auto_delete=self.amqp_queue_properties['auto_delete']
     )
     self.amqp_channel.queue_bind(
         self.amqp_queue_name,
         exchange=self.amqp_exchange_name)
                self.configs['rbt-betrader-host-prod'],
                self.configs['rbt-betrader-port'],
                self.configs['rbt-betrader-username'],
                self.configs['rbt-betrader-password'],
                self.configs['rbt-betrader-vhost']))
        except Exception, e:
            self.logger.error(
                "Error attempting to get BT Rabbit Connection :: %r " % e)
            return False

        self.logger.info("BT Connection to rabbit server established ...")
        try:
            self.logger.info("BT Attempting to queue message")
            conn.connect()
            self.logger.info("BT Connected explicit.....")
            ch = amqp2.Channel(conn)
            ch.open()
            self.logger.info("BT Acquired a channel ....")

            msg = amqp2.basic_message.Message(json.dumps(message))
            msg.properties["content_type"] = "text/plain"
            msg.properties["delivery_mode"] = 1
            msg.properties["application_headers"] = {
                "replyRoutingKey": rkeyheader,
                "correlationId": correlationId
            }
            msg.properties["correlation_id"] = str(correlationId)

            self.logger.info(
                "Done preparing message now to publish {0}".format(msg))