rabbitmq安装使用

使用 http://www.open-open.com/lib/view/open1325131828249.html

ubuntu:
apt-get install erlang-nox
sudo apt-get install rabbitmq-server
启动
/etc/init.d/rabbitmq-server start|stop|restart (模式)

创建目录
sudo rabbitmqctl add_vhost /pyhtest
创建用户名
sudo rabbitmqctl add_user pyh pyh1234
设置用户权限
sudo rabbitmqctl set_permissions -p /pyhtest pyh “.*” “.*” “.*”

启动 sudo  ./bin/rabbitmq-server

python 调用
下载py-amqplib https://pypi.python.orgpypi?:action=display&name=amqplib
sudo chmod 777 setup.py
安装
python setup.py install

生产者

from amqplib import client_0_8 as amqp
import sys

filelist=[]

def readfiletolist(filename):
    file = open(filename)
    for line in file.readlines():
     line=line.strip('\n')
     filelist.append(line)
#     print "line" + line


conn = amqp.Connection(host="localhost:5672", userid="guest", password="guest", virtual_host="/", insist=False)
chan = conn.channel()

#readfiletolist("1.txt")
readfiletolist("/home/tanbo/stockdata/2014-06-27.txt")
for i in filelist:
        #msg = amqp.Message(i)
#msg = amsg = amqp.Message(sys.argv[1])
        msg = amqp.Message(i)
        msg.properties["delivery_mode"] = 2
        chan.basic_publish(msg,exchange="sorting_room",routing_key="jason")

chan.close() 
conn.close()

 

消费者

from amqplib import client_0_8 as amqp

conn = amqp.Connection(host="localhost:5672", userid="guest", password="guest", virtual_host="/", insist=False)
chan = conn.channel()

chan.queue_declare(queue="po_box", durable=True, exclusive=False, auto_delete=False)
chan.exchange_declare(exchange="sorting_room", type="direct", durable=True, auto_delete=False,)

chan.queue_bind(queue="po_box", exchange="sorting_room", routing_key="jason")

def recv_callback(msg):
    print 'Received: ' + msg.body + ' from channel #' + str(msg.channel.channel_id)

chan.basic_consume(queue='po_box', no_ack=True, callback=recv_callback, consumer_tag="testtag")
while True:
    chan.wait()
chan.basic_cancel("testtag")


chan.close()
conn.close()

 

 

 

 

posted @ 2014-07-02 17:33  谭志宇  阅读(1958)  评论(1编辑  收藏  举报