In "Java NIO" author ppt "How to Build a scalable multiplexed Server with NIO" and Doug Lea "scalable IO in Java" ppt
There is a description of the implementation of Java NIO through reactor pattern. Java NIO acts as a cross-platform IO operation. The IO model of the corresponding platform is encapsulated on different platforms.
As already mentioned in the reactor pattern author, cross-platform operations can be achieved through reactor pattern mode. So, Java NiO is done this way through the reactor pattern mode.
Java NIO is using the Select model below the window platform. For the analysis of Java NIO Source code, it is of little significance to understand the design pattern of reactor pattern. Because the Java NIO implementation
The encapsulation of the JNI is required. If you want to understand the encapsulation of different platforms, you can learn more by Zthead libraries. This avoids interference with the JNI. Because JNI involves the knowledge of scripting languages Java and C + + interaction.
How to Build a scalable multiplexed Server with NIO
Reactor Pattern Mapped to NIO
Handle
Selectionkey
Event
Selectionkey.op_read, etc
Demultiplexer
Selector
Dispatcher
Selector.select () + Iterate Selector.selectedkeys ()
Handler
An instance of Runnable or callable
The simplest example: Testreactor.java
public class testreactor{public static void Main (string[] args) throws Exception {//create Serversocketchannel channel. Serversocketchannel Serversocketchannel =serversocketchannel.open (); Set non-blocking, asynchronous mode serversocketchannel.configureblocking (false); The associated serversocket serversocket serversocket = Serversocketchannel.socket (); SocketAddress endpoint =new inetsocketaddress ("127.0.0.1", 8888); Binds the specified port serversocket.bind (endpoint); Create a selector. In reactor pattern mode, the equivalent of demultiplexer function, used for multiplexer Selector sel = Selector.open (); Register the link event in select. Selectionkey equivalent to event events in reactor mode. The Op_read,op_write,op_connect,op_accept event type exists in the Sectionkey. The channel associated with op_accept at this time is serversocketchannel selectionkey selkey = Serversocketchannel.register (sel, SELECTIONKEY.OP_ ACCEPT); while (true) {//is blocking the operation, waiting for the event to arrive. The return value represents the number of completion operations in the SELECT model int selcount = Sel.select (); if (selcount>0) {System.out.println ("selcount=>> "+selcount); }//Returns a collection of keys that can be manipulated. In the window select model, returns the Fd_set collection that can be manipulated set<selectionkey> Selkeyset = Sel.selectedkeys (); for (Selectionkey key:selkeyset) {//in Selectionkey, there is no blocking behavior when the Accept () function is called if there is a link acceptable event. Select if (key.isacceptable ()) {//Gets the channel associated with the selectionkey.op_accept. That is Serversocketchannel. Serversocketchannel Serverchannel = (serversocketchannel) key.channel (); Calling Serversocketchannel does not block. Get to Customer link socketchannel socketchannel = serverchannel.accept (); Set blocking mode socketchannel.configureblocking (false); Read and write event socketchannel.register for the associated Socketchannel (SEL, selectionkey.op_read| Selectionkey.op_write); You can also associate other objects in the Selectionkey. In select Mode, Selectionkey is equivalent to the Completionkey parameter} if (Key.iswritable ()) {Socketch Annel SockEtchannel = (Socketchannel) key.channel (); Bytebuffer src =bytebuffer.allocate (100); Src.putint (100); Src.flip (); Socketchannel.write (SRC); Read and write event socketchannel.register for associated Socketchannel (SEL, selectionkey.op_read); You can also associate other objects in the Selectionkey. In select Mode, Selectionkey is equivalent to the Completionkey parameter} if (Key.isreadable ()) { Socketchannel Socketchannel = (socketchannel) key.channel (); Inetsocketaddress remote = (inetsocketaddress) socketchannel.getremoteaddress (); String remotestring = remote.gethoststring () +remote.getport (); Read and write event socketchannel.register for associated Socketchannel (SEL, selectionkey.op_write); You can also associate other objects in the Selectionkey. In select mode, the Selectionkey is equivalent to the Completionkey parameter} selkeyset.remove (key); } } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The reactor pattern and Java NIO