Rabbitmq交换与队列

时间:2017-04-03 02:01:00

标签: rabbitmq

我尝试使用rabbitmq使用amqp进行一些基本测试。我用pika写了一个简单的python脚本来创建一个amqp客户端。我首先将消息直接发送到队列。这很好用,并且能够使用web rabbitmq管理工具以及" rabbitmqctl list_queues"来监控性能。我接下来尝试使用多个队列向交换机发送消息,接收方侦听所有队列。我很困惑,因为当我使用交换时,我无法再在管理工具或rabbitmqctl中看到队列。如何在使用交换时监控队列?

###########################################
queue send code:
###########################################
channel.queue_declare(queue='hello')

msgCnt = 1

while True:
    msg = "'Hello World! Msg Count: "+str(msgCnt)+"'"
    channel.basic_publish(exchange='',
                      routing_key='hello',
                      body=msg)
    print(" [x] Sent " + msg)
    msgCnt = msgCnt + 1
    time.sleep(1)

###########################################
exchange send code:
###########################################
queue = sys.argv[1]   #passing test.0001, test.0002 in diff clients.
channel.exchange_declare(exchange='test', type='topic')

msgCnt = 1

while True:
    msg = "'Hello World! Msg Count: "+str(msgCnt)+"'"
    channel.basic_publish(exchange='test',
                      routing_key=queue,
                      body=msg)
    print(" [x] Sent " + msg)
    msgCnt = msgCnt + 1
    time.sleep(1)

###########################################
consumer code:
###########################################
channel.exchange_declare(exchange='test',
                         type='topic')

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

binding_keys = sys.argv[1:]

for binding_key in binding_keys:
    channel.queue_bind(exchange='test',
                       queue=queue_name,
                       routing_key=binding_key)

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

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

0 个答案:

没有答案