/** * Asynchronous exception handling. * * @param block <code>true</code> => block until an exception is available, <code>false</code => * return <code>null</code> if no exception is available. * @return The exception, or <code>null</code> if no exception is available or this Acceptor has * been shut down. */ public Exception getPendingException(boolean block) { try { return (Exception) m_exceptionQueue.dequeue(block); } catch (ThreadSafeQueue.ShutdownException e) { return null; } }
/** * Shut down this acceptor. * * @throws CommunicationException If an IO exception occurs. */ public void shutdown() throws CommunicationException { synchronized (m_socketSets) { // Prevent recursion and creation of new ResourcePools. if (m_isShutdown) { return; } m_isShutdown = true; } try { m_serverSocket.close(); } catch (IOException e) { UncheckedInterruptedException.ioException(e); throw new CommunicationException("Error closing socket", e); } finally { // Interrupt the acceptor thread group. m_threadPool.stop(); // We clone contents of m_socketSets and don't hold m_socketSets whilst // closing the ResourcePools to remove opportunity for dead lock with // events triggered by closing resources. final ResourcePool[] socketSets = cloneListOfSocketSets(); for (int i = 0; i < socketSets.length; ++i) { socketSets[i].closeCurrentResources(); } m_exceptionQueue.shutdown(); } }
private void discriminateConnection(Socket localSocket) throws IOException, ShutdownException { boolean closeSocket = true; try { final Connector.ConnectDetails connectDetails = Connector.read(localSocket.getInputStream()); final SocketWrapper socketWrapper = new SocketWrapper(localSocket); socketWrapper.setAddress(connectDetails.getAddress()); // Possible minor race if the socket is closed between here... final ResourcePool.Closeable closeable = getSocketSet(connectDetails.getConnectionType()).add(socketWrapper); // .. and the time a listener is registered. Will pick up such a zombie // the next time we try to use the resource. socketWrapper.addClosedListener( new SocketWrapper.ClosedListener() { public void socketClosed() { closeable.close(); } }); // We did good. closeSocket = false; } catch (CommunicationException e) { try { m_exceptionQueue.queue(e); } catch (ThreadSafeQueue.ShutdownException shutdownException) { // Can happen due to race condition with shutdown, ignore. } } finally { if (closeSocket) { try { localSocket.close(); } catch (IOException ioException) { UncheckedInterruptedException.ioException(ioException); // Ignore. } } } }