From b93b0473f89d640296c64c36d3477689bd6a29b8 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Tue, 24 Nov 2015 14:03:13 +0000
Subject: [PATCH] OPENDJ-1607 Cancel connection timeout future on completion

---
 opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java           |    8 +-------
 opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java |   11 ++++++++++-
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java b/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
index 29c8931..27ea81c 100644
--- a/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
+++ b/opendj-sdk/opendj-core/src/main/java/com/forgerock/opendj/util/StaticUtils.java
@@ -41,7 +41,6 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ThreadFactory;
-import java.util.concurrent.TimeUnit;
 
 import org.forgerock.i18n.LocalizableException;
 import org.forgerock.i18n.LocalizableMessage;
@@ -119,12 +118,7 @@
 
                 @Override
                 protected void destroyInstance(ScheduledExecutorService instance) {
-                    instance.shutdown();
-                    try {
-                        instance.awaitTermination(5, TimeUnit.SECONDS);
-                    } catch (InterruptedException e) {
-                        Thread.currentThread().interrupt();
-                    }
+                    instance.shutdownNow();
                 }
             };
 
diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java
index d8ddbcc..5ddc352 100644
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java
@@ -453,8 +453,9 @@
         // Register the connect timeout timer.
         final PromiseImpl<Connection, LdapException> promise = PromiseImpl.create();
         final AtomicReference<LDAPConnectionImpl> connectionHolder = new AtomicReference<>();
+        final ScheduledFuture<?> timeoutFuture;
         if (connectTimeoutMS > 0) {
-            scheduler.get().schedule(new Runnable() {
+            timeoutFuture = scheduler.get().schedule(new Runnable() {
                 @Override
                 public void run() {
                     if (promise.tryHandleException(newConnectTimeoutError())) {
@@ -463,6 +464,8 @@
                     }
                 }
             }, connectTimeoutMS, TimeUnit.MILLISECONDS);
+        } else {
+            timeoutFuture = null;
         }
 
         // Now connect, negotiate SSL, etc.
@@ -482,6 +485,9 @@
             .thenOnResult(new ResultHandler<Result>() {
                 @Override
                 public void handleResult(Result result) {
+                    if (timeoutFuture != null) {
+                        timeoutFuture.cancel(false);
+                    }
                     final LDAPConnectionImpl connection = connectionHolder.get();
                     final ConnectionImpl connectionImpl = new ConnectionImpl(connection);
                     if (!promise.tryHandleResult(registerConnection(connectionImpl))) {
@@ -492,6 +498,9 @@
             .thenOnException(new ExceptionHandler<LdapException>() {
                 @Override
                 public void handleException(final LdapException e) {
+                    if (timeoutFuture != null) {
+                        timeoutFuture.cancel(false);
+                    }
                     final LdapException connectException;
                     if (e instanceof ConnectionException || e instanceof AuthenticationException) {
                         connectException = e;

--
Gitblit v1.10.0