Search code examples
python-2.7activemq-classicstomp

How to notify all subscribers of ActiveMQ


I have ActiveMQ with support stomp

<transportConnectors>
    <transportConnector name="stomp" uri="stomp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

The client connects to ActiveMQ, and can send messages to:

#!/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

and a few clients, whichs can receive the messages:

#!/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

The current configuration of the AMQ message takes only one client. But not the fact that this client has to handle the message.

How do I broadcast messages? Or maybe it is possible to identify all clients subscribed to?


Solution

  • Use "/topic/test_topic" instead of "/queue/test_queue". Queue is point-to-point. Topic is publish and subscribe, which is what you want.