From 885865ed67c24feb9535d04392d195da8ca174fd Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Wed, 09 May 2012 09:53:37 +0000
Subject: [PATCH] Wait for cancelation result before closing the connection and exiting.

---
 opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/SearchAsync.java |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/SearchAsync.java b/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/SearchAsync.java
index e0cf6fe..308a3a5 100644
--- a/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/SearchAsync.java
+++ b/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/SearchAsync.java
@@ -178,6 +178,7 @@
     }
 
     private static final CountDownLatch COMPLETION_LATCH = new CountDownLatch(1);
+    private static final CountDownLatch CANCEL_LATCH = new CountDownLatch(1);
     private static final LDIFEntryWriter WRITER = new LDIFEntryWriter(System.out);
     private static String userName;
     private static String password;
@@ -197,11 +198,13 @@
         public void handleErrorResult(final ErrorResultException error) {
             System.err.println("Cancel request failed with result code: "
                     + error.getResult().getResultCode().intValue());
+            CANCEL_LATCH.countDown();
         }
 
         @Override
         public void handleResult(final ExtendedResult result) {
             System.err.println("Cancel request succeeded");
+            CANCEL_LATCH.countDown();
         }
 
     }
@@ -270,6 +273,15 @@
             return;
         }
 
+        // Await completion of the cancel request.
+        try {
+            CANCEL_LATCH.await();
+        } catch (final InterruptedException e) {
+            System.err.println(e.getMessage());
+            System.exit(ResultCode.CLIENT_SIDE_USER_CANCELLED.intValue());
+            return;
+        }
+
         if (connection != null) {
             connection.close();
         }

--
Gitblit v1.10.0