所以下面这段代码的作用就是为这个子channel增加handle*/
.childHandler(pipeline);
ChannelFuture f = b.bind().sync();/异步绑定到服务器,sync()会阻塞直到完成/
System.out.println("Netty server start,port is " + port);
f.channel().closeFuture().sync();/阻塞直到服务器的channel关闭/
} finally {
group.shutdownGracefully().sync();/优雅关闭线程组/
}
}
}
使用netty实现服文件服务器端。
3、Pipeline
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
/**
-
作者:DarkKIng
-
创建日期:2019/12/17
-
作用:职责链
*/
public class Pipeline extends ChannelInitializer {
private EventExecutorGroup businessEventExecutorGroup = new DefaultEventExecutorGroup(10);
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
/**
- http服务器端对response编码
*/
pipeline.addLast(“encoder”, new HttpResponseEncoder());
/**
- http服务器端对request解码3.
*/
pipeline.addLast(“decoder”, new HttpRequestDecoder());
/**
- 合并请求
*/