JMS服务器无法识别message.body并引发异常:“协议消息包含无效标记(零)。”它适用于Stomp协议

时间:2017-03-02 12:19:09

标签: python jms activemq protocol-buffers qpid

通过 ActiveMQ 从python-qpid-proton客户端请求 JMS 服务器。

message.body包含 Protobuf

JMS 服务器无法识别message.body并引发异常:“Protocol message contained an invalid tag (zero).

它适用于Stomp协议(但它是另一个故事)。

代码:

import sys
import socket
import uuid
from time import time

from proton import Message
from proton.handlers import MessagingHandler
from proton.reactor import Container

from proto import common

def make_message(message_body):

    message = Message()

    message.body = message_body
    message.correlation_id = 1

    message.properties = {
        'type': 'ru.messages.CommonProtos$LoginRequest',
        'content-length': len(message_body),
        'messageId': str(uuid.uuid4()),
        'errorMsg': '',
        'timestamp': str(int(time())),
        'user': 'vasja'
    }

    return message


def make_login_message():
    login_request = common.LoginRequest()

    login_request.ip.append(socket.gethostbyname(socket.gethostname()))
    login_request.username = "vasja"
    login_request.password = "123"
    login_request.clientVersion = "1.1.1"
    login_request.subscriber = "test"
    login_request.clientTime = int(time())
    login_request.pkName = "test"
    login_request.hostName = "vasja"

    return make_message(login_request.SerializeToString())


class AMQPClient(MessagingHandler):
    def __init__(self, server, request, receive=None):
        super(AMQPClient, self).__init__()
        self.server = server
        self.request = request
        self.receive = (
            request + "receive") if receive is None else (request + receive)

    def on_start(self, event):
        conn = event.container.connect(self.server)
        event.container.create_receiver(conn, self.receive)
        event.container.create_sender(conn, self.request)

    def on_sendable(self, event):
        message = make_login_message()
        event.sender.send(message)
        event.sender.close()

    def on_message(self, event):
        print(event.message.body)
        event.connection.close()


def main():  
    Container(AMQPClient(server="192.168.77.100:5672", request="test")).run()


if __name__ == "__main__":
    main()

0 个答案:

没有答案