軟體版本:
apache-activemq-5.7.0-bin.tar.gz
stomp.py-3.1.1.tar.gz
:
http://activemq.apache.org/download.html
http://code.google.com/p/stomppy/downloads/list
activemq安裝:
tar -zxvf apache-activemq-5.7.0-bin.tar.gz
mv apache-activemq-5.7.0 /usr/local/activemq/
改配置:
vi /usr/local/activemq/conf/activemq.xml
chmod -R 777 /usr/local/activemq/
添加一句
<transportConnectors> <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB --> <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireformat.maxFrameSize=104857600"/> <transportConnector name="stomp+nio" uri="stomp+nio://0.0.0.0:61613"/></transportConnectors>
啟動服務:
/usr/local/activemq/bin/activemq start
檢查:
[root@localhost pythontest]# netstat -an | grep 61616
tcp 0 0 :::61616 :::* LISTEN
[root@localhost pythontest]# netstat -an | grep 61613
tcp 0 0 :::61613 :::* LISTEN
http://192.168.10.62:8161/admin
stomp安裝:
tar -zxvf stomp.py-3.1.1.tar.gz
cd stomp.py-3.1.1
python setup.py install --prefix=/usr/local/python27 (python安裝目錄)
完成。
python demo
以下轉自:http://blog.csdn.net/xiarendeniao/article/details/7053958#comments
#!/usr/bin/python## Usage: stomp_send.py <msize> <nmsgs>#import sysimport osimport loggingimport stompimport timeimport jsonlogging.basicConfig()nmsgs = int(sys.argv[1])msg = {'pid':1,'id':1,'start_at':"time",'type':'parse_title','articleid':0, 'from':1, 'to':361}dest = '/queue/worker'start = time.time()conn = stomp.Connection([('172.16.1.217', 61612)])conn.start()conn.connect(wait=True)for i in range(nmsgs):msg['articleid'] = iconn.send(json.write(msg), destination=dest)print "send one"conn.disconnect()print "OK Finished msgs %d time %f" % (nmsgs, (time.time()-start))
mq_rective.py
import sysimport osimport loggingimport stompimport jsonimport timeclass MyListener(object):def on_error(self, headers, message):print 'received an error %s' % messagedef on_message(self, headers, message):print 'received a message %s' % message#print headers['message-id']#sys.exit(0)conn.ack({'message-id':headers['message-id']})dest = '/queue/test1'logging.basicConfig()conn = stomp.Connection([('172.16.1.217', 61612)])conn.set_listener('', MyListener())conn.start()conn.connect(wait=True)conn.subscribe(destination=dest, ack='client')while True:try:time.sleep(1)except:break
運行:
[root@localhost pythontest]# python mq_send.py 4
send one
send one
send one
send one
OK Finished msgs 4 time 0.015546
[root@localhost pythontest]#
[root@localhost pythontest]# python mq_receive.py
received a message {"start_at": "time", "pid": 1, "to": 361, "articleid": 0, "from": 1, "type": "parse_title", "id": 1}
received a message {"start_at": "time", "pid": 1, "to": 361, "articleid": 1, "from": 1, "type": "parse_title", "id": 1}
received a message {"start_at": "time", "pid": 1, "to": 361, "articleid": 2, "from": 1, "type": "parse_title", "id": 1}
received a message {"start_at": "time", "pid": 1, "to": 361, "articleid": 3, "from": 1, "type": "parse_title", "id": 1}