netty4 java序列化_netty4与netty5序列化问题记录

两个版本,序列化问题疑惑

在netty4上,使用序列化

Netty4.x实战(二) 对象传输 - 程序园

http://www.voidcn.com/article/p-hwrhqscn-bau.html

源码下载:

stevenlii/Socket_Netty

https://github.com/stevenlii/Socket_Netty

其中关键代码:

server.java

packagebhz.netty.serial;importio.netty.bootstrap.ServerBootstrap;importio.netty.channel.ChannelFuture;importio.netty.channel.ChannelInitializer;importio.netty.channel.ChannelOption;importio.netty.channel.EventLoopGroup;importio.netty.channel.nio.NioEventLoopGroup;importio.netty.channel.socket.SocketChannel;importio.netty.channel.socket.nio.NioServerSocketChannel;importio.netty.handler.codec.serialization.ClassResolvers;importio.netty.handler.codec.serialization.ObjectDecoder;importio.netty.handler.codec.serialization.ObjectEncoder;importio.netty.handler.logging.LogLevel;importio.netty.handler.logging.LoggingHandler;public classServer {public static void main(String[] args) throwsException{

EventLoopGroup pGroup= newNioEventLoopGroup();

EventLoopGroup cGroup= newNioEventLoopGroup();

ServerBootstrap b= newServerBootstrap();

b.group(pGroup, cGroup)

.channel(NioServerSocketChannel.class)

.option(ChannelOption.SO_BACKLOG,1024)//设置日志

.handler(newLoggingHandler(LogLevel.INFO))

.childHandler(new ChannelInitializer() {protected void initChannel(SocketChannel sc) throwsException {

sc.pipeline().addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));

sc.pipeline().addLast(newObjectEncoder());

sc.pipeline().addLast(newServerHandler());

}

});

ChannelFuture cf= b.bind(8765).sync();

cf.channel().closeFuture().sync();

pGroup.shutdownGracefully();

cGroup.shutdownGracefully();

}

}

使用如下方式序列化

sc.pipeline().addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));

sc.pipeline().addLast(new ObjectEncoder());

而在netty5上,使用如下方式

sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());

sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());

org.jboss.marshalling

jboss-marshalling

2.0.4.Final

org.jboss.marshalling

jboss-marshalling-serial

2.0.4.Final

test

详细方式在

基于netty的Marshalling序列化框架简单实现 - 简书

https://www.jianshu.com/p/bacdc610f557

也可以找到

疑惑:

如果在4上使用5的配置方式,则无法收到消息,非常奇怪。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值