diff --git a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/AccountingClientDaemon.java b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/AccountingClientDaemon.java index 4aa531f..859f37c 100644 --- a/src/main/java/org/gcube/portlets/admin/accountingmanager/server/AccountingClientDaemon.java +++ b/src/main/java/org/gcube/portlets/admin/accountingmanager/server/AccountingClientDaemon.java @@ -1,5 +1,6 @@ package org.gcube.portlets.admin.accountingmanager.server; +import java.util.ArrayList; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutionException; @@ -31,10 +32,13 @@ public class AccountingClientDaemon implements Runnable { private ServletContextEvent sce; private volatile boolean running = true; private volatile AccountingCache accountingCache; + private ArrayList> workers; - public AccountingClientDaemon(ServletContextEvent sce, AccountingCache accountingCache) { + public AccountingClientDaemon(ServletContextEvent sce, + AccountingCache accountingCache) { this.sce = sce; - this.accountingCache=accountingCache; + this.accountingCache = accountingCache; + workers = new ArrayList<>(); } public void terminate() { @@ -55,26 +59,41 @@ public class AccountingClientDaemon implements Runnable { final TaskRequest taskRequest = jobQueue.poll(); AccountingClientCallable accountingClientCallable = new AccountingClientCallable( - taskRequest,accountingCache); + taskRequest, accountingCache); Future futureResult = executorService .submit(accountingClientCallable); - TaskStatus result = null; - try { - result = futureResult.get(Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MINUTES, - TimeUnit.MINUTES); - logger.info("AccountingClientTask: " + result); - - } catch (InterruptedException | ExecutionException e) { - logger.error( - "AccountingClientTask: " + e.getLocalizedMessage(), - e); - } catch (TimeoutException e) { - logger.error("AccountingClientTask No response after " - + Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MINUTES + " minutes!"); - futureResult.cancel(true); - } + workers.add(futureResult); } + + if (!workers.isEmpty()) { + ArrayList> dones=new ArrayList<>(); + for (Future futureResult : workers) { + if (futureResult.isDone()) { + TaskStatus result = null; + try { + result = futureResult + .get(Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MINUTES, + TimeUnit.MINUTES); + logger.info("AccountingClientTask: " + result); + + } catch (InterruptedException | ExecutionException e) { + logger.error( + "AccountingClientTask: " + + e.getLocalizedMessage(), e); + } catch (TimeoutException e) { + logger.error("AccountingClientTask No response after " + + Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MINUTES + + " minutes!"); + futureResult.cancel(true); + } + dones.add(futureResult); + } + } + workers.removeAll(dones); + + } + } } } \ No newline at end of file