ActiveMQ在python中的基本用法

介绍

Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件,很适合用来做分布式消息队列。

安装与配置

官网:ActiveMQ (apache.org)。目前有两个版本,经典版和artemis版本,这里以经典版未例子。

使用的是5.16.5,依赖版本是JAVA8,https://www.apache.org/dyn/closer.cgi?filename=/activemq/5.16.5/apache-activemq-5.16.5-bin.zip&action=download。

端口修改:conf/activemq.xml/transportConnectors

账号密码修改:conf/user.properties

添加认证密码:conf/activemq.xml/broker

        <plugins>
            <simpleAuthenticationPlugin>
                <users>
                   <authenticationUser username="user" password="123" groups="users,admins"/>
                </users>
            </simpleAuthenticationPlugin>
        </plugins>

支持用户界面,以及WEB配置,打开/bin/win64/activemq.bat允许,访问以下地址即可进行WEB配置:http://localhost:8161/admin/

python的基本使用


# -*- coding: utf-8 -*-
"""
python 3.7, 2022年10月6日
"""

# -*-coding:utf-8-*-
import stomp
import time
 
 
queue_name = '/queue/SampleQueue'
topic_name = '/topic/SampleTopic'
listener_name = 'SampleListener'
post=61613

class SampleListener(object):
    def on_message(self, message):
        print ('message: %s' % message)
 
# 推送到队列queue
def send_to_queue(msg):
    conn = stomp.Connection10([('127.0.0.1',post)])
    conn.connect('user', '123', wait=True)
    conn.send(queue_name, msg)
    conn.disconnect()
 
#推送到主题
def send_to_topic(msg):
    conn = stomp.Connection10([('127.0.0.1',post)])
    conn.connect('user', '123', wait=True)
    conn.send(topic_name, msg)
    conn.disconnect()
 
##从队列接收消息
def receive_from_queue():
    conn = stomp.Connection10([('127.0.0.1',61613)])
    conn.set_listener(listener_name, SampleListener())
    conn.connect('user', '123', wait=True)
    conn.subscribe(queue_name)
    time.sleep(1) # secs
    conn.disconnect()
 
##从主题接收消息
def receive_from_topic():
    conn = stomp.Connection10([('127.0.0.1',post)])
    conn.set_listener(listener_name, SampleListener())
    conn.connect('user', '123', wait=True)
    conn.subscribe(topic_name)
    while 1:
        send_to_topic('topic')
        time.sleep(3) # secs
    conn.disconnect()
 
if __name__=='__main__':
    send_to_queue('len 123')
    receive_from_queue()
    send_to_topic('len 345')
    receive_from_topic()
posted @   阿涛在此  阅读(627)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示