Netty网络编程(三)

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-21 05:20:16

引导

引导(Bootstrapping)一个应用程序是指对它进行配置,使它运行起来的过程。Netty处理引导的方式使您的应用程序和网络层相隔离。引导负责将前面介绍的ChannelPipeline,ChannelHandler和EventLoop组织起来成为一个可实际运行的整体。

Bootstrap类

  服务器致力于使用一个父channel来接受来自客户端的链接,并创建子channel以其相互之间的通信。而客户端将最可能只需要一个单独的,没有父channel的channel来用于网络交互。
  两种类型的通用的引导步骤由AbstractBootstrap处理,而特定于客户端或服务器的引导步骤分别由Bootstrap和ServerBootstrap处理。

Bootstrap类的API

名称 描述
Bootstrap group(EventLoopGroup) 设置为处理channel所有事件的EventLoopGroup
Bootstrap channel(class<? extends C>)
Bootstrap channelFactory(ChannelFactory<? extends C>
channel方法指定了Channel的实现类,如果该实现类没有提供替代的构造函数,可以通过调用CahnnelFactory方法指定一个工厂类,将会被bind()方法调用
Bootstrap localAddress(SocketAddress) 指定Channel应该绑定到也可以通过bind()或connect()方法来指定localAddress
<T>Bootstrap option(Channeloption <T> option,T value) 设置ChannelOption,将被应用到每个新创建的Channel的ChannelConfig中.
Bootstrap attr(Attribute<T> key,T vlaue) 指定先创建的channel的属性值,通过绑定或连接方法设置到channel,具体取决于谁先被调用。该方法在创建后再调用不会有任何效果
Bootstrap handler(ChannelHanlder) 设置将被添加到的ChannelPipeline以接受事件通知的ChannelHandler
Bootstrap clone() 创建一个当前Bootstrap的克隆,其功能和原始Bootstrap相同的设置信息
Bootstrap remoteAddress(SocketAddress) 设置远程地址或通过connect方法指定它
ChannelFuture connect() 链接到远程并行并返回一个ChannelFuture,重新链接操作完成后接收到通知
ChannelFuture bind() 绑定Channel并返回一个ChannelFuture,将会重新绑定操作完成后接收到通知,在那之后必须调用Channel.connect方法来建立链接。

引导客户端代码示例

EventLoopGroup group = new NioEventLoopGroup(); 

Bootstrap bootstrap = new Bootstrap();	 //创建一个Bootstrap类的实例来创建和链接新的客户端channel
bootstrap.group(group) 			//设置EventLoopGroup提供用于处理Channel事件的EventLoop
	.channel(NioSocketChannel.class)//指定要使用的通道实现
	//设置为ChannelEvent通道和数据的ChannelInboundHandler 
	.handler(new SimpleChannelInboundHandler<ByteBuf> (){
		@Override 
		protected void channelRead0(
			ChannelHandlerContext channelContext,
			ByteBuf byteBuf)
			throws Exception{
				System.out .printf(“Received data”);
			}
		}
	); 
	//链接到远程主机
	ChannelFuture future = bootstrap.connect(
		new InetSocketAddress(“ www.manning.com”),80);)
	future.addListener (new ChannelFutureListener(){
	@Override
	public void operationComplete(ChannelFuture channe Future)
	throws Exception{
		if (channelFutre.isSuccess()){
               		System.out.printf(“connect established”);
               }else { 
                     System.out.printf(“bound atempt failed ”) ; 
                     channelFutre.cause().printStackTrace(); 
              }
       }
});

引导服务器代码示例

EventLoopGroup group = new NioEventLoopGroup(); 
ServerBootstrap bootstrap = new ServerBootstrap();    
bootstrap.group(group)                    //设置EventLoopGroup提供用于处理Channel事件的EventLoop
       .channel(NioServerSocketChannel.class)//指定要使用的通道实现
       //设置为ChannelEvent通道和数据的ChannelInboundHandler 
       .channelHandler(new SimpleChannelInboundHandler<ByteBuf> (){
              @Override
              protected void channelRead0(
                     ChannelHandlerContext channelContext ctx,
                     ByteBuf byteBuf
                     )throws Exception{
                            System.out.printf(“Received data”);
                     }
              }
       ); 

        //通过配置好的ServerBootstrap的实例绑定该Channel
        ChannelFuture future = bootstrap.bind(new InetSocketAddress("8080"))
       	future.addListener(new ChannelFutureListener(){
       	@Override
       	public void operationComplete(ChannelFuture channelFuture)
		throws Exception{
             	if (channelFutre.isSuccess()){
                        System.out.printf(“connect established”);
               }else { 
                     System.out.printf(“bound attempt failed”) ; 
                     channelFutre.cause().printStackTrace();

              }

	}

});

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!