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

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("");
	
	@Override
	public 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(){

			@Override
			public void operationComplete(ChannelFuture future) throws Exception {
				// TODO Auto-generated method stub
				if (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;
	}
	
	@Override
	protected void initChannel(Channel ch) throws Exception {
		// TODO Auto-generated method stub
		ChannelPipeline pipeline = ch.pipeline();
		// 以块的方式写入文件。
		pipeline.addLast(new ChunkedWriteHandler() , 
				new ChunkHandler());
	}
	
	private class ChunkHandler extends SimpleChannelInboundHandler<ByteBuf>{
		
		
		@Override
		protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
			// TODO Auto-generated method stub
			ctx.writeAndFlush(new ChunkedStream(new FileInputStream(file)));
		}
		
	}

}
在网络中传输,RPC需要对于POJO进行序列化和反序列化。

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值