前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Netty --Selector选择器

Netty --Selector选择器

作者头像
疯狂的KK
修改2020-11-03 14:56:34
9820
修改2020-11-03 14:56:34
举报
文章被收录于专栏:Java项目实战

Selector:检测多个通道上是否有事件的发生。

Netty各组件对应关系

每一个链接对应一个线程

NIO 非阻塞IO java1.4

channel buffer Selector

线程Thread

|

Selector 根据不同的事件在各个channel切换

|

Channel(read/write) 多个连接 Event决定切换到那个channel

↑↓

Buffer buffer底层有一个数组 数据的读/写 双向的flip切换

Client(socket)

当channel有事件发生返回selectorkey

遍历所有的key 反向得到channel

代码语言:javascript
复制
public abstract class Selector implements Closeable {
}

实现方法

代码语言:javascript
复制
public abstract SelectableChannel channel();

//返回可选择的channel 确定与哪个channel链接

代码语言:javascript
复制
This method performs a blocking <a href="#selop">selection
public abstract int select(long timeout)
 throws IOException;

即使没有监听到事件 也会返回

代码语言:javascript
复制
This method performs a non-blocking <a href="#selop">selection

非阻塞的

代码语言:javascript
复制
public abstract int selectNow() throws IOException;
代码语言:javascript
复制
public abstract Set<SelectionKey> selectedKeys();
1.当客户端链接时 通过seversocketchannel得到对应的socketchannel
2.将得到的socketchannel注册到selector上,一个selctor注册多个selectorchannel
selelctor的父类
public final SelectionKey register(Selector sel, int ops,
                                   Object att)
    throws ClosedChannelException
{
父类SelectableChannel
public final SelectionKey register(Selector sel, int ops//关注的事件)
    throws ClosedChannelException
{
    return register(sel, ops, null);
}
3.注册手返回一个Selelctionkey,与selectors集合关联
4.Selector进行监听selector()方法,返回当前管理的有事件发生的通道的个数
5.进一步得到Selectionkey(有事件发生的)
6.通过Selectionkey反向得到channel()

ops可传入的值

NioServer

代码语言:javascript
复制
package com.kk.netty;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;

/**
 * @author zhaokkstart
 * @create 2020-02-10 14:28
 */
public class NioServer {


    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        Selector selector = Selector.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(6666));
        serverSocketChannel.configureBlocking(false);
        //注册到selector  OP_ACCEPT
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        //等待客户端连接
        while (true) {
            //没有事件发生
            if (selector.select(1000) == 0) {
                System.out.println("服务器等待中....");
                continue;
            }
            //有事件发生的
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            //获取到相关的Selectionkey集合
            Iterator<SelectionKey> iterator = selectionKeys.iterator();

            while (iterator.hasNext()){
                SelectionKey key = iterator.next();
                //根据key对应的通道发生的事件作相应的处理
                if (key.isAcceptable()) {
                //生成一个channel
                    try {
                        SocketChannel accept = serverSocketChannel.accept();
                        accept.configureBlocking(false);
                        //注册到selector,关注事件read的channel的buffer
                        accept.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(1024));
                        //发送数据
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(key.isReadable()){
                    //读事件    通过key获取到对应的channel
                    SocketChannel channel = (SocketChannel) key.channel();
                    //获取到该channel关联的buffer
                    ByteBuffer buffer = (ByteBuffer) key.attachment();
                    //channel读取
                    try {
                        channel.read(buffer);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("客户端发送数据"+new String(buffer.array()));
                    //手动删除当前事件key  防止重复操作
                    iterator.remove();
                }
            };
        }
    }
}

ByteBuffer的warp方法

代码语言:javascript
复制
public static ByteBuffer wrap(byte[] array) {
    return wrap(array, 0, array.length);
}

NioClient

代码语言:javascript
复制
package com.kk.netty;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author zhaokkstart
 * @create 2020-02-10 11:21
 */
public class NioClient {

    public static void main(String[] args) throws IOException {
        //获取通道
        SocketChannel socketChannel = SocketChannel.open();
        //非阻塞
        socketChannel.configureBlocking(false);
        //提供服务器端ip+端口
        InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 6666);
        if (!socketChannel.connect(socketAddress)) {
            while (!socketChannel.finishConnect()) {
                System.out.println("连接中.....");
            }
        }
        //连接成功  发送数据
        String str = "Hello  KK";
        //不需要指定大小
        ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
        //将buffer写入channel
        socketChannel.write(buffer);
        //读取一个
        System.in.read();
    }
}

启动Server,Client,得到字符串,这时的程序基本与Netty的书写理念一致,弄清了channel与selector以及对应关系。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-02-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 赵KK日常技术记录 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档