I want to use protobuf instead of Json to communicate between message queue.
I know how to deal with it when there is only one proto message.
Assume that the proto file is:
//person.proto
syntax = "proto3";
option java_outer_classname = "PersonProto";
message Person {
int32 id = 2;
string name = 1;
string email = 3;
}
Now, i can deal with it with the approach below:
PersonProto.Person person = PersonProto.Person.newBuilder()
.setEmail("[email protected]")
.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);
But what if there are multiple proto messages?
Assume that there is another proto message called Address.
syntax = "proto3";
option java_outer_classname = "PersonProto";
message Person {
int32 id = 2;
string name = 1;
string email = 3;
}
message Address {
string address = 1;
}
When consumer received byte array from message queue, how to know which proto message it is? and how to deserialise the byte array?