NIOMON -  NIO  Network  Monitor Tool

Introduction

NIOMON is a simple NIO network monitoring tool. (670 words; April, 2002)  

By Clayton Press

Introduction

Java 1.4 introduced a new API for high performance, nonblocking I/O and networking. The API consists of three development packages: java.nio, java.nio.channels, java.nio.charset, and two Service-provider packages: java.nio.channels.spi, java.nio.charset.spi.

Multiplexed nonblocking IO is also useful for client applications that maintain multiple network connections in a non-blocking manner - for example a web browser.

Developing client and server network applications is complex. There are a range of issues to consider: multi-threading, sockets, configuration, tuning. As the demand for high peformance low latency network application increases, so does the need for developer tools with which to test & monitor such applications, during development and in production.

The NIOMON network application monitor is a simple tool which which to monitor NIO-based java applications. Thus far, it monitors on the run:

NIO Use Case

Previously, standard IO server applications required a dedicated thread for each connection. A multi-threaded socket server, serving hundreds of connections, must maintain hundreds of threads. NIO introduces channels and selectors which allow multiplexed, non-blocking I/O operations. The Channel Selector semantics enable a dramatic reduction in multi-threaded resources and complexity.

Let’s take a look at a simple NIO test application.

NIOMON Network monitor tool

The NIOMON Network monitor connects multiple NIOClient to one NIOServer .   The NIOClient sends a message to the NIOServer over a socket. The NIOServer responds back. This continues according to a TTL "count" parameter. The "count" parameter is configured on the NIOClient.

The application is composed of one server and multiple client components.   The client sends a message to the server.   The client displays the response back from the server.   The server component manages remote communication with multiple connected clients.   The server sends the message back to the respective client.  

NIOMON consists of 2 standalone java applications, NIOClient and NIOServer, which communicate over TCP network sockets. NIOServer is capable of handling multiple NIOClients

The server application

The client application Both client and server application automated testing

References and ReferenceQueues

Reference and ReferenceQueue types merit special comment.

In a high frequency environment, close attention to memory management is required, hence Reference types. The java.lang.ref Package description says it best:

A Reference object encapsulates a reference to some other object so that the reference itself may be examined and manipulated like any other object. Three types of reference objects are provided, each weaker than the last: soft, weak, and phantom. Each type corresponds to a different level of reachability, as defined below. Soft references are for implementing memory-sensitive caches, weak references are for implementing canonicalizing mappings that do not prevent their keys (or values) from being reclaimed, and phantom references are for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.

Each reference-object type is implemented by a subclass of the abstract base Reference class. An instance of one of these subclasses encapsulates a single reference to a particular object, called the referent. Every reference object provides methods for getting and clearing the reference. Aside from the clearing operation reference objects are otherwise immutable, so no set operation is provided. A program may further subclass these subclasses, adding whatever fields and methods are required for its purposes, or it may use these subclasses without change.

Notification A program may request to be notified of changes in an object's reachability by registering an appropriate reference object with a reference queue at the time the reference object is created. Some time after the garbage collector determines that the reachability of the referent has changed to the value corresponding to the type of the reference, it will add the reference to the associated queue. At this point, the reference is considered to be enqueued. The program may remove references from a queue either by polling or by blocking until a reference becomes available. Reference queues are implemented by the ReferenceQueue class.

Application Design

NIOServer Walk Through

A condensed version of the Server class logic, is listed below.  Exception handling and other support logic has been excluded for clarity. Refer to the source code for more details. BASIC INFO

   public class NIOServer {

        // try - catch blocks omitted for brevity


	1	ServerSocketChannel ssc = ServerSocketChannel.open();
	2	ssc.socket().bind(new InetSocketAddress( port ));
	3	ssc.configureBlocking(false);


	4	SelectionKey serverkey = ssc.register(selector, SelectionKey.OP_ACCEPT);


	5	selector.select();	//block until a channel is ready for I/O


	6	SocketChannel client = ssc.accept();
	7	client.configureBlocking(false);
	8	SelectionKey clientkey = client.register(selector, SelectionKey.OP_READ);


	9	bytesread = client.read(buffer);


	10	buffer.flip();
	11	String request = decoder.decode(buffer).toString();
	12	buffer.clear();

   }

Program Notes

How to Download, Install and Run

Complete source code is available for research and development purposes.   The following link provides step-by-step technical information to guide you through the Download and Installation process.

N I O M O N    T e c h n i c a l    S u p p o r t

Java  Documentation

J a v a    D o c

Conclusion

Java NIO provides for high performance, non-blocking, multiplexed I/O network server development.

About the Author

Clayton Press has worked in Chicago as a systems developer since 1982.   He has built trade systems for the CME, CBOT, UBS, Bank of America, Bank of Montreal, Lehman Brothers and T.Rowe Price.   He can be reached at press0@gmail.com

References