How to use Protobufs on Vertx Eventbus
I managed to get a Generic Protobuf MessageCodec working..
Feel free to use it if it works for you!
You simply register your Proto with the eventbus as the code below shows:
Notes:
- If the eventbus is in LOCAL mode, then no serialization takes place and the same proto is used
- If the eventbus is in CLUSTER mode, then the
encodeToWire
anddecodeFromWire
methods are used
// Create the codec using the default instance of the protobuf (SupMessage in my example)
ProtobufMessageCodec<SupMessage> protobufMessageCodec = new ProtobufMessageCodec<>(SupMessage.getDefaultInstance());
// register the codec
_vertx.eventBus().registerDefaultCodec(SupMessage.class, protobufMessageCodec);
// Send the proto on the eventbus
vertx.eventBus().publish("proto.response", mySupProtoInstance);
// Listen for protos on the eventbus
vertx.eventBus().<SupMessage>consumer("proto.response", message -> System.out.println("******** PROTO RESPONSE: ********" + message.body()));