【问题标题】:How to notify all subscribers of ActiveMQ如何通知 ActiveMQ 的所有订阅者
【发布时间】:2017-04-22 14:23:13
【问题描述】:

我有支持 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 消息的当前配置只需要一个客户端。但不是这个客户端必须处理消息的事实。

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

【问题讨论】:

    标签: python-2.7 activemq stomp


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-09
      • 2020-03-29
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多