zoukankan      html  css  js  c++  java
  • netty服务器端启动

    package com.imooc.netty.ch3;
    
    import com.imooc.netty.ch6.AuthHandler;
    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.*;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.util.AttributeKey;
    
    /**
     * @author
     */
    public final class Server {
    
        public static void main(String[] args) throws Exception {
            EventLoopGroup bossGroup = new NioEventLoopGroup(1);
            EventLoopGroup workerGroup = new NioEventLoopGroup();
    
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .childOption(ChannelOption.TCP_NODELAY, true)
                        .childAttr(AttributeKey.newInstance("childAttr"), "childAttrValue")
                        .handler(new ServerHandler())
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch) {
                                ch.pipeline().addLast(new AuthHandler());
                                //..
    
                            }
                        });
    
                ChannelFuture f = b.bind(8888).sync();
    
                f.channel().closeFuture().sync();
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    }
    package com.imooc.netty.ch3;
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;
    
    import java.util.concurrent.TimeUnit;
    
    public class ServerHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            System.out.println("channelActive");
        }
    
        @Override
        public void channelRegistered(ChannelHandlerContext ctx) {
            System.out.println("channelRegistered");
        }
    
        @Override
        public void handlerAdded(ChannelHandlerContext ctx) {
            System.out.println("handlerAdded");
        }
    
        @Override
        public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
            super.channelRead(ctx, msg);
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 耗时的操作
                    String result = loadFromDB();
    
                    ctx.channel().writeAndFlush(result);
                    ctx.executor().schedule(new Runnable() {
                        @Override
                        public void run() {
                            // ...
                        }
                    }, 1, TimeUnit.SECONDS);
    
                }
            }).start();
        }
    
        private String loadFromDB() {
            return "hello world!";
        }
    }

    1:服务端的socket 在哪里进行初始化

    2:在哪进行accept连接

     

  • 相关阅读:
    Window—mysql下载及安装
    postgresql 在windows下启动调试功能
    FASTREPORT自动换行及行高自适应
    如何卸载已经安装在delphi7中控件包?
    cxgrid使用三问1cxgrid 如何动态创建列2cxGrid 通过字段名取得列3cxGrid动态创建的列里动态创建事件的方法
    VirtualBox中Linux设置共享文件夹
    Android & iOS 启动画面制作工具(转自龟山Aone)
    PostgreSQL 基本数据类型及常用SQL 函数操作
    win10 安装Postgresql 服务不能启动报错
    TdxDbOrgChart 图标显示问题
  • 原文地址:https://www.cnblogs.com/zuochanzi/p/9883697.html
Copyright © 2011-2022 走看看