ubuntu12.04 安装 rabbitmq与测试

#sudo vi /etc/apt/sources.list
添加
deb http://www.rabbitmq.com/debian/ testing main

#wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
#sudo apt-key add rabbitmq-signing-key-public.asc
#sudo apt-get update
#sudo apt-get install rabbitmq-server
#sudo service rabbitmq-server start
#ulimit -n 1024
#sudo pip pika #如果没有安装pip的同学,请百度一下安装方法

#vi send.py    #生产者程序

#!/usr/bin/env python
import pika


connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()


channel.queue_declare(queue='hello')


channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()


#vi receive.py  #消费者程序

#!/usr/bin/env python
import pika


connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()


channel.queue_declare(queue='hello')


print ' [*] Waiting for messages. To exit press CTRL+C'


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


channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)


channel.start_consuming()

测试结果

 $ python send.py
 [x] Sent 'Hello World!'

 $ python receive.py
 [*] Waiting for messages. To exit press CTRL+C
 [x] Received 'Hello World!'
发布了112 篇原创文章 · 获赞 37 · 访问量 68万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章