python 通过stomp协议发送message到 ActiveMQ,通过PrintWire协议接收的坑

背景

和另一个系统进行对接,使用activemq进行消息通信。对方使用java客户端监听一个topic,我们需要发送TextMessage消息,对方接收后进行处理。而我们因为系统架构的原因只能使用python进行推送,也就只能通过stomp协议发送消息。然后就遇到了问题,发送的消息在java消费者端只能解析成BinaryMessage,而发送的时候根本没有办法指定消息类型。网上搜了很久没有找到相同的情况。
根据官方通过python往ActiveMQ发送message的demo编写如下代码。
import stomp
    
def send_to_topic(msg):
    try:
        conn = stomp.Connection10([("10.10.19.200", 61613)])
        conn.start()
        conn.connect()
        conn.send('/topic/xxxx', msg, content_type="text/plain")
        conn.disconnect()
        return 1
    except Exception as e:
        # logging.error(f"send message with activemq failed, error is:{e}")
        return 0

问题解决

阅读Stomp模块的源码并没有找到指定消息类型的代码,怀疑使用python通过stomp协议根本无法指定消息类型,只能在java程序中用BinaryMessage接收然后转换为String,然后去翻了一下ActiveMQ的官方文档,有了新的发现。官方文档地址如下
http://activemq.apache.org/stomp.html

特别应该注意的是这段:

Working with JMS Text/Bytes Messages and Stomp
Stomp is a very simple protocol - that’s part of the beauty of it! As such, it does not have knowledge of JMS messages such as TextMessage’s or BytesMessage’s. The protocol does however support a content-length header. To provide more robust interaction between STOMP and JMS clients, ActiveMQ keys off of the inclusion of this header to determine what message type to create when sending from Stomp to JMS. The logic is simple:

|Inclusion of content-length header	| Resulting Message |
|yes	| BytesMessage |
|no	| TextMessage |

大概意思是Stomp是一个很简单的协议,协议中不携带TextMessage和BytesMessage相关的信息,而是通过content-length header判断消息类型的。header中有content-length则说明是BytesMessage,否则是TextMessage。
接下来的问题就简单了,发送的时候不在header中携带content-length就可以了,查看send方法的源码发现

def __init__(self, transport, auto_content_length=True):
    self.transport = transport
    self.auto_content_length = auto_content_length
    transport.set_listener('protocol-listener', self)
    self.version = '1.0'

def send(self, destination, body, content_type=None, headers=None, **keyword_headers):
    """
    Send a message to a destination.

    :param str destination: the destination of the message (e.g. queue or topic name)
    :param body: the content of the message
    :param str content_type: the content type of the message
    :param dict headers: a map of any additional headers the broker requires
    :param keyword_headers: any additional headers the broker requires
    """
    assert destination is not None, "'destination' is required"
    assert body is not None, "'body' is required"
    headers = utils.merge_headers([headers, keyword_headers])
    headers[HDR_DESTINATION] = destination
    if content_type:
        headers[HDR_CONTENT_TYPE] = content_type
    body = encode(body)
    if self.auto_content_length and body and HDR_CONTENT_LENGTH not in headers:
        headers[HDR_CONTENT_LENGTH] = len(body)
    self.send_frame(CMD_SEND, headers, body)
if self.auto_content_length and body and HDR_CONTENT_LENGTH not in headers:
        headers[HDR_CONTENT_LENGTH] = len(body)

三个条件都为true则会填充content-length,而auto_content_length是在__init__方法中传入的,默认值为True,所以只需要在创建对象的时候将该值设置为False即可。

修改后的代码如下

import stomp

def send_to_topic(msg):
    try:
        conn = stomp.Connection10([("10.10.19.200", 61613)],auto_content_length=False)
        conn.start()
        conn.connect()
        conn.send('/topic/xxxx', msg)
        conn.disconnect()
        return 1
    except Exception as e:
        # logging.error(f"send message with activemq failed, error is:{e}")
        return 0

问题解决

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,可以尝试使用pymqi和stomp.py这两个库来实现。首先安装这两个库:pip install pymqi stomp.py 然后使用pymqi连接ActiveMQ,在连接成功后,使用stomp.py订阅消息,可以按照以下代码示例来实现: ```python import stomp import pymqi host = 'localhost' port = '61613' channel = 'SYSTEM.ADMIN.SVRCONN' queue_manager = 'QM' queue_name = 'QUEUE.NAME' class MQListener(stomp.ConnectionListener): def on_message(self, headers, message): print('Received message: {}'.format(message)) def on_error(self, headers, message): print('Error received: {}'.format(message)) conn = stomp.Connection(host_and_ports=[(host, port)]) conn.set_listener('', MQListener()) queue_manager = pymqi.connect(queue_manager, channel, '{}({})'.format(host, port)) queue = pymqi.Queue(queue_manager, queue_name) queue.get() pymqi.disconnect(queue_manager) conn.subscribe(destination='/queue/{}'.format(queue_name), id=1, ack='auto') conn.start() conn.connect() while True: pass ``` 这段代码中,我们首先定义了一个MQListener类,用于接收消息后打印消息内容。然后创建了一个Connection对象,使用set_listener()方法将MQListener对象加入进去。使用pymqi连接ActiveMQ,通过Queue.get()方法获取一条消息后,执行pymqi.disconnect()方法退出连接。接着使用stomp.py的subscribe()方法订阅消息,使用start()方法启动连接,然后进入死循环等待消息的到来,当有消息到来就会进入on_message()方法,处理消息
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值