private synchronized void connect() { if (!connecting) { // We defer actual connection until the first part of body is written or end is called // This gives the user an opportunity to set an exception handler before connecting so // they can capture any exceptions on connection client.getConnection( port, host, conn -> { synchronized (this) { if (exceptionOccurred) { // The request already timed out before it has left the pool waiter queue // So return it conn.close(); } else if (!conn.isClosed()) { connected(conn); } else { // The connection has been closed - closed connections can be in the pool // Get another connection - Note that we DO NOT call connectionClosed() on the pool // at this point // that is done asynchronously in the connection closeHandler() connect(); } } }, exceptionHandler, vertx.getOrCreateContext()); connecting = true; } }
@Override public void run() { try { ClientConnection cc = null; while (true) { // Accept client connections Socket socket = serverSocket.accept(); // Clean up old connection if (oldSocket != null) { try { oldSocket.close(); } catch (IOException e) { // Ignore } } if (cc != null) { // Close old worker cc.close(); } oldSocket = socket; cc = new ClientConnection(handler, privateKey, socket); cc.start(); } } catch (Exception e) { e.printStackTrace(); } gui.exit(); }
@Override public void close() { checkClosed(); pool.close(); for (ClientConnection conn : connectionMap.values()) { conn.close(); } actualCtx.removeCloseHook(closeHook); closed = true; }