/** * Handle OP_WRITE. * * @param key {@link SelectionKey} * @param ctx {@link Context} */ public boolean onWriteInterest(final SelectionKey key, final Context ctx) throws IOException { // disable OP_WRITE on key before doing anything else key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE)); if (asyncQueueWriter.isReady(key)) { invokeAsyncQueueWriter(pollContext(ctx, key, Context.OpType.OP_WRITE)); return false; } Object attach = SelectionKeyAttachment.getAttachment(key); if (attach instanceof CallbackHandler) { NIOContext context = pollContext(ctx, key, Context.OpType.OP_WRITE); invokeCallbackHandler((CallbackHandler) attach, context); return false; } return true; }
/** {@inheritDoc} */ public void closeChannel(SelectableChannel channel) { // channel could be either SocketChannel or ServerSocketChannel if (channel instanceof SocketChannel) { Socket socket = ((SocketChannel) channel).socket(); try { if (!socket.isInputShutdown()) socket.shutdownInput(); } catch (IOException e) { logger.log(Level.FINEST, "Unexpected exception during channel inputShutdown", e); } try { if (!socket.isOutputShutdown()) socket.shutdownOutput(); } catch (IOException e) { logger.log(Level.FINEST, "Unexpected exception during channel outputShutdown", e); } try { socket.close(); } catch (IOException e) { logger.log(Level.FINEST, "Unexpected exception during socket close", e); } } try { channel.close(); } catch (IOException e) { logger.log(Level.FINEST, "Unexpected exception during channel close", e); } if (asyncQueueReader != null) { asyncQueueReader.onClose(channel); } if (asyncQueueWriter != null) { asyncQueueWriter.onClose(channel); } }
/** Shuntdown this instance by closing its Selector and associated channels. */ public void shutdown() { // If shutdown was called for this SelectorHandler if (isShutDown.getAndSet(true)) return; stateHolder.setState(State.STOPPED); if (selector != null) { try { boolean isContinue = true; while (isContinue) { try { for (SelectionKey selectionKey : selector.keys()) { selectionKeyHandler.close(selectionKey); } isContinue = false; } catch (ConcurrentModificationException e) { // ignore } } } catch (ClosedSelectorException e) { // If Selector is already closed - OK } } try { if (serverSocket != null) { serverSocket.close(); serverSocket = null; } } catch (Throwable ex) { Controller.logger().log(Level.SEVERE, "serverSocket.close", ex); } try { if (serverSocketChannel != null) { serverSocketChannel.close(); serverSocketChannel = null; } } catch (Throwable ex) { Controller.logger().log(Level.SEVERE, "serverSocketChannel.close", ex); } try { if (selector != null) selector.close(); } catch (Throwable ex) { Controller.logger().log(Level.SEVERE, "selector.close", ex); } if (asyncQueueReader != null) { asyncQueueReader.close(); asyncQueueReader = null; } if (asyncQueueWriter != null) { asyncQueueWriter.close(); asyncQueueWriter = null; } selectorHandlerTasks.clear(); attributes = null; }