zoukankan      html  css  js  c++  java
  • Java NIO框架Netty demo

    Netty是什么

    Netty是一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。

    也就是说,Netty 是一个基于NIO的客户、服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户,服务端应用。Netty相当简化和流线化了网络应用的编程开发过程,例如,TCP和UDP的socket服务开发。

    “快速”和“简单”并不意味着会让你的最终应用产生维护性或性能上的问题。Netty 是一个吸收了多种协议的实现经验,这些协议包括FTP,SMTP,HTTP,各种二进制,文本协议,并经过相当精心设计的项目,最终,Netty 成功的找到了一种方式,在保证易于开发的同时还保证了其应用的性能,稳定性和伸缩性。

    https://www.oschina.net/p/netty

    Server端
    public class TimeServer {
        public void bind(int port) throws Exception {
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            EventLoopGroup workerGroup = new NioEventLoopGroup();
            try {
                // 配置服务器的NIO线程租
                ServerBootstrap bootstrap = new ServerBootstrap();
                bootstrap.group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .option(ChannelOption.SO_BACKLOG, 1024)
                        .childHandler(new ChildChannelHandler());
    
                // 绑定端口,同步等待成功
                ChannelFuture future = bootstrap.bind(port).sync();
                // 等待服务端监听端口关闭
                future.channel().closeFuture().sync();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 优雅退出,释放线程池资源
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    
        private class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
            @Override
            protected void initChannel(SocketChannel arg0) throws Exception {
                System.out.println("server initChannel..");
                arg0.pipeline().addLast(new TimerServerHandler());
            }
        }
    
        public static void main(String[] args) throws Exception {
            int port = 9000;
            new TimeServer().bind(port);
        }
    }
    
    public class TimerServerHandler extends ChannelHandlerAdapter {
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg)
                throws Exception {
            System.out.println("server channelRead..");
            ByteBuf buf = (ByteBuf) msg;
            byte[] req = new byte[buf.readableBytes()];
            buf.readBytes(req);
            String body = new String(req, "UTF-8");
            System.out.println("The time server receive order:" + body);
            String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date(
                    System.currentTimeMillis()).toString() : "BAD ORDER";
            ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
            ctx.write(resp);
        }
    
        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            System.out.println("server channelReadComplete..");
            ctx.flush();//刷新后才将数据发出到SocketChannel
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                throws Exception {
            System.out.println("server exceptionCaught..");
            ctx.close();
        }
    }
    
    Client端
    public class TimeClient {
        public void connect(int port, String host) throws Exception {
            // 配置客户端NIO线程组
            EventLoopGroup group = new NioEventLoopGroup();
            try {
                Bootstrap bootstrap = new Bootstrap();
                bootstrap.group(group).channel(NioSocketChannel.class)
                        .option(ChannelOption.TCP_NODELAY, true)
                        .handler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel socketChannel)
                                    throws Exception {
                                System.out.println("client initChannel..");
                                socketChannel.pipeline().addLast(new TimeClientHandler());
                            }
                        });
                // 发起异步连接操作
                ChannelFuture future = bootstrap.connect(host, port).sync();
                // 等待客户端链路关闭
                future.channel().closeFuture().sync();
            } finally {
                // 优雅退出,释放NIO线程组
                group.shutdownGracefully();
            }
        }
    
        public static void main(String[] args) throws Exception {
            int port = 9000;
            new TimeClient().connect(port, "127.0.0.1");
        }
    }
    
    public class TimeClientHandler extends ChannelHandlerAdapter implements ChannelHandler{
        private static final Logger logger = Logger
                .getLogger(TimeClientHandler.class.getName());
        private final ByteBuf firstMessage;
    
        public TimeClientHandler() {
            byte[] req = "QUERY TIME ORDER".getBytes();
            firstMessage = Unpooled.buffer(req.length);
            firstMessage.writeBytes(req);
        }
    
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            //与服务端建立连接后
            System.out.println("client channelActive..");
            ctx.writeAndFlush(firstMessage);
        }
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg)
                throws Exception {
            System.out.println("client channelRead..");
            //服务端返回消息后
            ByteBuf buf = (ByteBuf) msg;
            byte[] req = new byte[buf.readableBytes()];
            buf.readBytes(req);
            String body = new String(req, "UTF-8");
            System.out.println("Now is :" + body);
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                throws Exception {
            System.out.println("client exceptionCaught..");
            // 释放资源
            logger.warn("Unexpected exception from downstream:"
                    + cause.getMessage());
            ctx.close();
        }
    }
    
  • 相关阅读:
    小程序与VUE请求后台接口,传数组
    VUE中使用Echarts图表
    封装一个VUE时间线组件
    Git的使用方法及IDEA与Git的集成(二)
    Git中拥有不同的仓库的不同账户
    IDEA无法集成Git问题
    IDEA之debug学习
    MyBatis 学习(三)
    MyBatis 学习(二)
    MyBatis 学习(一)
  • 原文地址:https://www.cnblogs.com/umgsai/p/6265092.html
Copyright © 2011-2022 走看看