Gracefully release resource in file reception plugin 86/84986/1
authorLianhao Lu <lianhao.lu@intel.com>
Thu, 11 Apr 2019 02:26:45 +0000 (10:26 +0800)
committerLianhao Lu <lianhao.lu@intel.com>
Thu, 11 Apr 2019 02:26:45 +0000 (10:26 +0800)
In the FileSystemReception plugin, we need to shutdown the thread pool
to release the resources gracefully.

Change-Id: I800e4070d7bf8c052d964139117a68dc48c50f76
Issue-ID: POLICY-1631
Signed-off-by: Lianhao Lu <lianhao.lu@intel.com>
plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/file/FileSystemReceptionHandler.java

index 0ddd2a9..56bca90 100644 (file)
@@ -120,35 +120,38 @@ public class FileSystemReceptionHandler extends AbstractReceptionHandler {
         WatchKey key;
         ExecutorService pool = Executors.newFixedThreadPool(maxThread);
 
-        running = true;
-        while (running) {
-            key = watcher.take();
+        try {
+            running = true;
+            while (running) {
+                key = watcher.take();
 
-            for (final WatchEvent<?> event : key.pollEvents()) {
-                final WatchEvent<Path> ev = (WatchEvent<Path>) event;
-                final Path fileName = ev.context();
-                pool.execute(new Runnable() {
-                    public void run() {
-                        LOGGER.debug("new CSAR found: {}", fileName);
-                        DistributionStatisticsManager.updateTotalDistributionCount();
-                        final String fullFilePath = dir.toString() + File.separator + fileName.toString();
-                        try {
-                            waitForFileToBeReady(fullFilePath);
-                            createPolicyInputAndCallHandler(fullFilePath);
-                            LOGGER.debug("CSAR complete: {}", fileName);
-                        } catch (InterruptedException e) {
-                            LOGGER.error("waitForFileToBeReady interrupted", e);
+                for (final WatchEvent<?> event : key.pollEvents()) {
+                    final WatchEvent<Path> ev = (WatchEvent<Path>) event;
+                    final Path fileName = ev.context();
+                    pool.execute(new Runnable() {
+                        public void run() {
+                            LOGGER.debug("new CSAR found: {}", fileName);
+                            DistributionStatisticsManager.updateTotalDistributionCount();
+                            final String fullFilePath = dir.toString() + File.separator + fileName.toString();
+                            try {
+                                waitForFileToBeReady(fullFilePath);
+                                createPolicyInputAndCallHandler(fullFilePath);
+                                LOGGER.debug("CSAR complete: {}", fileName);
+                            } catch (InterruptedException e) {
+                                LOGGER.error("waitForFileToBeReady interrupted", e);
+                            }
                         }
-                    }
-                });
-            }
-            final boolean valid = key.reset();
-            if (!valid) {
-                LOGGER.error("Watch key no longer valid!");
-                break;
+                    });
+                }
+                final boolean valid = key.reset();
+                if (!valid) {
+                    LOGGER.error("Watch key no longer valid!");
+                    break;
+                }
             }
+        } finally {
+            pool.shutdown();
         }
-        pool.shutdown();
     }
 
     /**