python利用stomp连接activemq,已验证可以连接成功

安装使用到的库

复制代码
输入如下命令安装stomp
pip install stomp.py

发送请求

复制代码
# -*-coding:utf-8-*-

import stomp
import time

# 队列名(接收方可以根据管道名来选择接受那个队列数据)
location_queue = "123456"
# 服务器ip,端口固定用这个
conn = stomp.Connection([('127.0.0.1', 61613)])
# 账号密码
conn.connect(username='admin', passcode='admin', wait=True)


def send_to_queue(msg):
    print('---------消息发送--------------')
    # body=数据, destination=根据队列名传输数据,如果队列不存在,就创建一个
    conn.send(body=str(msg), destination=location_queue)
    print(msg)

if __name__ == '__main__':
    send_to_queue('len 123')
    # receive_from_queue()
    conn.disconnect()

接收数据

复制代码
# -*-coding:utf-8-*-
import stomp


# 队列名
location_queue = "123456"
conn = stomp.Connection([('127.0.0.1', 61613)])
conn.connect(username='admin', passcode='admin', wait=True)


class SampleListener(object):
    def on_message(self, headers, message):
        print('headers: %s' % headers)
        print('message: %s' % message)

def receive_from_queue():
    # 如果接受数据,就调用这个类,里面的参数是类名和类,名称必须一致
    conn.set_listener('SampleListener', SampleListener())
    # 从选择的管道中区数据,管道名,id(随便写一个数字就行)
    conn.subscribe(location_queue, 12)
    # 不能让程序停止,负责每传一次数据都得接收一次
    while True:
        pass



if __name__ == '__main__':
    receive_from_queue()
    conn.disconnect()

注意

连接端口是61613

相关推荐
前端精髓6 小时前
结构化输出with_structured_output(抽卡)
python·langchain
尹人入圣7 小时前
目前热门的IP驱动产业新场景新工具
网络·python·网络协议·tcp/ip
Nuanyt8 小时前
Agent 学习笔记(下篇三):上线工程、进阶选择与操作界面(Stage 7 / 7.5 / 8 + 延伸路线)
笔记·python·学习·memory·aiagent·harness
吴卫斌8 小时前
定投ETF实战指南(四):行业/主题ETF——定投的“进攻端“
开发语言·python·股票·量化
2601_967987378 小时前
Python——入门级(定义功能def函数)
python·函数·可变参数·默认参数·关键字参数
专注仿真10 小时前
梯度提升类(XFBoost)算法详解与代码实例
开发语言·人工智能·python·算法·大模型训练·控制算法
外收内放11 小时前
Python基础语法练习题(75)
开发语言·python·学习
黑十12 小时前
PyTorch 报 “no CUDA GPUs are available“ 怎么办?3 步排查
服务器·网络·人工智能·pytorch·python
threelab12 小时前
开源图片去水印代码
python
taller_200013 小时前
【012】数据类型入门: Excel 数据到 Python 值的转换
python·excel·pie·py