From c82614f968fb7941f7778873afa3bfad51ffd42f Mon Sep 17 00:00:00 2001 From: Lianhao Lu Date: Thu, 11 Apr 2019 10:26:45 +0800 Subject: Gracefully release resource in file reception plugin 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 --- .../handling/file/FileSystemReceptionHandler.java | 53 ++++++++++++---------- 1 file changed, 28 insertions(+), 25 deletions(-) (limited to 'plugins/reception-plugins/src/main/java/org') diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/file/FileSystemReceptionHandler.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/file/FileSystemReceptionHandler.java index 0ddd2a92..56bca900 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/file/FileSystemReceptionHandler.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/file/FileSystemReceptionHandler.java @@ -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 ev = (WatchEvent) 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 ev = (WatchEvent) 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(); } /** -- cgit 1.2.3-korg