如何通知与支持蹬地的ActiveMQ
问题描述:
的所有用户我的ActiveMQ如何通知与支持蹬地的ActiveMQ
<transportConnectors>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
客户端连接到ActiveMQ的,并可以发送邮件到:
#!/usr/bin/python2
# -*- coding: utf-8 -*-
from stompy import stomp
try:
s = stomp.Stomp(amq_ip, amq_port)
s.connect(username=amq_user, password=amq_pass) # connecting to AMQ
body = '{"sample_msg": "%s"}' % "for second client"
message = {
"destination": "/queue/test_queue",
"body": body,
"persistent": "true"
}
s.send(message) # sending message
except stomp.ConnectionError:
print u"Couldn’t connect to the STOMP server."
except stomp.ConnectionTimeoutError:
print u"Timed-out while establishing connection to the STOMP server."
except stomp.NotConnectedError:
print u"No longer connected to the STOMP server."
except Exception as e:
print e
和几个客户,伟驰能接收消息:
#!/usr/bin/python2
# -*- coding: utf-8 -*-
from stompy import stomp
import json
s = stomp.Stomp(amq_ip, amq_port)
try:
s.connect(username=amq_user, password=amq_pass)
s.subscribe({'destination': '/queue/%s' % amq_queue, 'ack': 'client'})
except Exception as e:
print "ActiveMQ error\n %s" % e
while True:
try:
frame = s.receive_frame()
body = json.loads(frame.body)
# This message for me?
if body["sample_msg"] == "for first client":
print "Its for me. I receive it"
# This message for me. I'll take it and treat
s.ack(frame)
else:
# This message is intended for someone else, and does not suit me
print "Its not for me"
except Exception as e:
print e
AMQ消息的当前配置只有一个客户端。但不是这个客户必须处理这个信息的事实。
如何广播消息?或者,也许有可能确定订阅的所有客户?
答
使用“/ topic/test_topic”而不是“/ queue/test_queue”。队列是点对点的。主题是发布和订阅,这是你想要的。