Java——操作ProtocolBuffer格式數據

一、項目需求

需要使用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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章