Tuesday, October 6, 2015

Introduction to Java NIO

                              

           IO Operations plays an important role when it comes to integrate network level operations into software applications.Assume you are going to develop an application which analysis some incoming event stream and output important relationship between the events in the event stream and generate new events send them as separate event streams to different applications which are decoupled through network level or hardware level.


Definitely your application should listen for incoming events from the network level and write to network. So before developing the application developer should have a basic understanding of the network level operations. So in this post I would like to introduce some JAVA related IO Operation handling strategies .

Socket

A socket is one endpoint of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively.

Socket Channel

A Java NIO SocketChannel is a channel that is connected to a TCP network socket. There can be more than one SocketChannel connected to a Socket which means there can be many open connections  in one endpoint.


Blocking IO

At the beginning  of  network programming this model is used. Separate connection need to be handle by separate thread . So this method is not scalable with high number of connections for a socket.

blocking.png

Non Blocking IO

With the introduction of Java 1.4  they have introduced Non-Blocking IO which means connection processing threads are not wait for a socketchannel. so we can processed IO events in Asynchronous manner .

nonblocking.png




Reactor Pattern

Reactor pattern in the domain of Java NIO is used  to handle incoming connections and their  related operations in more scalable and accepted manner with high concurrency scenarios. Basically there is manager thread which manage connections and handover connections to worker threads carefully.  Thereafter worker threads are responsible for handling IO events for connections lifetime.

Selector
Selector is the way to  make system calls to the operating system for detect events through OS. Instead of using multiple threads , using selector which is one thread responsible for  handling multiple concurrent channels is minimized thread switching overhead and increase scalability with increasing connections. Channels are registered to selector based on operations which called Selection Keys. There are four operations where channel can fire event to Selector They are as

  • Connect
  • Accept
  • Read
  • Write
while registering a channel to selector you can specify I am interesting in this  kind of event of the channel and  as well as you can attached an object to the selector for identify the channel uniquely.

Byte Buffers
Another  interesting component in the IO Operations is Byte Buffers. Basically we have
  • Direct Byte Buffers
  • Heap Byte Buffers

Direct Byte Buffers are the  Byte Buffers which are allocated inside the memory .So allocation and deallocation has  high cost  and need to re use. But if using direct byte buffers zero copy capability can be enabled.

Heap byte buffers can be fastly allocate and deallocate in JVM heap space.

There are common Bytebuffer operations that are heavily used with Byte Buffers.ByteBuffer has index  , limit and capacity.  index is the current position of read or write. limit is the position of new data written in read mode . capacity is the maximum size  that data can be written.

  • Flip ()
                  Calling this method will be caused to switch to read mode of the byte buffer. then index will be came to lastest  read position and limit is set to maximum index of newly written data.

  • Compact()
         Enables write mode and move data to beginning of the buffer.   and update index latest value of the read position and limit to capacity.           

  • Clear()
   Enables write mode and move index to beginning of the buffer and limit to capacity.

  • hasRemaining()
before calling this buffer need to be  flip and then this will written difference between  index and limit.

Sample Http Reverse Proxy Written Using HttpCore

For better understanding and investigation of the Java NIO implementations and how to use it actually for read and write data can be seen at  [1] . This basically read the data from socket and write data to socket using Java NIO library called Http Core.







No comments:

Post a Comment