设为首页 加入收藏

TOP

Netty5 序列化方式(Jboss Marshalling)(一)
2017-12-14 14:32:09 】 浏览:986
Tags:Netty5 序列化 方式 Jboss Marshalling

Netty作为很多高性能的底层通讯工具,被很多开发框架应用再底层,今天来说说常用的序列化工具,用Jboss的Marshalling。


直接上代码,Marshalling的工厂类


package com.netty.serialize.marshalling;


import io.netty.handler.codec.marshalling.*;


import org.jboss.marshalling.MarshallerFactory;


import org.jboss.marshalling.Marshalling;


import org.jboss.marshalling.MarshallingConfiguration;


 


/**


 * Created by sdc on 2017/8/28.


 */


public class MarshallingCodeCFactory {


 


    /**


    * 解码


    * @return


    */


    public static MarshallingDecoder buildMarshallingDecoder() {


        //首先通过Marshalling工具类的精通方法获取Marshalling实例对象 参数serial标识创建的是java序列化工厂对象。


        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");


        //创建了MarshallingConfiguration对象,配置了版本号为5


        final MarshallingConfiguration configuration = new MarshallingConfiguration();


        configuration.setVersion(5);


        //根据marshallerFactory和configuration创建provider


        UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);


        //构建Netty的MarshallingDecoder对象,俩个参数分别为provider和单个消息序列化后的最大长度


        MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024);


        return decoder;


    }


 


    /**


    * 编码


    * @return


    */


    public static MarshallingEncoder buildMarshallingEncoder() {


        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");


        final MarshallingConfiguration configuration = new MarshallingConfiguration();


        configuration.setVersion(5);


        MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration);


        //构建Netty的MarshallingEncoder对象,MarshallingEncoder用于实现序列化接口的POJO对象序列化为二进制数组


        MarshallingEncoder encoder = new MarshallingEncoder(provider);


        return encoder;


    }


 


}


这个是Marshalling的序列化方式,Marshalling自带编解码,所以不用担心中途编解码半包的问题。


服务端的Server实现:


package com.netty.serialize.server;


 


import com.netty.serialize.coder.MsgDecoder;


import com.netty.serialize.coder.MsgEncoder;


import com.netty.serialize.handler.ServerHandler;


import com.netty.serialize.marshalling.MarshallingCodeCFactory;


import io.netty.bootstrap.ServerBootstrap;


import io.netty.channel.*;


import io.netty.channel.nio.NioEventLoopGroup;


import io.netty.channel.socket.SocketChannel;


import io.netty.channel.socket.nio.NioServerSocketChannel;


 


/**


 * Created by sdc on 2017/8/26.


 */


public class MsgServer {


 


    public void bind(int port) throws Exception {


        EventLoopGroup bossGroup = new NioEventLoopGroup();


        EventLoopGroup workerGroup = new NioEventLoopGroup();


        try {


            ServerBootstrap sb = ne

首页 上一页 1 2 3 4 5 6 下一页 尾页 1/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python模块入门教程之smtplib 邮.. 下一篇Java 设计模式之适配器模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目