Netty task queue Task There are three typical usage scenarios

Each has a selector and NioEventLoop in TaskQueue, when we conduct a number of time-consuming operation time, will produce blocked, this time we can use TaskQueue
There are three concrete realization:

1. common tasks user-defined programs: the method handler custom channelRead of:

        // 比如这里我们有一个非常耗时的业务,异步执行,提交该channel 对应的NioEventLoop的TaskQueue中
        // 解决方案1:
        ctx.channel().eventLoop().execute(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(10000);
                    ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端1",CharsetUtil.UTF_8));
                } catch (InterruptedException e) {
                    System.out.println("发生异常");
                }
            }
        });

2. The timing of the user-defined task, which is submitted to the scheduleTaskQueue

        ctx.channel().eventLoop().schedule(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(5000);
                    ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端1",CharsetUtil.UTF_8));
                } catch (InterruptedException e) {
                    System.out.println("发生异常");
                }
            }
        },5, TimeUnit.SECONDS);

Various methods reactor 3. Non current calling thread, by setting the parameters when hashCode, for example, in a business system, which push the thread, according to the user's identity, to find the channel corresponding to the reference, and then call the Write method of the class to the user push message , will enter into such a scenario. Write the final will be submitted to the task queue after asynchronous consumption

serverBootstrap.group(bossGroup,workGroup)
                    // 使用NioServerSocketChannel作为服务器的通道实现
                    .channel(NioServerSocketChannel.class)
                    // 设置线程队列得到连接个数
                    .option(ChannelOption.SO_BACKLOG,128)
                    // 设置保持活动连接状态
                    .childOption(ChannelOption.SO_KEEPALIVE,true)
                    // 给我们的workGroup的EventLoop对应的管道设置处理器
                    // 创建一个通道测试对象
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        // 给pipeline设置处理区
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            // 可以使用一个集合来管理 socketChannel ,
                            // 在推送消息时,可以将业务加入到各个channel对应的NIOEventLoop的taskQueue或者scheduleTaskQueue中
                            System.out.println("客户socketChannel hashcode="+socketChannel.hashCode());
                            socketChannel.pipeline().addLast(new JymNettyServerHandler());
                        }
                    });
Program for a further explanation

1.netty abstract two thread pool, BossGroup responsible for client connections, WorkerGroup responsible for the network read and write operations
2.NioEventLoop represent a continuous loop of thread executing processing tasks, each NioEventLoop has a selector, for monitoring binding network channel in its socket on the
internal 3.NioEventLoop serial design, reading and writing from slender, decoding, processing, encoding, transmission, are responsible for the thread NioEventLoop IO

Object relations before

Under NioEventGroup contains more NioEventLoop
each NioEventLoop contains a Selector, a taskQueue
the selector each NioEventLoop can register multiple listeners NioChannel
each NioChannel only binding on only NioEventLoop
each NioChannel is bound to have a own the Channel and Pipeline

Lack of study time, too shallow knowledge, that's wrong, please forgive me.

There are 10 kinds of people in the world, one is to understand binary, one is do not understand binary.

Published 71 original articles · won praise 54 · views 420 000 +

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104228803