def consume(self, queue, limit=None, callback=None, auto_declare=False): """consume messages in queue queue - name of queue limit - amount of messages to iterate through (default: no limit) callback - method to call when a new message is received must take two arguments: message_data, message must send the acknowledgement: message.ack() default: print message to stdout and send ack auto_declare - automatically declare the queue (default: false) """ if not callback: callback = _consume_callback consumer = Consumer(connection=self.broker, queue=queue, auto_declare=auto_declare) consumer.register_callback(callback) for message in consumer.iterqueue(limit=limit, infinite=False): consumer.receive(message.payload, message) consumer.close()
def get_responses(logger=logging): connection = DjangoBrokerConnection() consumer = Consumer(connection=connection, exchange="collector.response", queue="responses", routing_key="response") for message in consumer.iterqueue(): responses = message.payload for resp in responses: logger.debug("resp=%s" % resp) try: tag = Tag.objects.get(name=resp['name']) tag.save_with_history(resp['current_value']) except Exception as ex: logger.error(ex) #print "Could have saved '%s' for tag '%s'" % (resp['current_value'], tag.id,) message.ack() consumer.close() connection.close()
def get_responses(logger=logging): connection = DjangoBrokerConnection() consumer = Consumer(connection=connection, exchange="collector.response", queue="responses", routing_key="response") for message in consumer.iterqueue(): responses = message.payload for resp in responses: logger.debug("resp=%s" % resp ) try: tag=Tag.objects.get(name=resp['name']) tag.save_with_history(resp['current_value']) except Exception as ex: logger.error(ex) #print "Could have saved '%s' for tag '%s'" % (resp['current_value'], tag.id,) message.ack() consumer.close() connection.close()
def save_hits(): conn = DjangoAMQPConnection() consumer = Consumer(connection=conn, queue='requestrequesthitqueue', exchange='request', routing_key='request.*', exchange_type='topic') count = 0 messages = [] for message in consumer.iterqueue(): messages.append(message) request = message.decode() request.path = convert_unicode_to_string(request.path[:255]) request.data = convert_unicode_to_string(request.data[:255]) request.referer = convert_unicode_to_string(request.referer[:255]) request.user_agent =\ convert_unicode_to_string(request.user_agent[:255]) request.language = convert_unicode_to_string(request.language[:255]) request.save() count += 1 logger.info("Saved {0} requests".format(count)) [m.ack() for m in messages] logger.debug("Acknowledged all messages") consumer.close() conn.close()