python调用activateMQ进行数据传输

网上的很多代码都不适用了,同时存在一个问题就是基于stomp协议的activateMQ的接收端,存在一个问题,就是接收到的数据队列是通过回调函数(类)进行更新的,网上大多数都是这种方面,但是这样做对后面的数据处理很不友好,那么怎么处理呢?

其实很简单自己去看官方的代码和例子即可,都是最新更新的

http://jasonrbriggs.github.io/stomp.py/index.html这个是教程

https://github.com/jasonrbriggs/stomp.py这个是源码

源码中的tests文件夹里又大量的事例如:

 例如 activateMQ的代码:

import stomp
from stomp.listener import TestListener
from .testutils import *


@pytest.fixture()
def conn():
    conn = stomp.Connection11(get_default_host())
    conn.set_listener("testlistener", TestListener("123", print_to_log=True))
    conn.connect(get_default_user(), get_default_password(), wait=True)
    yield conn
    conn.disconnect(receipt=None)


class TestActiveMQ(object):

    def test_send_to_activemq(self, conn):
        conn.subscribe(destination="/queue/test", id=1, ack="auto")

        conn.send(body="this is a test", destination="/queue/test", content_type="text/blah", receipt="123")

        validate_send(conn)

        logging.info(conn.get_listener("testlistener").get_latest_message())

 在数据传输的时候存在一个问题,就是java那么无法接受我这里发送的字符串,接受的是二进制类型的数据,这是方面怎么解决呢?(网上有一篇教程很好,直接拿过来)

python发送消息到activeMQ后java接收到BinaryMessage的坑

和另一个系统进行对接,使用activemq进行消息通信。对方使用java客户端监听一个topic,我们需要发送TextMessage消息,对方接收后进行处理。而我们因为系统架构的原因只能使用python进行推送,也就只能通过stomp协议发送消息。然后就遇到了问题,发送的消息在java消费者端只能解析成BinaryMessage,而发送的时候根本没有办法指定消息类型。网上搜了很久没有找到相同的情况。
根据官方通过python往ActiveMQ发送message的demo编写如下代码。

# -*-coding:utf-8-*-
import stomp
import time

queue_name = '/queue/SampleQueue'
topic_name = '/topic/SampleTopic'
listener_name = 'SampleListener'
test_name = "springBootMqQueue"
springBootMqQueue = '/queue/springBootMqQueue'


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


# 推送到队列queue
def send_to_queue(msg):
    conn = stomp.Connection10([('192.168.36.213', 61613)], auto_content_length=False)
    conn.connect('admin', 'admin', wait=True)
    conn.send(springBootMqQueue, msg)
    conn.disconnect()


##从队列接收消息
def receive_from_queue():
    conn = stomp.Connection10([('192.168.36.213', 61613)], auto_content_length=False)
    conn.set_listener(listener_name, SampleListener())
    conn.connect('admin', 'admin', wait=True)
    conn.subscribe(springBootMqQueue)
    time.sleep(1)  # secs
    conn.disconnect()


if __name__ == '__main__':
    send_to_queue('{"content":{"flow":{"network":"5","times":"1-1","url":"http://www.baidu.com","way":"5"},"sms":{"direction":"0","text":"短信内容详情"},"voice":{"connect":"5","key":"挂断"}},"form":"13901295021","formPort":"com4","interval":"2-2","network":"5","taskId":"1dsf3641212434g","times":"1-3","to":"18611010269","type":"1"}')
    receive_from_queue()

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)

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

# 推送到队列queue
def send_to_queue(msg):
    conn = stomp.Connection10([('192.168.36.213', 61613)], auto_content_length=False)
    conn.connect('admin', 'admin', wait=True)
    conn.send(springBootMqQueue, msg)
    conn.disconnect()

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值