Netty 基础-组件之EventLoop

3. 组件

3.1 EventLoop

事件循环对象

EventLoop 本质是一个单线程执行器(同时维护了一个 Selector),里面有 run 方法处理 Channel 上源源不断的 io 事件。

它的继承关系比较复杂

  • 一条线是继承自 j.u.c.ScheduledExecutorService 因此包含了线程池中所有的方法
  • 另一条线是继承自 netty 自己的 OrderedEventExecutor,
    • 提供了 boolean inEventLoop(Thread thread) 方法判断一个线程是否属于此 EventLoop
    • 提供了 parent 方法来看看自己属于哪个 EventLoopGroup

事件循环组

EventLoopGroup 是一组 EventLoop,Channel 一般会调用 EventLoopGroup 的 register 方法来绑定其中一个 EventLoop,后续这个 Channel 上的 io 事件都由此 EventLoop 来处理(保证了 io 事件处理时的线程安全)

  • 继承自 netty 自己的 EventExecutorGroup
    • 实现了 Iterable 接口提供遍历 EventLoop 的能力
    • 另有 next 方法获取集合中下一个 EventLoop

3.1.1 获取 NioEventLoop

// 内部创建了两个 EventLoop, 每个 EventLoop 维护一个线程
DefaultEventLoopGroup group = new DefaultEventLoopGroup(2);
System.out.println(group.next());
System.out.println(group.next());
System.out.println(group.next());

输出

io.netty.channel.DefaultEventLoop@60f82f98
io.netty.channel.DefaultEventLoop@35f983a6
io.netty.channel.DefaultEventLoop@60f82f98

也可以使用 for 循环

DefaultEventLoopGroup group = new DefaultEventLoopGroup(2);
for (EventExecutor eventLoop : group) {
    System.out.println(eventLoop);
}

输出

io.netty.channel.DefaultEventLoop@60f82f98
io.netty.channel.DefaultEventLoop@35f983a6

3.1.2 NioEventLoop 处理 io 事件

服务器端两个 nio worker 工人

package com.itcxc.netty.c3;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;

/**
 * @author chenxc
 * @date 2021/8/17 22:54
 */
@Slf4j
public class EventLoopServer {

    public static void main(String[] args) {
        new ServerBootstrap()
                // 细分1:boos 只负责ServerSocketChannel上的accept事件  worker只负责SocketChannel上的读写
                .group(new NioEventLoopGroup(1),new NioEventLoopGroup(2))
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) {
                        ch.pipeline().addLast( "handler1", new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                final ByteBuf buf = (ByteBuf) msg;
                                log.debug(buf.toString(Charset.defaultCharset()));
                                super.channelRead(ctx, msg);  //让消息传递给下一个handler
                            }
                        });
                    }
                }).bind(8080);
    }

}

客户端,启动三次,分别修改发送字符串为 zhangsan(第一次),lisi(第二次),wangwu(第三次)

package com.itcxc.netty.c3;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;

/**
 * @author chenxc
 * @date 2021/8/17 23:01
 */
@Slf4j
public class EventLoopClient {

    public static void main(String[] args) throws InterruptedException {
        //2.带有Future,Promise的类型都是和异步方法配套使用
        final ChannelFuture channelFuture = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringEncoder());
                    }
                })
                //1.连接到服务器
                //异步非阻塞的, main线程发起了调用,由nio线程执行连接操作
                .connect(new InetSocketAddress("localhost", 8080));
        //2.1 方法一 使用sync方法同步处理结果
        /*channelFuture.sync();  //阻塞当前线程,直到nio线程建立连接完毕
        //将channelFuture.sync();注释后  将无阻塞的向下执行,直接获取到还没建立连接的channel,导致数据不知道发送到那里去
        final Channel channel = channelFuture.channel();
        channel.writeAndFlush("hello world");
        log.debug("{}",channel);*/

        //2.2 方法二 addListener(回调对象)  方法异步处理结果
        //在nio线程建立好连接之后,会调用operationComplete方法
        channelFuture.addListener((ChannelFutureListener) future -> {
            final Channel channel = future.channel();
            channel.writeAndFlush("hello world");
            log.debug("{}",channel);
        });

    }

}

最后输出

19:49:25 [DEBUG] [nioEventLoopGroup-4-1] c.i.n.c.EventLoopServer -  zhangsan         
19:49:25 [DEBUG] [nioEventLoopGroup-4-2] c.i.n.c.EventLoopServer -  lisi               
19:49:25 [DEBUG] [nioEventLoopGroup-4-1] c.i.n.c.EventLoopServer -  wangwu        
 

可以看到两个工人轮流处理 channel,但工人与 channel 之间进行了绑定

在这里插入图片描述

再增加两个非 nio 工人

//创建一个独立的EventLoopGroup  不处理io事件  处理复杂的业务逻辑,防止因为处理太长,导致处理io事件太慢
EventLoopGroup group = new DefaultEventLoopGroup();
new ServerBootstrap()
        // 细分1:boos 只负责ServerSocketChannel上的accept事件  worker只负责SocketChannel上的读写
        .group(new NioEventLoopGroup(1),new NioEventLoopGroup(2))
        .channel(NioServerSocketChannel.class)
        .childHandler(new ChannelInitializer<NioSocketChannel>() {
            @Override
            protected void initChannel(NioSocketChannel ch) {
                ch.pipeline().addLast( "handler1", new ChannelInboundHandlerAdapter(){
                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        final ByteBuf buf = (ByteBuf) msg;
                        log.debug(buf.toString(Charset.defaultCharset()));
                        super.channelRead(ctx, msg);  //让消息传递给下一个handler
                    }
                });
                ch.pipeline().addLast(group, "handler2", new ChannelInboundHandlerAdapter(){
                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        final ByteBuf buf = (ByteBuf) msg;
                        log.debug(buf.toString(Charset.defaultCharset()));
                        super.channelRead(ctx, msg);
                    }
                });
            }
        }).bind(8080);

