Netty实战读书笔记(第十一章(下))

来源:互联网 发布:qq企业邮箱 域名 编辑:程序博客网 时间:2024/05/16 10:20

Netty提供异步通信的机制,所以当有大数据需要传输时,即使数据还没有传输完成,也会返回通知ChannelFuture,所以这时会有内存溢出的危险。这个时候需要一个NIO实现零拷贝的方式,直接操作网络栈中的数据,实现一个FileRegion,支持零拷贝的Channle的文件传输协议。

import java.io.File;import java.io.FileInputStream;import java.nio.channels.FileChannel;import io.netty.channel.Channel;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import io.netty.channel.DefaultFileRegion;import io.netty.channel.FileRegion;import io.netty.channel.socket.nio.NioSocketChannel;/** *  * @author pc * 通过实现FileRegion实现数据在Channel中的零拷贝传输。 */public class FileRegionWriterHandler extends ChannelInboundHandlerAdapter{private Channel channel = new NioSocketChannel();private File file = new File("");@Overridepublic void channelActive(final ChannelHandlerContext ctx) throws Exception {File fileTemp = file;Channel channelTemp = channel;FileInputStream input = new FileInputStream(fileTemp);FileChannel fileChannel = input.getChannel();FileRegion fileRegion = new DefaultFileRegion(fileChannel, 0, fileTemp.length());// channel 发送该fileRegion,这个FileRegion可能为大数据,通过申明为FileRegion不需要在jvm栈和网络栈中传输。ChannelFuture future = ctx.writeAndFlush(fileRegion);future.addListener(new ChannelFutureListener(){@Overridepublic void operationComplete(ChannelFuture future) throws Exception {// TODO Auto-generated method stubif (future.isSuccess()){} else {Throwable throwTemp = future.cause();}}});}}
FileRegion支持文件的传输,当需要写入大数据时到内存处理时,写入文件, 需要用ChunkWriteHandler。

import java.io.File;import java.io.FileInputStream;import io.netty.buffer.ByteBuf;import io.netty.channel.Channel;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.handler.stream.ChunkedStream;import io.netty.handler.stream.ChunkedWriteHandler;/** *  * @author pc * */public class ChunkedWriteHandlerInitializer extends ChannelInitializer{private File file;public ChunkedWriteHandlerInitializer(File file){this.file = file;}@Overrideprotected void initChannel(Channel ch) throws Exception {// TODO Auto-generated method stubChannelPipeline pipeline = ch.pipeline();// 以块的方式写入文件。pipeline.addLast(new ChunkedWriteHandler() , new ChunkHandler());}private class ChunkHandler extends SimpleChannelInboundHandler<ByteBuf>{@Overrideprotected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {// TODO Auto-generated method stubctx.writeAndFlush(new ChunkedStream(new FileInputStream(file)));}}}
在网络中传输,RPC需要对于POJO进行序列化和反序列化。

主要有java JDK提供的序列化API、protocol buffer。




阅读全文
0 0