A protocol buffer is the simplest and most efficient way to manage structured data, largely because of its built-in support for serializing messages. The types of messages a protocol buffer can manage include structured data forms and composite messages. The message is stored in the .proto extension file. Normally, developers call this type of message as a proto message.
For Java developers, Google provides a protocol buffer compiler for Java. Developers can use this compiler to convert a .proto message to a Java-based source file of the message, which can help speed up storage and retrieval of data.
In this article, we will explain how to implement Java object management using a protocol buffer and the Google protocol buffer compiler.
Anatomy of a Message in Java
A message is a collection of different fields/attributes stored in a single name, where a field is a combination of different scalar types, composite types and enumerations.
The types of messages a protocol buffer can manage include structured data forms and composite messages. The message is stored in the .proto extension file. Normally, developers call this type of message as a proto message.
The following is the syntax for a message type:
message <name of message> {
<Field rule> <field type> <name of the field>= <tag value>
}
A message has the following field rules:
Required
: This field is mandated while saving the file and validated as part of well-formed message.
Optional
: A message can have zero or more fields, and the value of the field may be optional.
Repeated
: This field can be repeated any number of times.
You can use the following data types in field declarations:
- All Java primitive data types (e.g.
int
, long
, float
, double
, Boolean
)
String
and ByteString
To name fields, you simply follow the Java variable naming approach.
Each field in the message is tagged with a tag value. The tag value is used to identify the field in the message binary format, and its size range is from 1 to 536,870,911. This value cannot change once the message is in use. A developer can reserve the tag value for frequent-usage fields. The value range of 1 to 15 is for frequent-usage fields and it takes one byte to encode tag value. The range 16 to 2047 takes two bytes.
The following is an example message type:
message employee {
required int empno=1;
required String name=2;
optional String sex=3;
}
message emp_record {
repeated employee emp=1; //employee message repeated any number of times.
}
A message also supports the following:
- Using
//
to add comments in the message
- Declaring one or more message in the same .proto file
- Declaring nested messages (i.e. a message within a message).
A developer can also set the following message attributes using proto:
- The package for a message
- The Java class name for the message
- The default value for the message field
Here's a sample message that shows all the above support:
Option java_package ="com.emp";
Option java_outer_classname= "EmployeeProto";
message emp{
required int empno=1; //Employee id –comments
optional String name=2;
optional String sex=3; [default="M"];
//Message Enum declaration
enum phonetype {
HOME=1;
MOBILE=2;
OFFICE=3;
}
//Nested Message –Composite type message
message contact{
optional string number=1;
optional phonetype type=2; [default=MOBILE]
}
repeated contact empcontact=4;
}