How to know which protobuf message the byte array is?
<p>I want to use protobuf instead of Json to communicate between message queue. </p>

<p>I know how to deal with it when there is only one proto message.</p>

<p>Assume that the proto file is:</p>

<pre><code>//person.proto
syntax = "proto3";

option java_outer_classname = "PersonProto";

message Person {
    int32 id = 2;
    string name = 1;
    string email = 3;
}
</code></pre>

<p>Now, i can deal with it with the approach below:</p>

<pre><code>PersonProto.Person person = PersonProto.Person.newBuilder()
        .setEmail("123@test.com")
        .setId(1)
        .setName("name-test")
        .build();

byte[] bytes = person.toByteArray();

//Transfer from publisher to consumer between message queue.

//I can deserialise it, because i know the proto message is Person.
PersonProto.Person.parseFrom(bytes);
</code></pre>

<p>But what if there are multiple proto messages?</p>

<p>Assume that there is another proto message called <code>Address</code>.</p>

<pre><code>syntax = "proto3";

option java_outer_classname = "PersonProto";

message Person {
    int32 id = 2;
    string name = 1;
    string email = 3;
}

message Address {
    string address = 1;
}
</code></pre>

<p>When consumer received byte array from message queue, how to know which proto message it is? and how to deserialise the byte array?</p>