Close the SouthboundHandler Executor Gracefully 10/9710/1
authorDave Tucker <djt@redhat.com>
Tue, 5 Aug 2014 18:45:07 +0000 (19:45 +0100)
committerDave Tucker <djt@redhat.com>
Tue, 5 Aug 2014 18:48:50 +0000 (19:48 +0100)
During the OSGi "stop" method try and close the Executor gracefully.
Stop accepting new work by calling shutdown()
Wait a given time for  the current task to terminate before issuing a
shutdownNow()

Fixes bug 281

Change-Id: I0823a3dce87e67ca3e5d18640e9ecd5d4e75b7ba
Signed-off-by: Dave Tucker <djt@redhat.com>
openstack/net-virt/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/SouthboundHandler.java

index e85d9330fbb9c40a2ca0e9e90bd2ff095c05515c..16ffc9db5900416f66a8709df4811a2d910a8ca4 100644 (file)
@@ -40,6 +40,7 @@ import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
 
 public class SouthboundHandler extends AbstractHandler implements OvsdbInventoryListener, IInventoryListener {
     static final Logger logger = LoggerFactory.getLogger(SouthboundHandler.class);
@@ -100,7 +101,22 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
     }
 
     void stop() {
-        eventHandler.shutdownNow();
+        // stop accepting new tasks
+        eventHandler.shutdown();
+        try {
+            // Wait a while for existing tasks to terminate
+            if (!eventHandler.awaitTermination(10, TimeUnit.SECONDS)) {
+                eventHandler.shutdownNow();
+                // Wait a while for tasks to respond to being cancelled
+                if (!eventHandler.awaitTermination(10, TimeUnit.SECONDS))
+                    logger.error("Southbound Event Handler did not terminate");
+            }
+        } catch (InterruptedException e) {
+            // (Re-)Cancel if current thread also interrupted
+            eventHandler.shutdownNow();
+            // Preserve interrupt status
+            Thread.currentThread().interrupt();
+        }
     }
 
     @Override