个人理解:

定义.proto文件就是指明消息里包含的成员和类型,protoc会compile成相应的java文件包含interface和implementation class,然后在构建message的时候要使用builder,然后写到outputstream里。

应用实例:

ByteArrayOutputStream out = new ByteArrayOutputStream(BYTE_ARRAY_SIZE);
CodedOutputStream codedOut = CodedOutputStream.newInstance(out);
GothamAuthProto.ExclusiveGroupChoices.newBuilder().setUsername(getActor().getUserId()).addChoices(GothamAuthProto.NameValuePair.newBuilder().setName(GROUP_KEY).setValue(role)).build().writeTo(codedOut);
codedOut.flush();
String uri = solaceRoleSetupUri + getActor().getUserId();
LOGGER.info("Select role: " + out.toString() + " -> " + uri);
batman.createProducerTemplate().sendBody(uri, out.toByteArray());

From Google.protobuf tutorial:https://developers.google.com/protocol-buffers/docs/javatutorial

1, With protocol buffers, you write a .proto description of the data structure you wish to store.

2, From that, the protocol buffer compiler creates a class that implements automatic encoding and parsing of the protocol buffer data with an efficient binary format.

3, The generated class provides getters and setters for the fields that make up a protocol buffer and takes care of the details of reading and writing the protocol buffer as a unit.

a. START with a .proto file:

=====================================

package tutorial;


option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos"; message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3; enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
} message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
} repeated PhoneNumber phone = 4;
} message AddressBook {
repeated Person person = 1;
} ====================================================
b.  Run the protocol buffer compiler protoc on your .proto:(generate the classes you'll need to read and write AddressBook)
Here are some of the accessors for the Person class:
// required string name = 1;
public boolean hasName();
public String getName(); // required int32 id = 2;
public boolean hasId();
public int getId(); // optional string email = 3;
public boolean hasEmail();
public String getEmail();
c, To construct a message, you must first construct a builder, set any fields you want to set to your chosen values, then call the builder's build() method.
Person john =
  Person.newBuilder()
    .setId(1234)
    .setName("John Doe")
    .setEmail("jdoe@example.com")
    .addPhone(
      Person.PhoneNumber.newBuilder()
        .setNumber("555-4321")
        .setType(Person.PhoneType.HOME))
    .build();
 

Google protobuf的更多相关文章

  1. google protobuf安装与使用

    google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...

  2. VS2013编译google protobuf 出现问题error C3861: “min”:

    问题描述: 今天用vs2013编译protobuf 2.4.1 报错: 错误 3 error C3861: "max": 找不到标识符 f:\google\protobuf\pro ...

  3. google protobuf初体验

    最近在读别人代码的时候发现一个的东西,名字叫protobuf, 感觉挺好用的,写在这里,留个记录.那么什么是protobuf 呢?假如您在网上搜索,应该会得到类似这样的文字介绍: Google Pro ...

  4. Google protobuf proto文件编写规则

    转载自: http://blog.csdn.net/yi_ya/article/details/40404231 1. 简单介绍 protobuf文件:就是定义你要的消息(类似java中的类)和消息中 ...

  5. window下编译并使用google protobuf

    参考网址: http://my.oschina.net/chenleijava/blog/261263 http://www.ibm.com/developerworks/cn/linux/l-cn- ...

  6. GOOGLE PROTOBUF开发者指南

    原文地址:http://www.cppblog.com/liquidx/archive/2009/06/23/88366.html 译者: gashero 目录 1   概览 1.1   什么是pro ...

  7. google protobuf ios开发使用

    简介: protobuf 即 google protocol buffer 是一种数据封装格式协议: 比如其他经常用的xml,json等格式:protobuf的优势是效率高,同样的一份数据使用prot ...

  8. google protobuf 简单实例

    1.定义proto文件: User.proto package netty; option java_package="myprotobuf"; option java_outer ...

  9. google protobuf使用

    下载的是github上的:https://github.com/google/protobuf If you get the source from github, you need to gener ...

随机推荐

  1. [redis] Redis 配置文件置参数详解

    ################################ 基础配置 ################################# #daemonize no 默认情况下, redis 不 ...

  2. 补交git、ssh

    本来应该早就应该交的,自己给忘记了,非常抱歉,现在补交上来 词频统计: 代码地址:https://coding.net/u/liuff/p/cipin/git ssh:git@git.coding.n ...

  3. php使用位与运算符【&】位或运算符【|】实现权限管理

    权限值是这样的2^0=1,相应2进数为”0001″(在这里^我表示成”次方”,即:2的0次方,下同)2^1=2,相应2进数为”0010″2^2=4,相应2进数为”0100″2^3=8,相应2进数为”1 ...

  4. C/C++深度copy和浅copy

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...

  5. Java 集合系列 10 Hashtable详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  6. javascript function new this

    1. 首先,我们这里把function直接调用时将这个function当做方法来看待,而new function是将function当做类来看待 2. 当把function作为类来使用时,functi ...

  7. uva 1629

    1629 - Cake slicing Time limit: 3.000 seconds A rectangular cake with a grid of m * n <tex2html_v ...

  8. js——常见的小方法

    1.随机得到是六位数,可以当做“密码”来使用: Math.random().toString().substr(2, 6):

  9. Eclipse 汉化包

    http://www.eclipse.org/babel/downloads.php 下载地址,具体操作请百度. http://subclipse.tigris.org/update_1.6.x SV ...

  10. 使用Socket进行通信

    客户端通常可使用Socket的构造器来连接到指定服务器,Socket通常可使用如下两个构造器. Socket(lnetAddress/String  remoteAddress , int  port ...