如何通知所有ActiveMQ用户

时间:2016-12-07 15:07:45

标签: python-2.7 activemq stomp

我有支持stomp的ActiveMQ

<transportConnectors>
    <transportConnector name="stomp" uri="stomp://0.0.0.0:61616?maximumConnections=1000&amp;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消息的当前配置只占用一个客户端。但不是这个客户端必须处理该消息的事实。

如何广播消息?或者也许可以识别订阅的所有客户?

1 个答案:

答案 0 :(得分:2)

使用&#34; / topic / test_topic&#34;而不是&#34; / queue / test_queue&#34;。队列是点对点的。主题是发布和订阅,这是你想要的。