Example usage for java.nio.channels ServerSocketChannel register

List of usage examples for java.nio.channels ServerSocketChannel register

Introduction

In this page you can find the example usage for java.nio.channels ServerSocketChannel register.

Prototype

public final SelectionKey register(Selector sel, int ops) throws ClosedChannelException 

Source Link

Document

Registers this channel with the given selector, returning a selection key.

Usage

From source file:org.pvalsecc.comm.MultiplexedServer.java

/**
 * Attempt to create the listening socket for at most 5 minutes.
 *///ww  w .j a v  a2  s  . c  o m
private void createSocket() {
    while (!stop) {
        ServerSocketChannel serverSocketChannel = null;

        try {
            synchronized (selectorLOCK) {
                if (!stop) {
                    selector = Selector.open();
                }
            }

            if (!stop) {
                serverSocketChannel = ServerSocketChannel.open();
                //serverSocketChannel.socket().setReuseAddress(true);
                serverSocketChannel.configureBlocking(false);
                serverSocketChannel.socket().bind(address);
                serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
                LOGGER.info("[" + threadName + "] Start to listen on " + address);
            }

            break;
        } catch (IOException e) {
            //noinspection StringContatenationInLoop
            LOGGER.warn("Cannot start to listen on " + threadName + " (will try again later)", e);

            SystemUtilities.safeClose(serverSocketChannel);

            try {
                Thread.sleep(5000);
            } catch (InterruptedException ignored) {
                //ignored
            }
        }
    }
}