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:Main.java

public static void closeQuietly(Selector selector) {
    if (selector != null) {
        try {/*from   w  ww.  j ava 2  s .  c o  m*/
            selector.close();
        } catch (IOException var2) {
            ;
        }
    }

}

From source file:com.alibaba.napoli.gecko.core.util.SelectorFactory.java

/**
 * Decrease <code>Selector</code> pool size
 *//*from  ww w . j ava  2  s .c om*/
private static void reduce(int size) {
    for (int i = 0; i < maxSelectors - size; i++) {
        try {
            Selector selector = selectors.pop();
            selector.close();
        } catch (IOException e) {
            logger.error("SelectorFactory.reduce", e);
        }
    }
}

From source file:com.github.neoio.nio.util.NIOUtils.java

public static void selectorClose(Selector selector) throws NetSelectorException {
    try {/*from   w  w  w.j av  a  2 s .  c  o m*/
        selector.close();
    } catch (IOException e) {
        throw new NetSelectorException(e);
    }
}

From source file:Main.java

/**
 * Unconditionally close a <code>Selector</code>.
 * <p/>/*from   w w  w  . ja  v  a  2  s.c o  m*/
 * Equivalent to {@link Selector#close()}, except any exceptions will be ignored.
 * This is typically used in finally blocks.
 * <p/>
 * Example code:
 * <pre>
 *   Selector selector = null;
 *   try {
 *       selector = Selector.open();
 *       // process socket
 *
 *   } catch (Exception e) {
 *       // error handling
 *   } finally {
 *       IOUtils.closeQuietly(selector);
 *   }
 * </pre>
 *
 * @param selector the Selector to close, may be null or already closed
 * @since 2.2
 */
public static void closeQuietly(Selector selector) {
    if (selector != null) {
        try {
            selector.close();
        } catch (IOException ioe) {
            // ignored
        }
    }
}

From source file:ee.ria.xroad.proxy.clientproxy.FastestSocketSelector.java

private static void closeSelector(Selector selector, SocketChannel selectedChannel) throws IOException {
    for (SelectionKey key : selector.keys()) {
        if (selectedChannel == null || !selectedChannel.equals(key.channel())) {
            closeQuietly(key.channel());
        }/*from w ww.  j av a  2  s  .  c om*/
    }

    selector.close();
}

From source file:com.baomidou.mybatisplus.toolkit.IOUtils.java

/**
 * Closes a <code>Selector</code> unconditionally.
 * <p>// www.j  a va 2  s  .  c o m
 * Equivalent to {@link Selector#close()}, except any exceptions will be ignored. This is typically used in finally
 * blocks.
 * <p>
 * Example code:
 * <p>
 * <pre>
 * Selector selector = null;
 * try {
 *    selector = Selector.open();
 *    // process socket
 *
 * } catch (Exception e) {
 *    // error handling
 * } finally {
 *    IOUtils.closeQuietly(selector);
 * }
 * </pre>
 *
 * @param selector the Selector to close, may be null or already closed
 * @since 2.2
 */
public static void closeQuietly(final Selector selector) {
    if (selector != null) {
        try {
            selector.close();
        } catch (final IOException ioe) {
            logger.error("error close io", ioe);
        }
    }
}

From source file:net.sf.cindy.impl.SimpleEventGenerator.java

protected void finishedSelect(Selector selector) {
    for (Iterator iter = selector.keys().iterator(); iter.hasNext();) {
        SelectionKey key = (SelectionKey) iter.next();
        Session session = (Session) key.attachment();
        session.close();/*  ww w  .  ja  va2s. c o m*/
    }
    try {
        selector.close();
    } catch (IOException e) {
    }
    selector = null;
    lastSelectTime = null;
    register.clear();
    close = false;
    thread = null;
}

From source file:Proxy.java

void close(Selector sel, SocketChannel in_channel, SocketChannel out_channel) {
    try {/*from w w  w .  j  a va  2s.  co m*/
        if (sel != null)
            sel.close();
    } catch (Exception ex) {
    }
    try {
        if (in_channel != null)
            in_channel.close();
    } catch (Exception ex) {
    }
    try {
        if (out_channel != null)
            out_channel.close();
    } catch (Exception ex) {
    }
}

From source file:gridool.communication.transport.nio.GridNioServer.java

public void accept(@Nonnull final Selector selector) throws IOException {
    while (!closed && selector.select() > 0) {
        for (Iterator<SelectionKey> iter = selector.selectedKeys().iterator(); iter.hasNext();) {
            SelectionKey key = iter.next();
            iter.remove();//from  w w w . j a v  a2 s . c  o m

            if (!key.isValid()) { // Is key closed?
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Non valid key was detected. Key is already closed?");
                }
                continue;
            }

            if (key.isAcceptable()) {
                ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
                handleAccept(serverChannel, selector);
            } else if (key.isReadable()) {
                SocketChannel channel = (SocketChannel) key.channel();
                handleRead(channel, key, sharedReadBuf, notifier, execPool);
            }
        }
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("GridNioServer is going to be closed");
    }
    this.closed = true;
    if (selector.isOpen()) {
        for (SelectionKey key : selector.keys()) {
            NIOUtils.close(key);
        }
        selector.close();
    }
}

From source file:com.tomagoyaky.jdwp.IOUtils.java

/**
 * Closes a <code>Selector</code> unconditionally.
 * <p/>/* w w w  .j ava2  s  .c  o  m*/
 * Equivalent to {@link Selector#close()}, except any exceptions will be ignored.
 * This is typically used in finally blocks.
 * <p/>
 * Example code:
 * <pre>
 *   Selector selector = null;
 *   try {
 *       selector = Selector.open();
 *       // process socket
 *
 *   } catch (Exception e) {
 *       // error handling
 *   } finally {
 *       IOUtils.closeQuietly(selector);
 *   }
 * </pre>
 *
 * @param selector the Selector to close, may be null or already closed
 * @since 2.2
 */
public static void closeQuietly(final Selector selector) {
    if (selector != null) {
        try {
            selector.close();
        } catch (final IOException ioe) {
            // ignored
        }
    }
}