7.netty集成Marshaling序列化包

来源:互联网 发布:编程求解汉诺塔问题 编辑:程序博客网 时间:2024/05/29 17:33

1.介绍

JBoss Marshaling是一个java对象的序列化包,是对jdk默认序列化框架的一个优化实现。保持和Serializable接口兼容,同时增加了一些可调的参数和附加的特性

2.Marshaling开发环境

这里只需要用到Marshaling的序列化库类:
<!-- https://mvnrepository.com/artifact/org.jboss.marshalling/jboss-marshalling --><dependency>    <groupId>org.jboss.marshalling</groupId>    <artifactId>jboss-marshalling</artifactId>    <version>1.3.0.GA</version></dependency><dependency>    <groupId>org.jboss.marshalling</groupId>    <artifactId>jboss-marshalling-serial</artifactId>    <version>1.3.0.GA</version>    <scope>test</scope></dependency>

3.netty集成

创建解码器和编码器工厂类
package com.tyf.netty;import org.jboss.marshalling.MarshallerFactory;import org.jboss.marshalling.Marshalling;import org.jboss.marshalling.MarshallingConfiguration;import io.netty.handler.codec.marshalling.DefaultMarshallerProvider;import io.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;import io.netty.handler.codec.marshalling.MarshallerProvider;import io.netty.handler.codec.marshalling.MarshallingDecoder;import io.netty.handler.codec.marshalling.MarshallingEncoder;import io.netty.handler.codec.marshalling.UnmarshallerProvider;//静态工厂public final class MarshallingCodeCFactory {//创建解码器public static MarshallingDecoder buildDecoder(){//工厂类:serial由jar包提供支持表示创建的是java序列化工厂final MarshallerFactory factory = Marshalling.getProvidedMarshallerFactory("serial");//配置类,设置一些基本配置final MarshallingConfiguration config = new MarshallingConfiguration();config.setVersion(5);UnmarshallerProvider provider = new DefaultUnmarshallerProvider(factory, config);//返回解码器MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024);return decoder;}//创建编码器public static MarshallingEncoder buildEncoder(){//工厂类final MarshallerFactory factory = Marshalling.getProvidedMarshallerFactory("serial");//配置类,设置一些基本配置final MarshallingConfiguration config = new MarshallingConfiguration();config.setVersion(5);MarshallerProvider provider = new DefaultMarshallerProvider(factory, config);//返回解码器MarshallingEncoder encoder = new MarshallingEncoder(provider);return encoder;}}

server
package com.tyf.netty;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.ChannelHandler.Sharable;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.DelimiterBasedFrameDecoder;import io.netty.handler.codec.FixedLengthFrameDecoder;import io.netty.handler.codec.LengthFieldBasedFrameDecoder;import io.netty.handler.codec.LengthFieldPrepender;import io.netty.handler.codec.protobuf.ProtobufDecoder;import io.netty.handler.codec.protobuf.ProtobufEncoder;import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;import io.netty.handler.codec.string.StringDecoder;@Sharablepublic class MyServerHandler extends ChannelHandlerAdapter {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg)  throws Exception{//直接将msg转换成person类//Person person = (Person)msg;//打印System.out.println(msg);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {super.exceptionCaught(ctx, cause);}//创建起步程序public static void  main(String [] argsStrings) throws Exception {//配置服务端NIO线程组(boss线程、worker线程)EventLoopGroup bGroup = new NioEventLoopGroup();EventLoopGroup wGroup = new NioEventLoopGroup();//创建启动辅助类ServerBootstrap bootstrap = new ServerBootstrap();bootstrap.group(bGroup, wGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100) .childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel channel) throws Exception {//添加channel.pipeline().addLast(MarshallingCodeCFactory.buildDecoder());channel.pipeline().addLast(MarshallingCodeCFactory.buildEncoder());channel.pipeline().addLast(new MyServerHandler());} });try {//监听本地端口,同步等待监听结果ChannelFuture future = bootstrap.bind(11111).sync();//等待服务端监听端口关闭,优雅退出future.channel().closeFuture().sync();}finally {bGroup.shutdownGracefully();wGroup.shutdownGracefully();} }}


client
package com.tyf.netty;import io.netty.bootstrap.Bootstrap;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.FixedLengthFrameDecoder;import io.netty.handler.codec.LengthFieldBasedFrameDecoder;import io.netty.handler.codec.LengthFieldPrepender;import io.netty.handler.codec.protobuf.ProtobufDecoder;import io.netty.handler.codec.protobuf.ProtobufEncoder;import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;import io.netty.handler.codec.string.StringDecoder;public class MyClientHandler extends ChannelHandlerAdapter {@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {Person person = new Person("name", 1);ctx.write(person);ctx.flush();}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {super.exceptionCaught(ctx, cause);}//创建起步程序public static void  main(String [] argsStrings) throws Exception {//配置客户端端NIO线程组EventLoopGroup bGroup = new NioEventLoopGroup();//创建客户端启动辅助类Bootstrap bootstrap = new Bootstrap();bootstrap.group(bGroup).  channel(NioSocketChannel.class).  option(ChannelOption.TCP_NODELAY, true).  handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel channel) throws Exception {//添加channel.pipeline().addLast(MarshallingCodeCFactory.buildDecoder());channel.pipeline().addLast(MarshallingCodeCFactory.buildEncoder());channel.pipeline().addLast(new MyClientHandler());} });//发起异步连接ChannelFuture future = bootstrap.connect("127.0.0.1", 11111).sync();try {//等待客户端链路关闭future.channel().closeFuture().sync();} finally {//优雅退出,释放资源bGroup.shutdownGracefully();} }}


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 乔宝宝大结局第二部 孙权偷上二乔 二乔玉兰 二乔jojo jojo二乔 青马乔第二课堂 乔琰番外二 锁二乔 铜雀春深锁二乔全诗 乔宝宝日记第二卷 红楼石家女 二乔 铜雀春深锁二乔上一句是什么 回到三国娶二乔 锁二乔那首诗 二乔玉兰价格 二乔牡丹 荒野厨王马特和乔希第二季 二亚 第二人称有哪些 什么是第二人称 第二人称叙述 第二人偶 二人三足 婆媳二人先后怀孕 二人欢乐椅使用示意图 二人同心其利断金 二人交二人配免费播放 姐妹二人共带一夫 二人交二人配 二人森林 二人交二人配二人交二人配 科目二笨人多久能学会 尾人柱力 僧道挨踢二人组 人二 二人麻将规则 二人麻将单机版 手机二人麻将 二人 二人锅 二人并走