How to compile protocol buffers schema at runtime in Java?

3,345 views
Skip to first unread message

Burak Emre Kabakcı

unread,
Feb 7, 2015, 11:39:06 AM2/7/15
to prot...@googlegroups.com
I can create the schema (Descriptors.Descriptor) at runtime dynamically using FileDescriptorProto, also I'm able to serialize and deserialize messages using DynamicMessage.

However performance of DynamicMessage is not good enough because of the way it constructs the messages. I wonder whether it's possible to compile the schema at runtime and use it when de-serializing messages for better performance.

If protocol buffers doesn't provide a way to compile the schemas at runtime then is it possible to generate .proto file from a Descriptors.Descriptor instance? Maybe I can try to generate the classes by calling protoc command from my program, compile and load them to the program using Class.forName API.

osu...@gmail.com

unread,
Feb 14, 2015, 1:36:18 AM2/14/15
to prot...@googlegroups.com
Hi Burak,

This sounds intriguing - i'm curious how you create a schema dynamically at runtime, do you have sample code you could share?

I'm also curious what performance difference you measured between DynamicMessage vs generated POJO class. (I use DynamicMessage very successfully to avoid POJO generation, but perhaps performance requirements are different)

Yes I also think you would need protoc & compiler toolchain at runtime to achieve what you want. Probably using tools.jar to invoke java compiler, then Class.forName(). Generated class should be in classpath.

For protoc invocation perhaps following java wrapper can help
https://github.com/os72/protoc-jar

Cheers
--Oliver

osu...@gmail.com

unread,
Mar 21, 2015, 4:16:14 PM3/21/15
to prot...@googlegroups.com
I've investigated this and decided to implement an API that's easier to use:
https://github.com/os72/protobuf-dynamic

osu...@gmail.com

unread,
Apr 7, 2015, 12:57:35 AM4/7/15
to prot...@googlegroups.com
Made the first beta release, protobuf-dynamic 0.9, have a look perhaps, I think it's quite easy to use
https://github.com/os72/protobuf-dynamic

Reply all
Reply to author
Forward
0 new messages