协议缓冲区作为活动 Mq 上的消息

may*_*ank 2 activemq-classic jms protocol-buffers

我正在设计一个具有多个组件的应用程序,主要是用 java 和 python 编写的。我正在考虑使用“JMS-Active MQ”作为组件和“协议缓冲区”的面向消息的中间件。

1)这是前进的好方法吗?在我们的例子中,“消息大小”可以超过 10MB,协议缓冲区是否仍然具有跨组件通信的优势?对于可以处理“海量数据”的跨平台应用程序,是否有更好的通信“协议”?

2)我创建了一个概念证明,我通过“ActiveMQ”发送“协议buff”作为消息,我使用的是google的java教程中的示例proto文件。

AddressBook.Builder book = AddressBook.newBuilder();
Person.Builder person = Person.newBuilder();
person.setName("mayank");
person.setId(2);
book.addPerson(person); 
TextMessage message = session.createTextMessage();
message.setText(book.build().toString());
Run Code Online (Sandbox Code Playgroud)

在另一个 java 应用程序中,我听了这条消息并尝试将其反序列化回 AddressBook 对象:

public void onMessage(Message message) {
    TextMessage msg = (TextMessage) message;
    try {
        System.out.println(msg.getText());
        CodedInputStream stream =CodedInputStream.newInstance(msg.getText().getBytes());
        AddressBook book = AddressBook.parseFrom(stream);
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
}
Run Code Online (Sandbox Code Playgroud)

这会导致异常:

com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol 
  message, the input ended unexpectedly in the middle of a field.  This could 
  mean either than the input has been truncated or that an embedded message 
  misreported its own length.
at com.google.protobuf.InvalidProtocolBufferException.truncatedMessage(InvalidProtocolBufferException.java:49)
Run Code Online (Sandbox Code Playgroud)

我不知道怎么了..?

nca*_*sas 5

关于 1),协议缓冲区的文档在此处讨论了传输大消息。

关于2),问题似乎是您传输的方式book。看看您提到的教程如何将消息写入 OutputStream。您应该使用二进制消息而不是 TextMessage,例如首先将字节写入ByteArrayOutputStream然后写入消息