• Netty启动分析


    基于Netty-3.2.5

    先看一段Netty的服务端代码:

    import java.net.InetSocketAddress;
    import java.util.concurrent.Executors;
    
    import org.jboss.netty.bootstrap.ServerBootstrap;
    import org.jboss.netty.channel.Channel;
    import org.jboss.netty.channel.ChannelHandlerContext;
    import org.jboss.netty.channel.ChannelPipeline;
    import org.jboss.netty.channel.ChannelPipelineFactory;
    import org.jboss.netty.channel.ChannelStateEvent;
    import org.jboss.netty.channel.Channels;
    import org.jboss.netty.channel.ExceptionEvent;
    import org.jboss.netty.channel.MessageEvent;
    import org.jboss.netty.channel.SimpleChannelHandler;
    import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
    import org.jboss.netty.handler.codec.string.StringDecoder;
    import org.jboss.netty.handler.codec.string.StringEncoder;
    
    
    public class NettyServer
    {
        public static void main(String[] args)
        {
            ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    
            // Set up the default event pipeline.
            bootstrap.setPipelineFactory(new ChannelPipelineFactory()
            {
                @Override
                public ChannelPipeline getPipeline() throws Exception
                {
                    return Channels.pipeline(new StringDecoder(), new StringEncoder(), new ServerHandler());
                }
            });
    
            // Bind and start to accept incoming connections.
            Channel bind = bootstrap.bind(new InetSocketAddress(8000));
            System.out.println("Server已经启动,监听端口: " + bind.getLocalAddress() + ", 等待客户端注册。。。");
        }
    
        private static class ServerHandler extends SimpleChannelHandler
        {
            @Override
            public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception
            {
                if (e.getMessage() instanceof String)
                {
                    String message = (String) e.getMessage();
                    System.out.println("Client发来:" + message);
    
                    e.getChannel().write("Server已收到刚发送的:" + message);
    
                    System.out.println("
    等待客户端输入。。。");
                }
    
                super.messageReceived(ctx, e);
            }
    
            @Override
            public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception
            {
                super.exceptionCaught(ctx, e);
            }
    
            @Override
            public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
            {
                System.out.println("有一个客户端注册上来了。。。");
                System.out.println("Client:" + e.getChannel().getRemoteAddress());
                System.out.println("Server:" + e.getChannel().getLocalAddress());
                System.out.println("
    等待客户端输入。。。");
                super.channelConnected(ctx, e);
            }
        }
    }
    

    下面分析这个netty服务的启动过程

    核心启动代码:

    ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    
            // Set up the default event pipeline.
            bootstrap.setPipelineFactory(new ChannelPipelineFactory()
            {
                @Override
                public ChannelPipeline getPipeline() throws Exception
                {
                    return Channels.pipeline(new StringDecoder(), new StringEncoder(), new ServerHandler());
                }
            });
    
            // Bind and start to accept incoming connections.
            Channel bind = bootstrap.bind(new InetSocketAddress(8000));
    

    第一节:建立MainReactor和SubReactor线程池

    ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

    第二节:完成Client请求的ChannelHandle链的建立

     // Set up the default event pipeline.
    bootstrap.setPipelineFactory(new ChannelPipelineFactory()
    {
           @Override
           public ChannelPipeline getPipeline() throws Exception
           {
                    return Channels.pipeline(new StringDecoder(), new StringEncoder(), new ServerHandler());
           }
    });
    

    第三节:建立一个Server端启动所对应的ChannelHandle链

    // Bind and start to accept incoming connections. Channel bind = bootstrap.bind(new InetSocketAddress(8000));

      

  • 相关阅读:
    用stetho通过网页访问手机数据库
    Python学习笔记(一)
    Linux学习笔记(四)Linux常用命令
    Linux学习笔记(三)Shell命令机制
    Linux学习笔记(一)
    Linux学习笔记(五)Linux应用程序的安装和卸载
    Linux学习笔记(二)Shell教程
    Linux学习笔记(六)Linux服务程序的安装和卸载
    前端模块总结
    div+css命名规范
  • 原文地址:https://www.cnblogs.com/wuxinliulei/p/5083737.html
Copyright © 2020-2023  润新知