SelectorProvider

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.
 
 
 
 
 

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

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button