diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java index b7e8f3e..32da786 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java @@ -3,6 +3,9 @@ */ package org.gcube.accounting.persistence; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + import org.gcube.accounting.aggregation.scheduler.AggregationScheduler; import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.UsageRecord; @@ -20,13 +23,19 @@ public abstract class AccountingPersistence { protected FallbackPersistence fallback; protected AggregationScheduler aggregationScheduler; + /** + * Pool for thread execution + */ + private ExecutorService pool; + protected AccountingPersistence(){ - + this.pool = Executors.newCachedThreadPool(); } protected AccountingPersistence(FallbackPersistence fallback, AggregationScheduler aggregationScheduler){ this.fallback = fallback; this.aggregationScheduler = aggregationScheduler; + this.pool = Executors.newCachedThreadPool(); } /** @@ -117,18 +126,14 @@ public abstract class AccountingPersistence { * @throws InvalidValueException if the Record Validation Fails */ public void account(final SingleUsageRecord usageRecord) throws InvalidValueException{ - Thread thread = new Thread(){ + Runnable runnable = new Runnable(){ @Override public void run(){ validateAccountAggregate(usageRecord, true, true); } }; - thread.start(); - try { - thread.join(); - } catch (InterruptedException e) { - logger.error("Error joining Thread"); - } + pool.execute(runnable); + } public void flush() throws Exception {