Netty常见类

 
1.
public interface EventLoopGroup
extends EventExecutorGroup
Special  EventExecutorGroup which allows registering  Channels that get processed for later selection during the event loop.
2.
public class NioEventLoopGroup
extends MultithreadEventLoopGroup
MultithreadEventLoopGroup implementations which is used for NIO  Selector based  Channels.
3.
public class Bootstrap
extends AbstractBootstrap<Bootstrap,Channel>
Bootstrap that makes it easy to bootstrap a  Channel to use for clients.

The AbstractBootstrap.bind() methods are useful in combination with connectionless transports such as datagram (UDP). For regular TCP connections, please use the provided connect()methods.

strap的意识是捆绑
4.
public class ServerBootstrap
extends AbstractBootstrap<ServerBootstrap,ServerChannel>
Bootstrap sub-class which allows easy bootstrap of  ServerChannel

ServerBootstrap是Bootstrap的子类

5.了解下服务器启动的时候涉及的ServerBootstrap的相关方法
<T> ServerBootstrap childAttr(AttributeKey<T> childKey, T value)
Set the specific  AttributeKey with the given value on every child  Channel.
EventLoopGroup childGroup()
Return the configured  EventLoopGroup which will be used for the child channels or  null if non is configured yet.
ServerBootstrap childHandler(ChannelHandler childHandler)
Set the  ChannelHandler which is used to serve the request for the  Channel's.
<T> ServerBootstrap childOption(ChannelOption<T> childOption, T value)
Allow to specify a  ChannelOption which is used for the  Channel instances once they get created (after the acceptor accepted the Channel).
ServerBootstrap clone()
Returns a deep clone of this bootstrap which has the identical configuration.
ServerBootstrap group(EventLoopGroup group)
Specify the  EventLoopGroup which is used for the parent (acceptor) and the child (client).
ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
Set the  EventLoopGroup for the parent (acceptor) and the child (client).
String toString() 
ServerBootstrap validate()
Validate all the parameters.
因此,我们可以对服务器启动程序做如下解释:
public class HttpServer {

	public void start(int port) throws Exception {
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap b = new ServerBootstrap();
			b.group(bossGroup, workerGroup)
					.channel(NioServerSocketChannel.class)
					.childHandler(new ChannelInitializer<SocketChannel>() {
						@Override
						public void initChannel(SocketChannel ch)
								throws Exception {
							// server端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码
							ch.pipeline().addLast(new HttpResponseEncoder());
							// server端接收到的是httpRequest,所以要使用HttpRequestDecoder进行解码
							ch.pipeline().addLast(new HttpRequestDecoder());
							ch.pipeline().addLast(
									new HttpServerInboundHandler());
						}
					}).option(ChannelOption.SO_BACKLOG, 128)
					.childOption(ChannelOption.SO_KEEPALIVE, true);

			ChannelFuture f = b.bind(port).sync();

			f.channel().closeFuture().sync();
		} finally {
			workerGroup.shutdownGracefully();
			bossGroup.shutdownGracefully();
		}
	}

	public static void main(String[] args) throws Exception {
		HttpServer server = new HttpServer();

		server.start(8080);
	}

(1)创建了一个ServerBootstrap对象b,这个对象的作用是用来捆绑启动Netty服务
(2)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值