客户端代码不变,启动三次,分别修改发送字符串为 zhangsan(第一次),lisi(第二次),wangwu(第三次)

输出

19:49:25 [DEBUG] [nioEventLoopGroup-4-1] c.i.n.c.EventLoopServer - zhangsan
19:49:25 [DEBUG] [defaultEventLoopGroup-2-1] c.i.n.c.EventLoopServer - zhangsan
19:49:25 [DEBUG] [nioEventLoopGroup-4-2] c.i.n.c.EventLoopServer - lisi
19:49:25 [DEBUG] [defaultEventLoopGroup-2-2] c.i.n.c.EventLoopServer - lisi
19:49:25 [DEBUG] [nioEventLoopGroup-4-1] c.i.n.c.EventLoopServer - wangwu
19:49:25 [DEBUG] [defaultEventLoopGroup-2-1] c.i.n.c.EventLoopServer - wangwu

可以看到,nio 工人和 非 nio 工人也分别绑定了 channel

在这里插入图片描述

3.1.3 NioEventLoop 处理普通任务

NioEventLoop 除了可以处理 io 事件,同样可以向它提交普通任务

NioEventLoopGroup nioWorkers = new NioEventLoopGroup(2);

log.debug("server start...");
Thread.sleep(2000);
nioWorkers.execute(()->{
    log.debug("normal task...");
});

输出

22:30:36 [DEBUG] [main] c.i.o.EventLoopTest2 - server start...
22:30:38 [DEBUG] [nioEventLoopGroup-2-1] c.i.o.EventLoopTest2 - normal task...

可以用来执行耗时较长的任务

3.1.4 NioEventLoop 处理定时任务

NioEventLoopGroup nioWorkers = new NioEventLoopGroup(2);

log.debug("server start...");
Thread.sleep(2000);
nioWorkers.scheduleAtFixedRate(() -> {
    log.debug("running...");
}, 0, 1, TimeUnit.SECONDS);

输出

22:35:15 [DEBUG] [main] c.i.o.EventLoopTest2 - server start...
22:35:17 [DEBUG] [nioEventLoopGroup-2-1] c.i.o.EventLoopTest2 - running...
22:35:18 [DEBUG] [nioEventLoopGroup-2-1] c.i.o.EventLoopTest2 - running...
22:35:19 [DEBUG] [nioEventLoopGroup-2-1] c.i.o.EventLoopTest2 - running...
22:35:20 [DEBUG] [nioEventLoopGroup-2-1] c.i.o.EventLoopTest2 - running...
...

可以用来执行定时任务

3.1.5 💡 优雅关闭

优雅关闭 shutdownGracefully 方法。该方法会首先切换 EventLoopGroup 到关闭状态从而拒绝新的任务的加入,然后在任务队列的任务都处理完成后,停止线程的运行。从而确保整体应用是在正常有序的状态下退出的

客户端代码:

package com.itcxc.netty.c3;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress;
import java.util.Scanner;

/**
 * @author chenxc
 * @date 2021/8/20 22:51
 */
@Slf4j
public class CloseFutureClient {

    public static void main(String[] args) throws InterruptedException {
        final NioEventLoopGroup group = new NioEventLoopGroup();
        final ChannelFuture channelFuture = new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                        ch.pipeline().addLast(new StringEncoder());
                    }
                })
                .connect(new InetSocketAddress("localhost", 8080));

        final Channel channel = channelFuture.sync().channel();
        log.debug("{}",channel);
        new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            while (true){
                final String line = scanner.nextLine();
                if ("q".equals(line)){
                    channel.close();  //close是异步的
                    //log.debug("处理关闭之后的操作");  //不能在这里善后
                    break;
                }else {
                    channel.writeAndFlush(line);
                }
            }
        },"input").start();

        //获取cloneFuture对象,
        final ChannelFuture closeFuture = channel.closeFuture();
        log.debug("waiting clone");
        //1)同步关闭处理
        /*closeFuture.sync();
        log.debug("处理关闭之后的操作");*/

        // 2)异步关闭处理
        closeFuture.addListener((ChannelFutureListener) future -> {
            log.debug("处理关闭之后的操作");
            //优雅的关闭group里面的线程,不再接受新的任务,然后再将该处理的处理完
            group.shutdownGracefully();
        });
    }
}

3.1.6 💡 netty中的handler 执行中如何换人?

关键代码 io.netty.channel.AbstractChannelHandlerContext#invokeChannelRead()

static void invokeChannelRead(final AbstractChannelHandlerContext next, Object msg) {
    final Object m = next.pipeline.touch(ObjectUtil.checkNotNull(msg, "msg"), next);
    // 下一个 handler 的事件循环是否与当前的事件循环是同一个线程
    EventExecutor executor = next.executor();
    
    // 是,直接调用
    if (executor.inEventLoop()) {
        next.invokeChannelRead(m);
    } 
    // 不是,将要执行的代码作为任务提交给下一个事件循环处理(换人)
    else {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                next.invokeChannelRead(m);
            }
        });
    }
}
  • 如果两个 handler 绑定的是同一个线程,那么就直接调用
  • 否则,把要调用的代码封装为一个任务对象,由下一个 handler 的线程来调用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值