Protocol Buffer使用实例
来源:互联网 发布:通达信选股源码 编辑:程序博客网 时间:2024/06/06 05:41
1、安装说明
点击下载protocol相关程序
2、发送消息
Msg.proto内容如下:
package com.protocol;message Msg{required bytes head = 1;optional bytes body = 2;}message Head{required string head01 = 1;required int32 head02 = 2;required string head03 = 3;}message Body{required string body01 = 1;required int32 body02 = 2;required string body03 = 3;}说明:每一个消息Msg包含消息头head和消息体body,消息头的定义在Head中,消息体的定义在Body中。
//发送消息如下:Msg.Builder msgBuilder = Msg.newBuilder();//构建消息头Head.Builder msgHeadBuilder = Head.newBuilder();msgHeadBuilder.setHead01("head01");msgHeadBuilder.setHead02(2);msgHeadBuilder.setHead03("head03");Head head=msgHeadBuilder.build();msgBuilder.setHead(head.toByteString());//构建消息体Body.Builder msgBodyBuilder = Body.newBuilder();msgBodyBuilder.setBody01("body01");msgBodyBuilder.setBody02(2);msgBodyBuilder.setBody03("body03");Body body=msgBodyBuilder.build();msgBuilder.setBody(body.toByteString());Msg msg = msgBuilder.build(); //构建整个消息对象//添加消息至队列并启动发送线程msgQueue.add(msg)if (!sendThreadIsAlive) {new SendThreadRunnable().start();}//一般情况下会另开线程来发送消息private InputStream inputstream = socket.getInputStream();;private OutputStream outputStream = socket.getOutputStream();;private boolean sendThreadIsAlive = false; .//线程存活标志位private ConcurrentLinkedQueue<Msg> msgQueue = new ConcurrentLinkedQueue<Msg>();// 消息发送队列private class SendThread extends Thread {@Overridepublic void run() {sendThreadIsAlive = true; //线程开启中将其置为truewhile (!msgQueue.isEmpty()) {try {if (socket != null && !socket.isClosed() && !socket.isInputShutdown() && !socket.isOutputShutdown()) {Msg msg = msgQueue.poll();if (msg != null) {try {outputStream.write(msg.toByteArray());outputStream.flush();} catch (IOException e) {}} else {}} else {msgQueue.clear();}} catch (Exception e) {}} sendThreadIsAlive = false; //队列中没有消息后将其置为false,标识发送线程结束}}
3、接收解析
//同样另开线程来接收Msg,核心代码如下:byte tmpMsg[] = new byte[2048];int count = inputstream.read(tmpMsg);if (count < 0) {break;}byte[] message = new byte[count];System.arraycopy(tmpMsg, 0, message, 0, count);// 解析整个消息Msg msg = Msg.parseFrom(message);// 解析消息头ByteString head=msg.getHead();Head msgHead = Head.parseFrom(head);// head01String head01 = msgHead.getHead01();// head02int head02 = msgHead.getHead02();//解析消息体ByteString body = msg.getBody();Body msgBody = Body.parseFrom(body);//获得body的内容// body01String body01 = msgHead.getBody01();// body02int body02 = msgHead.getBody02();
阅读全文
0 0
- Protocol Buffer使用实例
- Protocol Buffer实例
- google protocol buffer (C++,Java序列化使用实例)
- google protocol buffer (C++,Java序列化使用实例)
- google protocol buffer 简介 版本 安装 使用 实例
- google protocol buffer (C++,Java序列化使用实例)
- google protocol buffer (C++,Java序列化使用实例)
- google protocol buffer (C++,Java序列化使用实例)
- protocol buffer简单使用
- Protocol buffer使用
- protocol buffer使用举例
- Protocol Buffer使用
- 为什么使用Protocol Buffer
- protocol buffer 使用
- Protocol Buffer使用简介
- Protocol Buffer使用简介
- protocol buffer的使用
- Protocol Buffer介绍(Java实例)
- linux自动以root登录,并自动启动用户程序的设置方法(适合sysVinit启动机制)
- 操作纪录 | github注册和windows版git客户端设置
- Django note3
- Maven 手动添加 JAR 包到本地仓库
- JAVA学习笔记
- Protocol Buffer使用实例
- 如何编写测试计划
- Json转换利器Gson之-实际开发中的特殊需求处理(手动解析-TypeToken处理)
- spring的@Transactional注解详细用法
- HGDB PG表中添加删除列
- Codeforces
- Lock使用----ReentrantRedWriteLock
- 例1.2 使用函数重载
- 关于多维数组的运算问题