如何通知ActiveMQ的所有订阅者
How to notify all subscribers of ActiveMQ
我有支持 stomp 的 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”。队列是点对点的。主题是发布和订阅,这就是你想要的。
我有支持 stomp 的 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”。队列是点对点的。主题是发布和订阅,这就是你想要的。