Protocol Buffer: Java program

Source: Internet
Author: User

First, use Maven to create a simple Java application.

 
MVN archetype: generate-dgroupid = org. freebird. protocolbuffer-dartifactid = sample-darchetypeartifactid = Maven-Archetype-Quickstart-dinteractivemode = false

The directory structure is as follows:

 
$ Tree samplesample ── pom. XML └ ── SRC ├ ── main │ └ ── Java │ └ ── org │ fre── freebird │ └ ── protocolbuffer │ └ ── app. java producer ── test consumer ── Java consumer ── org consumer ── freebird consumer ── protocolbuffer consumer ── apptest. java

Because the Java file generated by the Protocol buffer compiler is external, you need a plug-in to add it to this project.

<Plugin> <groupid> Org. codehaus. mojo </groupid> <artifactid> build-helper-Maven-plugin </artifactid> <version> 1.7 </version> <executions> <execution> <ID> Add-source </ID> <phase> Generate-sources </phase> <goals> <goal> Add-source </goal> </goals> <configuration> <sources> <source>.. /.. /.. /template/Java </source> </sources> </configuration> </execution> </executions> </plugin>

In addition, the Java library of the previously compiled protocol buffer has been deployed on its own nexus private server. Now you can reference it using dependency.

<Dependency> <groupid> com. Google. protobuf </groupid> <artifactid> protobuf-Java </artifactid> <version> 2.4.1 </version> </dependency>

Now we can implement a simple JavaProgram. Reads data from the file specified by the parameter and deserializes it into a user object.

 
Package org. freebird. protocolbuffer; import org. freebird. sample. userproto. User; import java. Io. fileinputstream; import java. Io. ioexception;/*** sample! **/Public class app {public static void main (string [] ARGs) throws ioexception {system. Out. println ("welcome to the Java sample for protocol buffer! "); User user = user. parsefrom (New fileinputstream (ARGs [0]); system. out. println (user. getname (); system. out. println (user. getemail ());}}

Run the following command (I assume you will use exec plugin)

MVN clean compilemvn Exec: Java

The result is as follows:

 
Welcome to the Java sample for protocol buffer! Freebirdshu_chen@esri.com

Therefore, the cross-language features of protocol buffer are quite useful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.