Java——操作ProtocolBuffer格式数据初步

来源:互联网 发布:淘宝能买到客户资料呢 编辑:程序博客网 时间:2024/05/21 16:57

一、项目需求

需要使用RabbitMQ发送Protocol格式数据到对方RabbitMQ服务器

查阅资料了解数据交换的方式比如:XML,JSON,Protobuf,protobuf当下还是很流行的,并且系出名门(谷歌)


二、参考文档

http://www.tuicool.com/articles/YNJJni

http://blog.csdn.net/qyf_5445/article/details/43793067


三、官网Java操作Guide

https://developers.google.com/protocol-buffers/docs/javatutorial


四、小试牛刀

1.首先是proto文件的编写,如:

//船数统计message PBShipStatistic{   required uint32 utcTimeStamp = 1;   optional uint32 sequenceNum = 2; //包序号。各种类型的包都共用同一个序列来编号。   repeated GlobalOnLineShipCount onLineShipCount = 3;   repeated ShipCountPerType shipCountPerType = 4;   repeated ShipCountPerGrid shipCountPerGrid = 5;}message GlobalOnLineShipCount{   optional uint32 onLineShipCount = 1; //在线船数   optional uint32 offLineShipCount = 2; //离线船数}message ShipCountPerType{   required uint32 shipType = 1;   required uint32 shipCount = 2;}message ShipCountPerGrid{   required int32 lowerLeftLongitude = 1;   required int32 lowerLeftLatitude = 2;   optional int32 shipCount = 3; //所有类型的船数   optional int32 fishingCount = 4; //渔船数   optional int32 passengerCount = 5; //客船数   optional int32 cargoCount = 6; //货船数   optional int32 tankerCount = 7;//油船数   optional int32 portVesselCount = 8; //港口相关船数}

2.使用protoc.exe生成Java类

官网有生成编译器


3.实现对象的序列号和反序列化,了解Protocol-buffers jar的相关API

//船数统计public ShipStatistic.PBShipStatistic.Builder getShipStatisticPB(){    ShipStatistic.PBShipStatistic.Builder builderPBShipStatistic=ShipStatistic.PBShipStatistic.newBuilder();    nowTime= Calendar.getInstance().getTimeInMillis()/1000;    builderPBShipStatistic.setUtcTimeStamp((int)nowTime);    packageNum.packageNumShipStatistic=packageNum.packageNumShipStatistic+1;    builderPBShipStatistic.setSequenceNum(packageNum.packageNumShipStatistic);    Ship_count ship_count_realtime=ocean_shipInfoService.getRealTime_shipping_abstract_activity();    if(ship_count_realtime!=null){        ShipStatistic.GlobalOnLineShipCount.Builder builderGlobalOnLineShipCount=ShipStatistic.GlobalOnLineShipCount.newBuilder();        int data_all=ship_count_realtime.getShip_count();        int ship_activity_count=ship_count_realtime.getShip_activity_24h_count();        int ship_noneactivity_count=data_all-ship_count_realtime.getShip_activity_48h_count();        builderGlobalOnLineShipCount.setOnLineShipCount(ship_activity_count);        builderGlobalOnLineShipCount.setOffLineShipCount(ship_noneactivity_count);        builderPBShipStatistic.addOnLineShipCount(builderGlobalOnLineShipCount);        ShipStatistic.ShipCountPerType.Builder builderShipCountPerType=ShipStatistic.ShipCountPerType.newBuilder();        List<Ship_type_realtime> ship_type_realtimes=ocean_shipInfoService.getRealTime_shipping_abstract_type();        if(ship_type_realtimes.size()>0){            for(Ship_type_realtime ship_type_realtime:ship_type_realtimes){                builderShipCountPerType.setShipCount(ship_type_realtime.getCount());                builderShipCountPerType.setShipType(ship_type_realtime.getShip_type());                builderPBShipStatistic.addShipCountPerType(builderShipCountPerType);            }        }else{        }    }    List<Ship_grid_count_realtime_simple> ship_grid_count_realtimes=ocean_ShipStatisticsService.getRealTime_shipping_abstract_fixedarea2();    if(ship_grid_count_realtimes!=null){        if(ship_grid_count_realtimes.size()>0){            ShipStatistic.ShipCountPerGrid.Builder builderShipCountPerGrid=ShipStatistic.ShipCountPerGrid.newBuilder();            for(Ship_grid_count_realtime_simple ship_grid_count_realtime_simple:ship_grid_count_realtimes){                builderShipCountPerGrid.setLowerLeftLongitude(ship_grid_count_realtime_simple.getLon());                builderShipCountPerGrid.setLowerLeftLatitude(ship_grid_count_realtime_simple.getLat());                builderShipCountPerGrid.setShipCount(ship_grid_count_realtime_simple.getCou());                builderPBShipStatistic.addShipCountPerGrid(builderShipCountPerGrid);            }        }    }    return builderPBShipStatistic;}


0 0