Example usage for java.nio.channels Selector close

List of usage examples for java.nio.channels Selector close

Introduction

In this page you can find the example usage for java.nio.channels Selector close.

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes this selector.

Usage

From source file:com.voxbone.kelpie.Session.java

public void run() {
    Selector sel = null;

    logger.info("[[" + internalCallId + "]] Session thread started: " + this.socketChannel.socket().toString());

    try {/*from www. j  a va 2s. co  m*/
        sel = SelectorProvider.provider().openSelector();
        socketChannel.register(sel, SelectionKey.OP_READ, this);

        while (true) {
            if (sel.select() >= 0) {
                Iterator<SelectionKey> itr = sel.selectedKeys().iterator();
                while (itr.hasNext()) {
                    SelectionKey key = itr.next();
                    itr.remove();
                    if (key.isReadable()) {
                        this.execute();
                    }
                }
            } else {
                logger.error("[[" + internalCallId + "]] Select returned error");
            }
            if (conn.getCurrentStatus().isDisconnected()) {
                break;
            }
        }

        logger.info("[[" + internalCallId + "]] Session Connection finished: "
                + this.socketChannel.socket().toString());
        sel.close();
    } catch (IOException e) {
        logger.error("[[" + internalCallId + "]] Error in xmpp session thread", e);
    } catch (StreamException e) {
        logger.error("[[" + internalCallId + "]] Error in xmpp session thread", e);
    } catch (Exception e) {
        logger.error("[[" + internalCallId + "]] Error in xmpp session thread", e);
    } finally {
        try {
            if (sel != null) {
                sel.close();
            }
        } catch (IOException e) {
            // we are dead already, RIP
        }
    }

    // make sure the connection is closed
    try {
        conn.close();
        conn.disconnect();
        try {
            socketChannel.socket().shutdownInput();
        } catch (IOException e) {
            // ignore
        }
        try {
            socketChannel.socket().shutdownOutput();
        } catch (IOException e) {
            // ignore
        }
        try {
            socketChannel.close();
        } catch (IOException e) {
            // ignore
        }
    } catch (StreamException e) {
        logger.error("[[" + internalCallId + "]] Problem closing stream", e);
    }
}

From source file:org.apache.axis2.transport.udp.IODispatcher.java

/**
 * Stop the dispatcher./*from   w  ww  .  j a  v a2 s. c  o  m*/
 * This method closes all sockets and causes the execution of the
 * {@link #run()} method to stop.
 * 
 * @throws IOException
 */
public void stop() throws IOException {
    execute(new SelectorOperation() {
        @Override
        public void doExecute(Selector selector) throws IOException {
            IOException exception = null;
            for (SelectionKey key : selector.keys()) {
                try {
                    key.channel().close();
                } catch (IOException ex) {
                    if (exception == null) {
                        exception = ex;
                    }
                }
            }
            try {
                selector.close();
            } catch (IOException ex) {
                if (exception == null) {
                    exception = ex;
                }
            }
            if (exception != null) {
                throw exception;
            }
        }
    });
}

From source file:org.apache.catalina.cluster.tcp.ReplicationListener.java

public void listen() throws Exception {
    doListen = true;//from  w w  w.j a  v  a  2s . c  o  m
    // allocate an unbound server socket channel
    ServerSocketChannel serverChannel = ServerSocketChannel.open();
    // Get the associated ServerSocket to bind it with
    ServerSocket serverSocket = serverChannel.socket();
    // create a new Selector for use below
    Selector selector = Selector.open();
    // set the port the server channel will listen to
    serverSocket.bind(new InetSocketAddress(bind, port));
    // set non-blocking mode for the listening socket
    serverChannel.configureBlocking(false);
    // register the ServerSocketChannel with the Selector
    serverChannel.register(selector, SelectionKey.OP_ACCEPT);
    while (doListen) {
        // this may block for a long time, upon return the
        // selected set contains keys of the ready channels
        try {

            //System.out.println("Selecting with timeout="+timeout);
            int n = selector.select(timeout);
            //System.out.println("select returned="+n);
            if (n == 0) {
                continue; // nothing to do
            }
            // get an iterator over the set of selected keys
            Iterator it = selector.selectedKeys().iterator();
            // look at each key in the selected set
            while (it.hasNext()) {
                SelectionKey key = (SelectionKey) it.next();
                // Is a new connection coming in?
                if (key.isAcceptable()) {
                    ServerSocketChannel server = (ServerSocketChannel) key.channel();
                    SocketChannel channel = server.accept();
                    registerChannel(selector, channel, SelectionKey.OP_READ,
                            new ObjectReader(channel, selector, callback));
                }
                // is there data to read on this channel?
                //System.out.println("key readable="+key.isReadable());
                if (key.isReadable()) {
                    readDataFromSocket(key);
                } else {
                    //System.out.println("This shouldn't get called");
                    key.interestOps(key.interestOps() & (~key.OP_WRITE));
                }

                // remove key from selected set, it's been handled
                it.remove();
            }
            //System.out.println("Done with loop");
        } catch (java.nio.channels.CancelledKeyException nx) {
            log.warn("Replication client disconnected, error when polling key. Ignoring client.");
        } catch (Exception x) {
            log.error("Unable to process request in ReplicationListener", x);
        }

    } //while
    serverChannel.close();
    selector.close();
}

From source file:org.pvalsecc.misc.SystemUtilities.java

public static void safeClose(Selector selector) {
    try {/*  w w w. j  a va2 s  . c o  m*/
        if (selector != null)
            selector.close();
    } catch (IOException e) {
        LOGGER.warn("Cannot close", e);
    }
}