Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Five ways to maximize Java NIO and NIO.2

Build more responsive Java applications with the New Input/Output APIs

  • Print
  • Feedback

Page 3 of 5

2. Selectors and asynchronous I/O: Selectors help multiplex

Newcomers to NIO sometimes associate it with "non-blocking input/output." NIO is more than non-blocking I/O but the error makes sense: basic I/O in Java is blocking -- meaning that it waits until it can complete an operation -- whereas non-blocking, or asynchronous, I/O is one of the most-used NIO facilities.

NIO's non-blocking I/O is event-based, as demonstrated by the file-system listener in Listing 1. This means that a selector (or callback or listener) is defined for an I/O channel, then processing continues. When an event occurs on the selector -- when a line of input arrives, for instance -- the selector "wakes up" and executes. All of this is achieved within a single thread, which is a significant contrast to typical Java I/O.

Listing 2 demonstrates the use of NIO selectors in a multi-port networking echo-er, a program slightly modified from one created by Greg Travis in 2003 (see Resources). Unix and Unix-like operating systems have long had efficient implementations of selectors, so this sort of networking program is a model of good performance for a Java-coded networking program.

Listing 2. NIO selectors

import java.io.*;
  import java.net.*;
  import java.nio.*;
  import java.nio.channels.*;
  import java.util.*;
  
  public class MultiPortEcho
  {
    private int ports[];
    private ByteBuffer echoBuffer = ByteBuffer.allocate( 1024 );
  
    public MultiPortEcho( int ports[] ) throws IOException {
      this.ports = ports;
  
      configure_selector();
    }
  
    private void configure_selector() throws IOException {
      // Create a new selector
      Selector selector = Selector.open();
  
      // Open a listener on each port, and register each one
      // with the selector
      for (int i=0; i<ports.length; ++i) {
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);
        ServerSocket ss = ssc.socket();
        InetSocketAddress address = new InetSocketAddress(ports[i]);
        ss.bind(address);
  
        SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);
  
        System.out.println("Going to listen on " + ports[i]);
      }
  
      while (true) {
        int num = selector.select();
  
        Set selectedKeys = selector.selectedKeys();
        Iterator it = selectedKeys.iterator();
  
        while (it.hasNext()) {
          SelectionKey key = (SelectionKey) it.next();
  
          if ((key.readyOps() & SelectionKey.OP_ACCEPT)
            == SelectionKey.OP_ACCEPT) {
            // Accept the new connection
            ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
            SocketChannel sc = ssc.accept();
            sc.configureBlocking(false);
  
            // Add the new connection to the selector
            SelectionKey newKey = sc.register(selector, SelectionKey.OP_READ);
            it.remove();
  
            System.out.println( "Got connection from "+sc );
          } else if ((key.readyOps() & SelectionKey.OP_READ)
            == SelectionKey.OP_READ) {
            // Read the data
            SocketChannel sc = (SocketChannel)key.channel();
  
            // Echo data
            int bytesEchoed = 0;
            while (true) {
              echoBuffer.clear();
  
              int number_of_bytes = sc.read(echoBuffer);
  
              if (number_of_bytes <= 0) {
                break;
              }
  
              echoBuffer.flip();
  
              sc.write(echoBuffer);
              bytesEchoed += number_of_bytes;
            }
  
            System.out.println("Echoed " + bytesEchoed + " from " + sc);
  
            it.remove();
          }
  
        }
      }
    }
  
    static public void main( String args[] ) throws Exception {
      if (args.length<=0) {
        System.err.println("Usage: java MultiPortEcho port [port port ...]");
        System.exit(1);
      }
  
      int ports[] = new int[args.length];
  
      for (int i=0; i<args.length; ++i) {
        ports[i] = Integer.parseInt(args[i]);
      }
  
      new MultiPortEcho(ports);
    }
  }

Compile this source, then launch it from the command-line with an invocation such as java MultiPortEcho 8005 8006. Once the MultiPortEchoer is running, start up a simple telnet or other terminal emulator running against ports 8005 and 8006. You will see that the program echoes back characters it receives -- and does it in a single Java thread!

  • Print
  • Feedback

Resources