0
点赞
收藏
分享

微信扫一扫

Marshalling在Netty中的使用

上一篇 <<<MessagePack反序列化使用示例
下一篇 >>>分布式缓存与本地缓存的区别


1. 引入jar包

2.编写编码解码程序

public final class MarshallingCodeCFactory {

    /**
     * 创建Jboss Marshalling解码器MarshallingDecoder
     * @return MarshallingDecoder
     */
    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 * 1024 * 1);
        return decoder;
    }

    /**
     * 创建Jboss Marshalling编码器MarshallingEncoder
     * @return MarshallingEncoder
     */
    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;
    }

}

3.使用

4.注意事项

public class RpcRequest  implements Serializable {
}

推荐阅读:
<<<OSI七层模型与层上协议
<<<TCP的三次握手建立链接和四次挥手释放链接
<<<TCP、UDP及Socket代码示例
<<<Https的1.0、2.0协议及长短链接区别
<<<Linux系统的五种IO模型
<<<BIO和NIO区别
<<<BIO模型的缺陷
<<<NIO模式的IO多路复用底层原理
<<<select、poll、epoll的区别
<<<Redis为什么单线程能够支持高并发
<<<Netty初识
<<<Netty的粘包和拆包问题分析
<<<粘包和拆包问题解决方案汇总
<<<序列化与反序列化知识点汇总
<<<MessagePack反序列化使用示例

举报

相关推荐

0 条评论