NIO相关概念之Selector

选择器(selector):

选择器管理者一个被注册的通道的集合信息和它们的就绪状态.通道是和选择器一起被注册的,并且使用选择器来更新通道的就绪状态,当这么做的时候,可以选择被激发的线程挂起,直到有就绪的通道

可选择通道(SelectableChannel)

这个抽象类提供了实现通道的可选择性所需要的公共方法。它是所有支持就绪检查的通道类的父类。FileChannel对象不是可选择的,因为它们没有继承SelectableChannel。所有socket通道都是可选择的,包括从管道(Pipe)对象的中获得的通道。SelectableChannel可以被注册到Selector对象上,同时可以指定对那个选择器而言,那种操作是感兴趣的。一个通道可以被注册到多个选择器上,但对每个选择器而言只能被注册一次。

选择键(SelectionKey)
选择键封装了特定的通道与特定的选择器的注册关系。选择键对象被SelectableChannel.register( ) 返回并提供一个表示这种注册关系的标记。选择键包含了两个比特集(以整数的形式进行编码),指示了该注册关系所关心的通道操作,以及通道已经准备好的操作。

就绪选择的相关类之间的关系如下:

调用可选择通道的register( )方法会将它注册到一个选择器上。如果您试图注册一个处于阻塞状态的通道,register( )将抛出未检查的IllegalBlockingModeException异常。

此外,通道一旦被注册,就不能回到阻塞状态。试图这么做的话,将在调用configureBlocking( )方法时将抛出IllegalBlockingModeException异常。并且,理所当然地,试图注册一个已经关闭的SelectableChannel实例的话,也将抛出ClosedChannelException异常,就像方法原型指示的那样

尽管SelectableChannel类上定义了register( )方法,还是应该将通道注册到选择器上,而不是另一种方式。选择器维护了一个需要监控的通道的集合。一个给定的通道可以被注册到多于一个的选择器上,而且不需要知道它被注册了那个Selector对象上。将register( )放在SelectableChannel上而不是Selector上,这种做法看起来有点随意。它将返回一个封装了两个对象的关系的选择键对象。重要的是要记住选择器对象控制了被注册到它之上的通道的选择过程。

选择器才是提供管理功能的对象,而不是可选择通道对象。选择器对象对注册到它之上的通道执行就绪选择,并管理选择键。

对于键的interest(感兴趣的操作)集合和ready(已经准备好的操作)集合的解释是和特定的通道相关的。每个通道的实现,将定义它自己的选择键类。在register( )方法中构造它并将它传递给所提供的选择器对象。

以下是一个基于selector来实现的一个Http服务器端:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created with Intellij IDEA
 *
 * @author: jiaoyiping
 * Mail: [email protected]
 * Date: 2018/07/22
 * Time: 11:18
 * To change this template use File | Settings | Editor | File and Code Templates
 */

public class HttpServer {
    private static final Logger logger = LoggerFactory.getLogger(HttpServer.class);


    private static final int DEFAULT_TIME_OUT = 3000;
    private static final int DEFAULT_HTTP_PORT = 8080;

    public static void main(String[] args) throws IOException {
        Selector selector = Selector.open();
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.bind(new InetSocketAddress(DEFAULT_HTTP_PORT));

        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        while (true) {
            if (selector.select(DEFAULT_TIME_OUT) == 0) {
                logger.info("没有接收到客户端的请求,继续等待");
                continue;
            }
            Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator();
            Handler handler = new Handler();
            while (keyIterator.hasNext()) {
                SelectionKey key = keyIterator.next();

                if (key.isAcceptable()) {
                    handler.handleAccept(key);
                }

                if (key.isReadable()) {
                    handler.handleRead(key);
                }

                keyIterator.remove();
            }
        }
    }

    private static class Handler {
        private static final int DEFAULT_BUFFER_SIZE = 4096;
        private static final String DEFAULT_HTTP_RESPONSE_HEADER = "HTTP/1.1 200 OK\nContent-Type: text/html;charset=UTF-8\n\r\n";
        private static final String DEFAULT_CHAR_SET = "UTF-8";

        public void handleAccept(SelectionKey key) throws IOException {
            SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept();
            socketChannel.configureBlocking(false);
            socketChannel.register(key.selector(), SelectionKey.OP_READ, ByteBuffer.allocate(DEFAULT_BUFFER_SIZE));
        }

        public void handleRead(SelectionKey key) throws IOException {
            SocketChannel socketChannel = (SocketChannel) key.channel();
            ByteBuffer buffer = (ByteBuffer) key.attachment();
            buffer.clear();

            if (socketChannel.read(buffer) == -1) {
                logger.debug("没有从请求中读取到内容");
                socketChannel.close();
                return;
            }

            buffer.flip();
            String receivedString = Charset.forName(DEFAULT_CHAR_SET).newDecoder().decode(buffer).toString();
            logger.info("收到了客户端的请求:\n{}", receivedString);

            //返回数据给客户端
            String message = "<html><body>黄河远上白云间</body></html>";
            ByteBuffer sendData = ByteBuffer.wrap((DEFAULT_HTTP_RESPONSE_HEADER + message).getBytes());
            socketChannel.write(sendData);
            socketChannel.close();

        }
    }


}

猜你喜欢

转载自www.cnblogs.com/jiaoyiping/p/9220367.html