zoukankan      html  css  js  c++  java
  • Google protobuf

    个人理解:

    定义.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();
     
  • 相关阅读:
    动态面板——axure线框图部件库介绍
    如何安装(装载)axure组件(部件)
    文本框、文本编辑框、按钮——axure线框图部件库介绍
    图片热区——axure线框图部件库介绍
    水平线、垂直线——axure线框图部件库介绍
    矩形、占位符组件——axure线框图部件库介绍
    文本面板——axure线框图部件库介绍
    windows server 2008 R2服务器安装IIS并添加网站
    使用SplitContainer来实现隐藏窗口的部分内容(转)
    C# VS2010结合SQL Server 2008数据库编程实现方法
  • 原文地址:https://www.cnblogs.com/chayu3/p/3893349.html
Copyright © 2011-2022 走看看