Protostuff開發
版權宣告:本文為博主chszs的原創文章,未經博主允許不得轉載。 https://blog.csdn.net/chszs/article/details/50460047
Protostuff開發
作者:chszs,未經博主允許不得轉載。經許可的轉載需註明作者和部落格主頁:http://blog.csdn.net/chszs
五、Protostuff用法
1、為Java實體產生schemas
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.3.8</version>
</dependency>
2、Runtime schemas
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.3.8</version>
</dependency>
3、Protostuff的Maven外掛
Protostuff的Maven外掛可以從.proto檔案中生成Java原始碼或其它語言的原始碼——通過使用基於StringTemplate的程式碼生成器(或擴充套件)。
下面講述此外掛的用法。
1)專案的結構如下:
<project root>
├───pom.xml
└───src
└───main
└───proto
└───hello.proto
Protocol buffer定義路徑是可配置的,但是推薦使用src/main/proto/路徑。所有的.proto檔案及其子目錄都放於此目錄下,且會被protobuf定義視為包結構,以import方式匯入。
hello.proto的內容如下:
package search;
option java_package = "stuff.ch";
message HelloRequest {
optional string name = 1;
}
message HelloResponse {
optional string greeting = 1;
}
service HelloService {
rpc hello (HelloRequest) returns (HelloResponse);
}
執行命令:
mvn protostuff:compile
在專案目錄下產生相應的Java檔案。
<project root>
├───pom.xml
└───target
└───generated-sources
└───proto
└───stuff
└───ch
└───HelloRequest.java
└───HelloResponse.java
HelloRequest.java的內容如下:
// Generated by http://code.google.com/p/protostuff/ ... DO NOT EDIT!
// Generated from proto
package stuff.ch;
import javax.annotation.Generated;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Objects;
import io.protostuff.GraphIOUtil;
import io.protostuff.Input;
import io.protostuff.Message;
import io.protostuff.Output;
import io.protostuff.Schema;
@Generated("java_bean")
public final class HelloRequest implements Externalizable, Message<HelloRequest>, Schema<HelloRequest> {
public static Schema<HelloRequest> getSchema() {
return DEFAULT_INSTANCE;
}
public static HelloRequest getDefaultInstance() {
return DEFAULT_INSTANCE;
}
static final HelloRequest DEFAULT_INSTANCE = new HelloRequest();
private String name;
public HelloRequest() {
}
// getters and setters
// name
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
final HelloRequest that = (HelloRequest) obj;
return Objects.equals(this.name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public String toString() {
return "HelloRequest{" + "name=" + name + `}`;
}
// java serialization
public void readExternal(ObjectInput in) throws IOException {
GraphIOUtil.mergeDelimitedFrom(in, this, this);
}
public void writeExternal(ObjectOutput out) throws IOException {
GraphIOUtil.writeDelimitedTo(out, this, this);
}
// message method
public Schema<HelloRequest> cachedSchema() {
return DEFAULT_INSTANCE;
}
// schema methods
public HelloRequest newMessage() {
return new HelloRequest();
}
public Class<HelloRequest> typeClass() {
return HelloRequest.class;
}
public String messageName() {
return HelloRequest.class.getSimpleName();
}
public String messageFullName() {
return HelloRequest.class.getName();
}
public boolean isInitialized(HelloRequest message) {
return true;
}
public void mergeFrom(Input input, HelloRequest message) throws IOException {
for (int number = input.readFieldNumber(this);; number = input.readFieldNumber(this)) {
switch (number) {
case 0:
return;
case 1:
message.name = input.readString();
break;
default:
input.handleUnknownField(number, this);
}
}
}
public void writeTo(Output output, HelloRequest message) throws IOException {
if (message.name != null)
output.writeString(1, message.name, false);
}
public String getFieldName(int number) {
return Integer.toString(number);
}
public int getFieldNumber(String name) {
return Integer.parseInt(name);
}
}
HelloResponse.java的內容如下:
// Generated by http://code.google.com/p/protostuff/ ... DO NOT EDIT!
// Generated from proto
package stuff.ch;
import javax.annotation.Generated;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Objects;
import io.protostuff.GraphIOUtil;
import io.protostuff.Input;
import io.protostuff.Message;
import io.protostuff.Output;
import io.protostuff.Schema;
@Generated("java_bean")
public final class HelloResponse implements Externalizable, Message<HelloResponse>, Schema<HelloResponse> {
public static Schema<HelloResponse> getSchema() {
return DEFAULT_INSTANCE;
}
public static HelloResponse getDefaultInstance() {
return DEFAULT_INSTANCE;
}
static final HelloResponse DEFAULT_INSTANCE = new HelloResponse();
private String greeting;
public HelloResponse() {
}
// getters and setters
// greeting
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
final HelloResponse that = (HelloResponse) obj;
return Objects.equals(this.greeting, that.greeting);
}
@Override
public int hashCode() {
return Objects.hash(greeting);
}
@Override
public String toString() {
return "HelloResponse{" + "greeting=" + greeting + `}`;
}
// java serialization
public void readExternal(ObjectInput in) throws IOException {
GraphIOUtil.mergeDelimitedFrom(in, this, this);
}
public void writeExternal(ObjectOutput out) throws IOException {
GraphIOUtil.writeDelimitedTo(out, this, this);
}
// message method
public Schema<HelloResponse> cachedSchema() {
return DEFAULT_INSTANCE;
}
// schema methods
public HelloResponse newMessage() {
return new HelloResponse();
}
public Class<HelloResponse> typeClass() {
return HelloResponse.class;
}
public String messageName() {
return HelloResponse.class.getSimpleName();
}
public String messageFullName() {
return HelloResponse.class.getName();
}
public boolean isInitialized(HelloResponse message) {
return true;
}
public void mergeFrom(Input input, HelloResponse message) throws IOException {
for (int number = input.readFieldNumber(this);; number = input.readFieldNumber(this)) {
switch (number) {
case 0:
return;
case 1:
message.greeting = input.readString();
break;
default:
input.handleUnknownField(number, this);
}
}
}
public void writeTo(Output output, HelloResponse message) throws IOException {
if (message.greeting != null)
output.writeString(1, message.greeting, false);
}
public String getFieldName(int number) {
return Integer.toString(number);
}
public int getFieldNumber(String name) {
return Integer.parseInt(name);
}
}
編寫HelloService.java,內容如下:
package stuff.ch;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.Schema;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class HelloService {
public static final Schema<HelloRequest> SEARCH_REQUEST_SCHEMA = HelloRequest.getSchema();
public static final Schema<HelloResponse> SEARCH_RESPONSE_SCHEMA = HelloResponse.getSchema();
/**
* Sample "hello" rpc method handler.
*
* @param requestData
* {@code HelloRequest} binary array encoded using Protocol
* Buffers
* @return {@code HelloResponse} binary array encoded using Protocol Buffers
*/
public byte[] hello(byte[] requestData) throws IOException {
HelloRequest request = deserialize(requestData);
HelloResponse response = hello(request);
return serialize(response);
}
private byte[] serialize(HelloResponse response) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
LinkedBuffer buffer = LinkedBuffer.allocate();
ProtobufIOUtil.writeTo(outputStream, response, SEARCH_RESPONSE_SCHEMA, buffer);
return outputStream.toByteArray();
}
private HelloRequest deserialize(byte[] requestData) {
HelloRequest request = SEARCH_REQUEST_SCHEMA.newMessage();
ProtobufIOUtil.mergeFrom(requestData, request, SEARCH_REQUEST_SCHEMA);
return request;
}
/**
* Service method implementation.
*/
private HelloResponse hello(HelloRequest request) {
HelloResponse response = new HelloResponse();
String name = request.getName();
response.setGreeting("Hello, " + name);
return response;
}
}
編寫單元測試類HelloServiceTest.java,內容如下:
package stuff.ch;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class HelloServiceTest {
public static final byte[] REQUEST = new byte[] { 0x0A, 0x03, `4`, `2`, `!` };
public static final byte[] RESPONSE = new byte[] { 0x0A, 0x0A, `H`, `e`, `l`, `l`, `o`, `,`, ` `, `4`, `2`, `!` };
private HelloService service;
@Before
public void setUp() throws Exception {
service = new HelloService();
}
@Test
public void testSearch() throws Exception {
byte[] responseData = service.hello(REQUEST);
Assert.assertArrayEquals(RESPONSE, responseData);
}
}
執行單元測試,測試通過。
相關文章
- 基於protostuff的序列化工具類開發
- Protostuff詳解
- Protocol Buffer序列化Java框架-ProtostuffProtocolJava框架
- 《從零開始搭建遊戲伺服器》 序列化工具(最優版Protostuff)遊戲伺服器
- 使用netty結合Protostuff傳輸物件例子Netty物件
- jackson、fastjson、kryo、protostuff等序列化工具效能對比ASTJSON
- 網易雲商·七魚智慧客服自適應 ProtoStuff 資料庫快取實踐資料庫快取
- 什麼是Netty編解碼,Netty編解碼器有哪些?Protostuff怎麼使用?Netty
- 本地開發、兩層開發、三層開發與分散式開發分散式
- 敏捷開發--Scrum開發模型敏捷Scrum模型
- 開發人員愛開發
- Spark開發-SparkSql的開發SparkSQL
- struts開發OA合作開發
- 軟體開發:app軟體開發,pc端軟體開發,微商城/小程式開發APP
- 開發
- 原生開發、H5開發和混合開發的區別H5
- 淺談軟體開發模型之瀑布開發和敏捷開發模型敏捷
- 即拼商城APP開發(開發APP)APP
- Flutter開發之Flutter外掛開發Flutter
- 軟體開發新模式:敏捷開發模式敏捷
- 教育app開發需要開發哪些功能APP
- 多端開發之uniapp開發appAPP
- [ Office 365 開發系列 ] 開發模式分析模式
- 【敏捷開發】驅動測試開發敏捷
- WEB開發經典文章-開發秘方Web
- 開發中所使用的開發環境開發環境
- 主流開發語言和開發環境開發環境
- OLE程式開發利用(開發EXCEL) (轉)Excel
- [新手開發記錄] 從測試開始開發
- 七牛雲端儲存--北京研發中心招聘(Golang開發、測試開發、前端開發)Golang前端
- 物聯網【專案開發】開源系統開發
- 教育直播系統開發APP開發(需求)APP
- CXF開發及與Spring整合開發Spring
- Flutter外掛開發《iOS原生模組開發》FlutteriOS
- 論萌新開發與大佬開發的不同?
- Weex開發之路(一):開發環境搭建開發環境
- Weex開發之路(1):開發環境搭建開發環境
- 微信開發之JSSDK介面開發(Java)JSJava