Getty is a NIO framework I wrote to learn Java NIO. During the implementation process, I referred to the design of Netty and used Groovy to implement it. Although it is just a toy, the sparrow is small and has all the internal organs. During the implementation process, I not only became familiar with the use of NIO, but also learned a lot of Netty's design ideas, improving my coding and design capabilities.
As for why I use Groovy to write it, because I just learned Groovy and just used it to practice. In addition, Groovy is compatible with Java, so the difference is only in syntax. The underlying implementation is still based on Java APIof.
Getty’s core code lines do not exceed 500 lines. On the one hand, it benefits from Groovy’s concise syntax, and on the other hand, because I only implemented the core logic. The most complicated thing is actually the decoder implementation. Scaffolding is easy to build, but building a skyscraper is not that easy, but it is enough for learning NIO.
Getty uses Reactor multi-threaded model
Getty-Detailed explanation of implementing Java NIO framework designs .
trigger will trigger the corresponding Getty-Detailed explanation of implementing Java NIO framework design, and the Getty-Detailed explanation of implementing Java NIO framework design handler will handle the corresponding Getty-Detailed explanation of implementing Java NIO framework design respectively. Response to complete the server-side business processing.
Event definitiononRead: This Getty-Detailed explanation of implementing Java NIO framework design is triggered when the client sends data and has been correctly read by the worker thread. This Getty-Detailed explanation of implementing Java NIO framework design notifies each Getty-Detailed explanation of implementing Java NIO framework design handler that the data sent by the client can be actually processed.
onWrite: This Getty-Detailed explanation of implementing Java NIO framework design is triggered when the client can start to accept data sent by the server. Through this Getty-Detailed explanation of implementing Java NIO framework design, we can send response data to the client. (Write Getty-Detailed explanation of implementing Java NIO framework designs are not used in the current implementation)
onClosed: This Getty-Detailed explanation of implementing Java NIO framework design is triggered when the client disconnects from the server.
: This is an Getty-Detailed explanation of implementing Java NIO framework designinterface
, which defines the server Getty-Detailed explanation of implementing Java NIO framework designs to be listened to interface ServerListener extends Serializable{
/**
* 可读事件回调
* @param request
*/
void onRead(ctx)
/**
* 可写事件回调
* @param request
* @param response
*/
void onWrite(ctx)
/**
* 连接关闭回调
* @param request
*/
void onClosed(ctx)
}
: Implement an adapter (EventAdapter) for the Serverlistener interface. The advantage of this is that the final Getty-Detailed explanation of implementing Java NIO framework design processor can only handle the Getty-Detailed explanation of implementing Java NIO framework designs of concern. <pre class="brush:java;toolbar:false;">class EventAdapter implements ServerListener {
//下个处理器的引用
protected next
void onRead(Object ctx) {
}
void onWrite(Object ctx) {
}
void onClosed(Object ctx) {
}
}</pre>
ifier<a href="http://www.php.cn/wiki/109.html" target="_blank">: Used to notify the registered Getty-Detailed explanation of implementing Java NIO framework design handler to respond to the Getty-Detailed explanation of implementing Java NIO framework design by triggering server Getty-Detailed explanation of implementing Java NIO framework designs at the appropriate time. response. </a><pre class="brush:java;toolbar:false;">interface Notifier extends Serializable{
/**
* 触发所有可读事件回调
*/
void fireOnRead(ctx)
/**
* 触发所有可写事件回调
*/
void fireOnWrite(ctx)
/**
* 触发所有连接关闭事件回调
*/
void fireOnClosed(ctx)
}</pre>
: Implements the Notifier
interface to maintain an orderly Getty-Detailed explanation of implementing Java NIO framework design handler chain, starting from the first handler each time trigger. <pre class="brush:java;toolbar:false;">class HandlerChain implements Notifier{
EventAdapter head
EventAdapter tail
/**
* 添加处理器到执行链的最后
* @param handler
*/
void addLast(handler) {
if (tail != null) {
tail.next = handler
tail = tail.next
} else {
head = handler
tail = head
}
}
void fireOnRead(ctx) {
head.onRead(ctx)
}
void fireOnWrite(ctx) {
head.onWrite(ctx)
}
void fireOnClosed(ctx) {
head.onClosed(ctx)
}
}</pre>
: Implements the Notifier
interface as an Getty-Detailed explanation of implementing Java NIO framework design bus to maintain a list of Getty-Detailed explanation of implementing Java NIO framework design chains. <pre class="brush:java;toolbar:false;">class PipeLine implements Notifier{
static logger = LoggerFactory.getLogger(PipeLine.name)
//监听器队列
def listOfChain = []
PipeLine(){}
/**
* 添加监听器到监听队列中
* @param chain
*/
void addChain(chain) {
synchronized (listOfChain) {
if (!listOfChain.contains(chain)) {
listOfChain.add(chain)
}
}
}
/**
* 触发所有可读事件回调
*/
void fireOnRead(ctx) {
logger.debug("fireOnRead")
listOfChain.each { chain ->
chain.fireOnRead(ctx)
}
}
/**
* 触发所有可写事件回调
*/
void fireOnWrite(ctx) {
listOfChain.each { chain ->
chain.fireOnWrite(ctx)
}
}
/**
* 触发所有连接关闭事件回调
*/
void fireOnClosed(ctx) {
listOfChain.each { chain ->
chain.fireOnClosed(ctx)
}
}
}</pre>
Programming model
Event processing adopts the chain of responsibility model, each processor After processing the data, it is decided whether to continue executing the next processor. If the processor does not hand over the task to the thread pool for processing, then the entire processing process is processed in the same thread. And each connection has a separate
PipeLine. The worker thread can switch between multiple connection contexts, but one connection context will only be processed by one thread. <h2>核心类</h2>
<h3>ConnectionCtx</h3>
<p>连接上下文<code>ConnectionCtx
class ConnectionCtx { /**socket连接*/ SocketChannel channel /**用于携带额外参数*/ Object attachment /**处理当前连接的工作线程*/ Worker worker /**连接超时时间*/ Long timeout /**每个连接拥有自己的pipeline*/ PipeLine pipeLine }
NioServer
主线程负责监听端口,持有工作线程的引用(使用轮转法分配连接),每次有连接到来时,将连接放入工作线程的连接队列,并唤醒线程selector.wakeup()
(线程可能阻塞在selector
上)。
class NioServer extends Thread { /**服务端的套接字通道*/ ServerSocketChannel ssc /**选择器*/ Selector selector /**事件总线*/ PipeLine pipeLine /**工作线程列表*/ def workers = [] /**当前工作线程索引*/ int index }
工作线程,负责注册server传递过来的socket连接。主要监听读事件,管理socket,处理写操作。
class Worker extends Thread { /**选择器*/ Selector selector /**读缓冲区*/ ByteBuffer buffer /**主线程分配的连接队列*/ def queue = [] /**存储按超时时间从小到大的连接*/ TreeMap<Long, ConnectionCtx> ctxTreeMap void run() { while (true) { selector.select() //注册主线程发送过来的连接 registerCtx() //关闭超时的连接 closeTimeoutCtx() //处理事件 dispatchEvent() } } }
我实现了一系列处理HTTP
请求的处理器,具体实现看代码。
LineBasedDecoder
:行解码器,按行解析数据
HttpRequestDecoder
:HTTP请求解析,目前只支持GET请求
HttpRequestHandler
:Http 请求处理器,目前只支持GET方法
HttpResponseHandler
:Http响应处理器
下面是写在test
中的例子
class WebServerTest { static void main(args) { def pipeLine = new PipeLine() def readChain = new HandlerChain() readChain.addLast(new LineBasedDecoder()) readChain.addLast(new HttpRequestDecoder()) readChain.addLast(new HttpRequestHandler()) readChain.addLast(new HttpResponseHandler()) def closeChain = new HandlerChain() closeChain.addLast(new ClosedHandler()) pipeLine.addChain(readChain) pipeLine.addChain(closeChain) NioServer nioServer = new NioServer(pipeLine) nioServer.start() } }
另外,还可以使用配置文件getty.properties
设置程序的运行参数。
#用于拼接消息时使用的二进制数组的缓存区 common_buffer_size=1024 #工作线程读取tcp数据的缓存大小 worker_rcv_buffer_size=1024 #监听的端口 port=4399 #工作线程的数量 worker_num=1 #连接超时自动断开时间 timeout=900 #根目录 root=.
Getty是我造的第二个小轮子,第一个是RedisHttpSession。都说不要重复造轮子。这话我是认同的,但是掌握一门技术最好的方法就是实践,在没有合适项目可以使用新技术的时候,造一个简单的轮子是不错的实践手段。
Getty 的缺点或者说还可以优化的点:
线程的使用直接用了Thread
类,看起来有点low。等以后水平提升了再来抽象一下。
目前只有读事件是异步的,写事件是同步的。未来将写事件也改为异步的。
The above is the detailed content of Getty-Detailed explanation of implementing Java NIO framework design. For more information, please follow other related articles on the PHP Chinese website!