知识点

一个thread + 队列 == 一个单线程线程池   =====> 线程安全的,任务是线性串行执行的

线程安全,不会产生阻塞效应 ,使用对象组

线程不安全,会产生阻塞效应, 使用对象池

Netty5服务端接受客户端的消息,需要继承
SimpleChannelInboundHandler类,同理,客户端接受服务端消息,也需要实现该类
针对单客户端多连,断开重连,可以看第五点

1、Netty5Server.java

package com.example.netty.lesson6;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @author yangwj
 * @date 2020/4/4 21:29
 */
public class Netty5Server {
    public static void main(String[] args) {
        //服务类
        ServerBootstrap bootstrap = new ServerBootstrap();
        //boss worker----其实就是线程池
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();
        try {
            //设置线程池
            bootstrap.group(boss,worker);

            //设置socket工厂
            bootstrap.channel(NioServerSocketChannel.class);

            //设置管道工厂
            bootstrap.childHandler(new ChannelInitializer<Channel>() {
                @Override
                protected void initChannel(Channel channel) throws Exception {
                    channel.pipeline().addLast(new StringDecoder());
                    channel.pipeline().addLast(new StringEncoder());
                    channel.pipeline().addLast(new ServerHandler());
                }
            });
            //设置参数
            bootstrap.option(ChannelOption.SO_BACKLOG,2048);//表示可以接受2048连接(连接缓冲池设置)
            bootstrap.childOption(ChannelOption.SO_KEEPALIVE,true);//维持连接的活跃,清除死连接
            bootstrap.childOption(ChannelOption.TCP_NODELAY,true);//关闭延迟发送
            //绑定端口
            ChannelFuture future = bootstrap.bind(51503);
            //等待服务端关闭
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            //释放资源
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}

2、ServerHandler.java

package com.example.netty.lesson6;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author yangwj
 * @date 2020/4/4 21:41
 */
public class ServerHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println("接受的消息:"+msg);

        //返回给客户端信息
        ctx.channel().writeAndFlush("服务端返回给你:你好");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("exceptionCaught");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //连接时触发
        System.out.println("channelActive");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        //断开时触发
        System.out.println("channelInactive");
    }
}
View Code

相关文章: