Sistemata EntityCacheComponent in debug
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good
This commit is contained in:
@@ -10,6 +10,7 @@ import it.integry.ems.dto.EntityHierarchy;
|
||||
import it.integry.ems.migration._base.IntegryCustomerDB;
|
||||
import it.integry.ems.sync.MultiDBTransaction.Connection;
|
||||
import it.integry.ems.sync.MultiDBTransaction.MultiDBTransactionManager;
|
||||
import it.integry.ems.utility.UtilityDebug;
|
||||
import it.integry.ems_model.base.EntityBase;
|
||||
import it.integry.ems_model.base.EntityPropertyHolder;
|
||||
import it.integry.ems_model.entity.MtbAart;
|
||||
@@ -60,7 +61,9 @@ public class EntityCacheComponent implements ApplicationListener {
|
||||
private MultiDBTransactionManager multiDBTransactionManager;
|
||||
|
||||
@PostContextConstruct
|
||||
public void init() throws SQLException, DataConverterNotFoundException, InstantiationException, IllegalAccessException {
|
||||
public void init() throws Exception {
|
||||
if (UtilityDebug.isDebugExecution())
|
||||
return;
|
||||
|
||||
for (Connection conn : multiDBTransactionManager.getActiveConnections()) {
|
||||
IntegryCustomerDB customerDB = IntegryCustomerDB.parse(conn.getDbName());
|
||||
@@ -68,13 +71,31 @@ public class EntityCacheComponent implements ApplicationListener {
|
||||
if (customerDB == null)
|
||||
throw new RuntimeException("Impossibile inizializzare IntegryCustomerDB per il database: " + conn.getDbName());
|
||||
|
||||
entityCache.putIfAbsent(customerDB, new ConcurrentHashMap<>());
|
||||
|
||||
for (Map.Entry<String, Class<? extends EntityBase>> enabledEntity : enabledEntities.entrySet()) {
|
||||
String tableName = enabledEntity.getKey();
|
||||
Class<? extends EntityBase> clazz = enabledEntity.getValue();
|
||||
|
||||
ConcurrentHashMap<HashMap<String, Object>, EntityBase> entities = retrieveEntityList(conn, tableName, clazz);
|
||||
refreshCacheForEntity(conn, customerDB, tableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void refreshCacheForEntity(IntegryCustomerDB customerDB, String tableName) throws Exception {
|
||||
try (MultiDBTransactionManager mdb = new MultiDBTransactionManager(customerDB)) {
|
||||
Connection conn = mdb.getPrimaryConnection();
|
||||
refreshCacheForEntity(conn, customerDB, tableName);
|
||||
}
|
||||
}
|
||||
|
||||
private void refreshCacheForEntity(Connection connection, IntegryCustomerDB customerDB, String tableName) throws Exception {
|
||||
Class<? extends EntityBase> clazz = enabledEntities.get(tableName);
|
||||
// if (clazz == null)
|
||||
// throw new RuntimeException("Entity cache is not enabled for table " + tableName);
|
||||
|
||||
entityCache.putIfAbsent(customerDB, new ConcurrentHashMap<>());
|
||||
entityCache.get(customerDB).remove(tableName);
|
||||
|
||||
ConcurrentHashMap<HashMap<String, Object>, EntityBase> entities = retrieveEntityList(connection, tableName, clazz);
|
||||
entityCache.get(customerDB).put(tableName, entities);
|
||||
|
||||
logger.trace(String.format("[%s] Cached %d records for entity %s",
|
||||
@@ -82,11 +103,16 @@ public class EntityCacheComponent implements ApplicationListener {
|
||||
entities.size(),
|
||||
clazz.getSimpleName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public <T extends EntityBase> List<T> getCachedEntitiesList(IntegryCustomerDB customerDB, String tableName, Predicate<T> filterPredicate) {
|
||||
if (!isCacheEnabled(customerDB, tableName) && UtilityDebug.isDebugExecution()) {
|
||||
try {
|
||||
refreshCacheForEntity(customerDB, tableName);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
List<T> entities = entityCache.get(customerDB).get(tableName)
|
||||
.values()
|
||||
.parallelStream()
|
||||
@@ -114,6 +140,9 @@ public class EntityCacheComponent implements ApplicationListener {
|
||||
|
||||
|
||||
private void handleRecordInserted(TableRecordInsertedEvent event) {
|
||||
if (!isCacheEnabled(event.getCustomerDB(), event.getTableName()))
|
||||
return;
|
||||
|
||||
HashMap<String, Object> entityPrimaryKey = convertSqlMapToEntityMap(event.getPrimaryKey(), enabledEntities.get(event.getTableName()));
|
||||
|
||||
try (MultiDBTransactionManager mdb = new MultiDBTransactionManager(event.getCustomerDB())) {
|
||||
@@ -131,6 +160,9 @@ public class EntityCacheComponent implements ApplicationListener {
|
||||
}
|
||||
|
||||
private void handleRecordUpdated(TableRecordUpdatedEvent event) {
|
||||
if (!isCacheEnabled(event.getCustomerDB(), event.getTableName()))
|
||||
return;
|
||||
|
||||
HashMap<String, Object> entityPrimaryKey = convertSqlMapToEntityMap(event.getPrimaryKey(), enabledEntities.get(event.getTableName()));
|
||||
|
||||
try (MultiDBTransactionManager mdb = new MultiDBTransactionManager(event.getCustomerDB())) {
|
||||
@@ -148,6 +180,9 @@ public class EntityCacheComponent implements ApplicationListener {
|
||||
}
|
||||
|
||||
private void handleRecordDeleted(TableRecordDeletedEvent event) {
|
||||
if (!isCacheEnabled(event.getCustomerDB(), event.getTableName()))
|
||||
return;
|
||||
|
||||
HashMap<String, Object> entityPrimaryKey = convertSqlMapToEntityMap(event.getPrimaryKey(), enabledEntities.get(event.getTableName()));
|
||||
|
||||
final EntityBase removedItem = entityCache.get(event.getCustomerDB())
|
||||
@@ -200,4 +235,9 @@ public class EntityCacheComponent implements ApplicationListener {
|
||||
return entityMap;
|
||||
}
|
||||
|
||||
|
||||
private boolean isCacheEnabled(IntegryCustomerDB customerDB, String tableName) {
|
||||
return entityCache.containsKey(customerDB) && entityCache.get(customerDB).containsKey(tableName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user