和另一个系统进行对接,使用activemq进行消息通信。对方使用java客户端监听一个topic,我们需要发送TextMessage消息,对方接收后进行处理。而我们因为系统架构的原因只能使用python进行推送,也就只能通过stomp协议发送消息。然后就遇到了问题,发送的消息在java消费者端只能解析成BinaryMessage,而发送的时候根本没有办法指定消息类型。网上搜了很久没有找到相同的情况。
根据官方通过python往ActiveMQ发送message的demo编写如下代码。
#-*-coding:utf-8-*-
importstompimporttime
queue_name= '/queue/SampleQueue'topic_name= '/topic/SampleTopic'listener_name= 'SampleListener'test_name= "springBootMqQueue"springBootMqQueue= '/queue/springBootMqQueue'
classSampleListener(object):defon_message(self, headers, message):print('headers: %s' %headers)print('message: %s' %message)#推送到队列queue
defsend_to_queue(msg):
conn= stomp.Connection10([('192.168.36.213', 61613)], auto_content_length=False)
conn.connect('admin', 'admin', wait=True)