生活随笔
收集整理的這篇文章主要介紹了
Netty使用Marshalling传输信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用Marshalling傳輸信息,需要有以下兩個包,可以在官網下載
jboss-marshalling-1.3.0.CR9.jar
jboss-marshalling-serial-1.3.0.CR9.jar
一、編寫要作為傳輸的Javabean,Student類一定要繼承Serializable接口,才能實現序列化
[java]view plain
?copy package?demo;????import?java.io.Serializable;????public?class?Student?implements?Serializable{??????String?name;??????String?classs;??????int?age;????????@Override??????public?String?toString()?{??????????return?"Student?[name="?+?name?+?",?classs="?+?classs?+?",?age="?+?age?+?"]";??????}????????public?Student(String?name,?String?classs,?int?age)?{??????????super();??????????this.name?=?name;??????????this.classs?=?classs;??????????this.age?=?age;??????}????????public?String?getName()?{??????????return?name;??????}????????public?void?setName(String?name)?{??????????this.name?=?name;??????}????????public?String?getClasss()?{??????????return?classs;??????}????????public?void?setClasss(String?classs)?{??????????this.classs?=?classs;??????}????????public?int?getAge()?{??????????return?age;??????}????????public?void?setAge(int?age)?{??????????this.age?=?age;??????}????}?? 二、編寫客戶端:
通過MarshallingCodeFactory獲得MarshallingDecoder和MarshallingEncoder,并將這兩個編解碼器添加到channelpipeline中
[java]view plain
?copy package?client;????import?factory.MarshallingCodeCFactory;??import?io.netty.bootstrap.Bootstrap;??import?io.netty.channel.ChannelFuture;??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;????public?class?Client?{??????public?static?void?main(String[]?args)?throws?Exception?{??????????new?Client().connect("127.0.0.1",?8888);??????}????????public?void?connect(String?host,?int?port)?throws?Exception?{??????????EventLoopGroup?group?=?new?NioEventLoopGroup();??????????try?{??????????????Bootstrap?b?=?new?Bootstrap();??????????????b.group(group).channel(NioSocketChannel.class);??????????????b.option(ChannelOption.TCP_NODELAY,?true);??????????????b.handler(new?ChannelInitializer<SocketChannel>()?{????????????????????@Override??????????????????protected?void?initChannel(SocketChannel?ch)?throws?Exception?{??????????????????????ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());??????????????????????ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());??????????????????????ch.pipeline().addLast(new?ClientChannelHandler());??????????????????}??????????????});??????????????ChannelFuture?f?=?b.connect(host,?port);??????????????f.channel().closeFuture().sync();??????????}?finally?{??????????????group.shutdownGracefully();??????????}??????}????}??
三、MarshallingCodeFactory的代碼如下:
[java]view plain
?copy package?factory;????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?class?MarshallingCodeCFactory?{????????public?static?MarshallingDecoder?buildMarshallingDecoder()?{??????????final?MarshallerFactory?factory?=?Marshalling.getProvidedMarshallerFactory("serial");??????????final?MarshallingConfiguration?configuration?=?new?MarshallingConfiguration();??????????configuration.setVersion(5);??????????UnmarshallerProvider?provider?=?new?DefaultUnmarshallerProvider(factory,?configuration);??????????MarshallingDecoder?decoder?=?new?MarshallingDecoder(provider,?1024);??????????return?decoder;??????}????????public?static?MarshallingEncoder?buildMarshallingEncoder()?{??????????final?MarshallerFactory?factory?=?Marshalling.getProvidedMarshallerFactory("serial");??????????final?MarshallingConfiguration?configuration?=?new?MarshallingConfiguration();??????????configuration.setVersion(5);??????????MarshallerProvider?provider?=?new?DefaultMarshallerProvider(factory,?configuration);??????????MarshallingEncoder?encoder?=?new?MarshallingEncoder(provider);??????????return?encoder;??????}????}??
四、ClientChannelHandler的代碼如下:客戶端與服務端連通后,客戶端直接將Student對象寫入channelpipeline中
[html]view plain
?copy package?demo;????import?io.netty.channel.ChannelHandlerAdapter;??import?io.netty.channel.ChannelHandlerContext;????public?class?ClientChannelHandler?extends?ChannelHandlerAdapter?{??????@Override??????public?void?channelActive(ChannelHandlerContext?ctx)?throws?Exception?{??????????Student?s?=?new?Student("小紅",?"5班",?12);??????????ctx.writeAndFlush(s);??????}????????@Override??????public?void?channelRead(ChannelHandlerContext?ctx,?Object?msg)?throws?Exception?{????????}????????@Override??????public?void?exceptionCaught(ChannelHandlerContext?ctx,?Throwable?cause)?throws?Exception?{????????}??}?? 五、服務端代碼: [java]view plain
?copy package?demo;????import?factory.MarshallingCodeCFactory;??import?io.netty.bootstrap.ServerBootstrap;??import?io.netty.channel.ChannelFuture;??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.NioServerSocketChannel;????public?class?Server?{??????public?static?void?main(String[]?args)?{??????????try?{??????????????new?Server().bind(8888);??????????}?catch?(Exception?e)?{??????????????e.printStackTrace();??????????}??????}????????public?void?bind(final?int?port)?throws?Exception?{??????????EventLoopGroup?bossGroup?=?new?NioEventLoopGroup();??????????EventLoopGroup?workGroup?=?new?NioEventLoopGroup();??????????try?{??????????????ServerBootstrap?b?=?new?ServerBootstrap();??????????????b.option(ChannelOption.TCP_NODELAY,?true);??????????????b.group(bossGroup,?workGroup).channel(NioServerSocketChannel.class)??????????????????????.childHandler(new?ChannelInitializer<SocketChannel>()?{????????????????????????????@Override??????????????????????????protected?void?initChannel(SocketChannel?ch)?throws?Exception?{??????????????????????????????ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());??????????????????????????????ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());??????????????????????????????ch.pipeline().addLast(new?ServerChannelHandler());??????????????????????????}????????????????????????});??????????????ChannelFuture?f?=?b.bind(port).sync();??????????????System.out.println("服務端已啟動");??????????????f.channel().closeFuture().sync();??????????}?catch?(InterruptedException?e)?{??????????????e.printStackTrace();??????????}?finally?{??????????????bossGroup.shutdownGracefully();??????????????workGroup.shutdownGracefully();??????????}??????}????}?? 六、ServerChannelHandler的代碼如下: [java]view plain
?copy package?demo;????import?io.netty.channel.ChannelHandlerAdapter;??import?io.netty.channel.ChannelHandlerContext;????public?class?ServerChannelHandler?extends?ChannelHandlerAdapter?{????????@Override??????public?void?channelActive(ChannelHandlerContext?ctx)?throws?Exception?{??????????System.out.println("active");??????}????????@Override??????public?void?channelRead(ChannelHandlerContext?ctx,?Object?msg)?throws?Exception?{??????????System.out.println(msg);??????}????????@Override??????public?void?exceptionCaught(ChannelHandlerContext?ctx,?Throwable?cause)?throws?Exception?{??????????cause.printStackTrace();??????}????}??
服務端將輸出如下:
[plain]view plain
?copy 服務端已啟動??active??Student?[name=小紅,?classs=5班,?age=12]?? 表明服務端成功接收了客戶端發送的Student對象
注意:
作為傳輸對象的JavaBean必須要繼承Serializable接口,如上述例子,如果Student繼承了Person,那么Person也必須繼承Serializable接口,否則將報錯
總結
以上是生活随笔為你收集整理的Netty使用Marshalling传输信息的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。