Home » Core Java » nio » channels » spi » SelectorProvider » java.nio.channels.spi.SelectorProvider Example

About Prasad Saya

Prasad Saya
Prasad Saya is a software engineer with over ten years’ experience in application development, maintenance, testing and consulting on various platforms. He is a certified Java and Java EE developer. At present his interest is in developing Java applications. He also has experience working with databases and ERP applications.

java.nio.channels.spi.SelectorProvider Example

SelectorProvider is an abstract class defined in the java.nio.channels.spi package. This is a central service-provider class for selectors and selectable channels defined in the java.nio.channels API.

A selector provider is a concrete subclass of this class that has a zero-argument constructor and implements the abstract factory methods of this class that return open channels and selector objects.

Want to be a Java NIO Master ?

Subscribe to our newsletter and download the Java NIO Programming Cookbook right now!

In order to help you master Java NIO Library, we have compiled a kick-ass guide with all the major Java NIO features and use cases! Besides studying them online you may download the eBook in PDF format!
A Java virtual machine maintains a single system wide default provider instance. This instance is returned by the provider() static method of this class. This default provider is used by the open() static methods of the DatagramChannel, Pipe, Selector, ServerSocketChannel, and SocketChannel classes; all the channel classes are subclasses of SelectableChannel.

For example, a ServerSocketChannel can be created as follows:

ServerSocketChannel channel = ServerSocketChannel.open();

The same function is achieved by using the SelectorProvider‘s provider() static method, as follows:

ServerSocketChannel channel = SelectorProvider.provider().openServerSocketChannel();

A custom provider implementation class can be specified as a system property: java.nio.channels.spi.SelectorProvider.

1. An Example

The example shows creating a ServerSocketChannel using the default SelectorProvider.

SelectorProviderExample.java

import java.nio.channels.spi.SelectorProvider;
import java.nio.channels.ServerSocketChannel;
import java.net.InetSocketAddress;
import java.io.IOException;

public class SelectorProviderExample {

    public static void main (String [] args)
            throws IOException {

        ServerSocketChannel serverChannel = SelectorProvider.provider().openServerSocketChannel();
        InetSocketAddress hostAddress = new InetSocketAddress("localhost", 3888);
        serverChannel.bind(hostAddress);

        System.out.println("Server socket channel bound to port: " + hostAddress.getPort());
        System.out.println("Waiting for client to connect... ");

        SocketChannel socketChannel = serverChannel.accept(); // the socket channel for the new connection

        // Process further; send or receive messages to-fro client here ...

        socketChannel.close();
        serverChannel.close();
    }
}

NOTE: The code on line 11 shows the creation of the server socket channel using the default provider.

1.1. The Output

Server socket channel bound to port: 3888
Waiting for client to connect...

From the output, the serverChannel.accept() method waits for the connection from a client socket channel. Note, the details of the processing the messages to or from client and the client program is not shown here.

2. Download Java Source Code

This was an example of java.nio.channels.spi.SelectorProvider

Download
You can download the full source code of this example here: SelectorProviderExample.zip
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of