Java网络编程学习A轮_08_NIO的Reactor模型
参考资料:
了解 Java NIO 的 Reactor 模型,大神 Doug Lea 的 PPT Scalable IO in Java 必看:http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf
示例代码:
https://github.com/gordonklg/study,socket module
A. 单线程版
Reactor 相当于一个中央事件收集分发器。一方面,Reactor 通过 Selector 可以收到已经准备完毕的事件通知,另一方面,Reactor 将事件发送给对应的 Handler 处理。
对于 NIO 服务端,建立连接与数据传输是通过不同类型的 Channel 处理的。ServerSocketChannel 用来处理连接建立请求,其 accept 方法创建出的 SocketChannel 用来处理与客户端的数据传输。多数情况下,服务端会有一个 ServerSocketChannel 以及数量与已连接客户端总数一致的 SocketChannel。在单线程版 Reactor 模型中,所有的 Channel 都会注册到 Reactor 的 Selector 上,由 Reactor 的事件循环代码分发(dispatch)事件。
Reactor 将事件发送给对应的 Handler 处理,acceptor 可以看作一个特殊的 Handler,用于处理连接建立请求。而每个具体的连接(对应 SocketChannel)对应一个 Handler 实例,该 Handler 实例负责读取数据、解码、执行业务逻辑、编码以及发送数据给客户端。显然,Handler 是线程安全的。
nio.pdf 文件中包含了单线程版几乎所有的源码,自己只需要实现几个简单方法即可。我额外增加了一些调试日志用于观察系统运行情况。
gordon.study.socket.nio.reactor.singlethread.Reactor.java
public class Reactor implements Runnable {
final Selector selector;
final ServerSocketChannel serverSocket;
public Reactor(int port) throws IOException {
selector = Selector.open();
serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(port));
serverSocket.configureBlocking(false);
SelectionKey sk = serverSocket.register(selector, SelectionKey.OP_ACCEPT);
sk.attach(new Acceptor());
}
@Override
public void run() {
try {
while (!Thread.interrupted()) {
selector.select();
Set<SelectionKey> selected = selector.selectedKeys();
printSelectedKeys(selected);
Iterator<SelectionKey> it = selected.iterator();
while (it.hasNext()) {
dispatch(it.next());
}
selected.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void dispatch(SelectionKey key) {
Runnable r = (Runnable) key.attachment();
if (r != null) {
r.run();
}
}
private void printSelectedKeys(Set<SelectionKey> selected) {
List<String> keyInfo = new ArrayList<>(selected.size());
for (SelectionKey sk : selected) {
String channelInfo = "";
if (sk.channel() instanceof SocketChannel) {
channelInfo = "SocketChannel port " + ((SocketChannel) sk.channel()).socket().getPort();
} else {
channelInfo = "ServerSocketChannel";
}
String readyOps = "";
if ((sk.readyOps() & SelectionKey.OP_ACCEPT) > 0) {
readyOps += "ACCEPT ";
}
if ((sk.readyOps() & SelectionKey.OP_CONNECT) > 0) {
readyOps += "CONN ";
}
if ((sk.readyOps() & SelectionKey.OP_READ) > 0) {
readyOps += "READ ";
}
if ((sk.readyOps() & SelectionKey.OP_WRITE) > 0) {
readyOps += "WRITE ";
}
String interestOps = "";
if ((sk.interestOps() & SelectionKey.OP_ACCEPT) > 0) {
interestOps += "ACCEPT ";
}
if ((sk.interestOps() & SelectionKey.OP_CONNECT) > 0) {
interestOps += "CONN ";
}
if ((sk.interestOps() & SelectionKey.OP_READ) > 0) {
interestOps += "READ ";
}
if ((sk.interestOps() & SelectionKey.OP_WRITE) > 0) {
interestOps += "WRITE ";
}
keyInfo.add(String.format("[%s, interestOps: %s, readyOps: %s]", channelInfo, interestOps, readyOps));
}
System.out.println(String.join(", ", keyInfo));
}
private class Acceptor implements Runnable {
@Override
public void run() {
try {
SocketChannel c = serverSocket.accept();
if (c != null) {
new Handler(selector, c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
gordon.study.socket.nio.reactor.singlethread.Handler.java
public final class Handler implements Runnable {
private final SocketChannel socket;
private final SelectionKey sk;
private ByteBuffer input = ByteBuffer.allocate(1024);
private ByteBuffer output = ByteBuffer.allocate(1024);
private static final int READING = 0, SENDING = 1;
private int state = READING;
public Handler(Selector sel, SocketChannel c) throws IOException {
socket = c;
c.configureBlocking(false);
sk = socket.register(sel, 0);
sk.attach(this);
sk.interestOps(SelectionKey.OP_READ);
}
@Override
public void run() {
try {
if (state == READING) {
read();
} else if (state == SENDING) {
send();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean inputIsComplete() {
if (input.position() >= 4) {
int length = input.getInt();
return input.position() >= length;
}
return false;
}
private boolean outputIsComplete() {
return output.remaining() == 0;
}
private void process() {
input.flip();
byte[] bytes = new byte[input.getInt() - 4];
input.get(bytes);
String msg = new String(bytes);
int remotePort = socket.socket().getPort();
System.out.println(remotePort + " Processing ... " + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(remotePort + " Processed ... " + msg);
output.put((byte) 'Y');
output.flip();
}
private void read() throws IOException {
socket.read(input);
if (inputIsComplete()) {
process();
state = SENDING;
sk.interestOps(SelectionKey.OP_WRITE);
}
}
private void send() throws IOException {
socket.write(output);
if (outputIsComplete()) {
sk.cancel();
}
}
}
gordon.study.socket.nio.reactor.singlethread.Main.java
public class Main {
public static void main(String[] args) throws IOException {
new Thread(new Reactor(8888)).start();
for (int i = 0; i < 10; i++) {
new Thread(new SocketClient()).start();
}
}
private static class SocketClient implements Runnable {
private String[] msgArray = { "ni hao", "hello", "chi le ma?", "你瞅啥?", "hi dude" };
@Override
public void run() {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(8888));
System.out.printf(" client (port %d) connected to server.\n", socket.getLocalPort());
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
int pos = ThreadLocalRandom.current().nextInt(msgArray.length);
sendMsg(msgArray[pos], dos);
char result = (char) dis.read();
System.out.printf(" client (port %d) get response from server: %s\n", socket.getLocalPort(), result);
dis.close();
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendMsg(String msg, DataOutputStream dos) throws Exception {
byte[] bytes = msg.getBytes();
int totalLength = 4 + bytes.length;
dos.writeInt(totalLength);
dos.write(bytes);
}
}
}
分析代码可以看出 Selector 是 Reactor 的核心,所有的 Channel 都会注册到 Selector 上。每个 Channel 对应一个 Handler 实例(Acceptor 视为特殊的 Handler),该 Handler 实例作为附件(attachment)附加在 SelectionKey 上。
这样,分发逻辑就十分简单了:当 Selector 选出 selectedKeys 时,遍历每个 key,拿出其附带的 Handler,执行其 run 方法即可。显然,Acceptor 与 Handler 实现 Runnable 接口的目的并不是为了多线程,只是为了有个共同的抽象(定义一个 AbstractHandler 替换 Runnable 的使用会更加容易理解一些)。
执行 main 方法,可以感受到单线程的效率低下(因为业务逻辑中 sleep 了一秒钟)。
有一个疑惑是关于 readyOps,从日志可以看出,当客户端发送了数据,SelectionKey 被选中时,readyOps 居然只包含 READ,而不包含 WRITE。在原来的理解中,这时 Channel 是可以向客户端发送数据的,所以 readyOps 应该包含 WRITE 才对啊?虽然可以写一段代码确认 readyOps 是不是受限于当前的 interestOps,但是意义不大,交给下一轮学习直接看源码吧。
B. 工作线程池版
一个最直接的优化思路就是将解码、业务处理和编码这些与 IO 无关的操作放到工作线程池中运行,以提高的 Reactor 的效率。
gordon.study.socket.nio.reactor.multithread.Handler.java
public final class Handler implements Runnable {
private final SocketChannel socket;
private final SelectionKey sk;
private ByteBuffer input = ByteBuffer.allocate(1024);
private ByteBuffer output = ByteBuffer.allocate(1024);
private static Executor executor = Executors.newCachedThreadPool();
private static final int READING = 0, SENDING = 1, PROCESSING = 2;
private int state = READING;
public Handler(Selector sel, SocketChannel c) throws IOException {
socket = c;
c.configureBlocking(false);
sk = socket.register(sel, 0);
sk.attach(this);
sk.interestOps(SelectionKey.OP_READ);
}
@Override
public void run() {
try {
if (state == READING) {
read();
} else if (state == SENDING) {
send();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean inputIsComplete() {
if (input.position() >= 4) {
int length = input.getInt();
return input.position() >= length;
}
return false;
}
private boolean outputIsComplete() {
return output.remaining() == 0;
}
private void process() {
input.flip();
byte[] bytes = new byte[input.getInt() - 4];
input.get(bytes);
String msg = new String(bytes);
int remotePort = socket.socket().getPort();
System.out.println(remotePort + " Processing ... " + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(remotePort + " Processed ... " + msg);
output.put((byte) 'Y');
output.flip();
}
private void read() throws IOException {
socket.read(input);
if (inputIsComplete()) {
state = PROCESSING;
executor.execute(new Processer());
}
}
private void send() throws IOException {
socket.write(output);
if (outputIsComplete()) {
sk.cancel();
}
}
private void processAndHandOff() {
process();
state = SENDING;
sk.interestOps(SelectionKey.OP_WRITE);
sk.selector().wakeup(); // important!
}
private class Processer implements Runnable {
public void run() {
processAndHandOff();
}
}
}
关于代码,在我看来,Handler 依然是线程安全的,所以没必要使用 synchronize 关键字。
第85行调用 selector 的 wakeup 方法很重要,否则服务端不会将响应内容发送给客户端。表面上的原因显然是第84行的 set interest ops 操作(发生在某个工作线程中)没有对 Reactor 所在线程(即主线程)当前阻塞的方法 Selector.select() 生效,所以通过 wakeup 方法强行终止掉本次阻塞,以期待下次 select 方法能接收到该 Channel 的 WRITE 事件,使 dispatch 循环正常运行下去。深层原因(为什么不生效)留给下一轮看源码分析吧。
C. 多 Reactor 版
如 06 篇所分析,处理连接建立的 Reactor(Selector)与处理数据传输的 Reactor(Selector)需要分开。更进一步,为了利用多核的能力,处理数据传输的 Reactor 应该有多个。
下图中 mainReactor 用于处理连接,subReactor 用于处理数据传输。
gordon.study.socket.nio.reactor.multireactor.Reactor.java
public class Reactor implements Runnable {
private Selector selector;
private ServerSocketChannel serverSocket;
public Reactor(int port) throws IOException {
selector = Selector.open();
serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(port));
serverSocket.configureBlocking(false);
SelectionKey sk = serverSocket.register(selector, SelectionKey.OP_ACCEPT);
sk.attach(new Acceptor());
}
public Reactor(Selector selector) throws IOException {
this.selector = selector;
}
@Override
public void run() {
try {
while (!Thread.interrupted()) {
if(selector.select(100) == 0) {
continue;
}
Set<SelectionKey> selected = selector.selectedKeys();
printSelectedKeys(selected);
Iterator<SelectionKey> it = selected.iterator();
while (it.hasNext()) {
dispatch(it.next());
}
selected.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void dispatch(SelectionKey key) {
Runnable r = (Runnable) key.attachment();
if (r != null) {
r.run();
}
}
private void printSelectedKeys(Set<SelectionKey> selected) {
}
private class Acceptor implements Runnable {
Selector[] selectors = new Selector[2];
int next = 0;
public Acceptor() throws IOException {
Executor executor = Executors.newFixedThreadPool(selectors.length);
for (int i = 0; i < selectors.length; i++) {
selectors[i] = Selector.open();
Reactor subReactor = new Reactor(selectors[i]);
executor.execute(subReactor);
}
}
@Override
public void run() {
try {
SocketChannel c = serverSocket.accept();
System.out.printf(" server established connection: %d\n", c.socket().getPort());
if (c != null) {
new Handler(selectors[next], c);
}
if (++next == selectors.length) {
next = 0;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我选择让 Acceptor 创建 subReactor(s),并通过 Round Robin 的方式将连接平均分配给 subReactors。 每个 subReactor 都运行在一个独立线程中。
代码24行选择了带超时的 select 方法,否则无论我怎样调用 wakeup,程序总是有概率卡死,原因不明。
Java网络编程学习A轮_08_NIO的Reactor模型的更多相关文章
- Java网络编程学习A轮_01_目标与基础复习
A. A轮目标 复习网络编程基础知识,重点学习下TCP三次握手四次挥手,以及可能引发的异常情况. 回顾 Socket 编程,好多年没写(chao)过相关代码了. 重学 NIO,以前学的基本忘光了,毕竟 ...
- Java网络编程学习A轮_06_NIO入门
参考资料: 老外写的教程,很适合入门:http://tutorials.jenkov.com/java-nio/index.html 上面教程的译文:http://ifeve.com/overview ...
- Java网络编程学习A轮_07_基于Buffer的Socket编程
示例代码: https://github.com/gordonklg/study,socket module A. LineSeparate 基于 Buffer 实现逐行读取的 EchoServer ...
- Java网络编程学习A轮_05_Socket编程
示例代码: https://github.com/gordonklg/study,socket module A. Socket 编程简单例子 最简单的 Socket 编程是通过回车/换行符,整行读取 ...
- Java网络编程学习A轮_03_抓包分析TCP四次挥手
参考资料: http://www.jellythink.com/archives/705 示例代码: https://github.com/gordonklg/study,socket module ...
- Java网络编程学习A轮_04_TCP连接异常
参考资料: https://huoding.com/2016/01/19/488 示例代码: https://github.com/gordonklg/study,socket module A. C ...
- Java网络编程学习A轮_02_抓包分析TCP三次握手过程
参考资料: https://huoding.com/2013/11/21/299 https://hpbn.co/building-blocks-of-tcp/#three-way-handshake ...
- Java 网络编程学习总结
新手一枚,Java学习中,把自己学习网络编程的知识总结一下,梳理下知识,方便日后查阅,高手莫进. 本文的主要内容: [1] 网络编程认识 [2] TCP/IP编程 ...
- Java网络编程学习笔记
Java网络编程,我们先来看下面这一张图: 由图可得:想要进行网络编程,首先是服务器端通过ServerSocket对某一个端口进行监听.通过accept来判断是否有客户端与其相连.若成功连上,则通过r ...
随机推荐
- csrf_token之全局认证与局部认证
1.settings.py没有注释到csrf.当post请求的方式会报错. 接下来就解决问题! 1. django中间件 最多5个 - process_request 请求 - process_vie ...
- Lucene.net的简单使用
一.Lucene.net的简单介绍 1.为什么要使用Lucene.net 使用like的模糊查询,模糊度太低,中间添加几个字就无法查找.同时会造成数据库的全文检索,效率低下,数据库服务器造 ...
- SpringIoc的精彩讲解
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- pta 习题集 5-15 数组循环左移
本题要求实现一个对数组进行循环左移的简单函数:一个数组aa中存有nn(>0>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向左移mm(≥0≥0)个位置,即将aa中的数据由(a0a ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- HDU_2586_How far away ?
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- acceptorThreadCount
Apache Tomcat 7 Configuration Reference (7.0.92) - The HTTP Connector https://tomcat.apache.org/tomc ...
- Django - rest - framework - 上
一.快速实例 http://www.django-rest-framework.org/tutorial/quickstart/#quickstart http://www.cnblogs.com/y ...
- 8.python中双冒号(::)的用法
s[i:j:k]是,根据该“片第从i到j与第k步”.何时i和j缺席,整个序列是和s[::k]意思是“每k个项目”. 示例 首先,让我们来初始化一个列表: >>> s = range( ...
- 概率图模型PFM——无向图
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdYAAAFPCAIAAAB/EXiGAAAgAElEQVR4nO2df4wl1XXn6/+0VuG/II ...