两个版本,序列化问题疑惑
在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的配置方式,则无法收到消息,非常奇怪。