背景
和另一个系统进行对接,使用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
问题解决