Rimpiazzato utilizzo di LooperService con @Scheduler
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good

This commit is contained in:
2025-05-21 18:47:43 +02:00
parent c490661c1d
commit 2165732206
7 changed files with 53 additions and 84 deletions

View File

@@ -1,6 +1,5 @@
package it.integry.ems.download;
import it.integry.annotations.PostContextConstruct;
import it.integry.ems.download.dto.CachedFileDto;
import it.integry.ems.download.dto.DownloadFileDto;
import it.integry.ems.looper.service.LooperService;
@@ -9,6 +8,7 @@ import it.integry.ems.utility.UtilityFile;
import it.integry.ems_model.entity.StbFilesAttached;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.File;
@@ -16,6 +16,7 @@ import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Service
public class DownloadFileHandlerService {
@@ -25,13 +26,12 @@ public class DownloadFileHandlerService {
private final HashMap<String, CachedFileDto> mFileMap = new HashMap<>();
@PostContextConstruct
public void init() {
looperService.add(() -> {
UtilityFile.cleanDirectory(getTempPath(), 1, "");
}, 60 * 60 * 1000, DownloadFileHandlerService.class.getName());
@Scheduled(fixedDelay = 1, timeUnit = TimeUnit.HOURS, zone = "Europe/Rome")
public void clean() {
UtilityFile.cleanDirectory(getTempPath(), 1, "");
}
public DownloadFileDto generateDownloadItem(File file) throws IOException {
return generateDownloadItem(file.getName(), FileUtils.readFileToByteArray(file), false);
}

View File

@@ -23,12 +23,14 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Service
@@ -59,20 +61,13 @@ public class EntityLoggerNewService {
return;
initSetup();
looperService.add(() -> {
try {
consumeQueue();
} catch (Exception e) {
logger.error("EntityLoggerNewService", e);
}
}, 10 * 1000, "LOG_" + EntityLoggerNewService.class.getName());
}
private void initSetup() {
List<String> dbNames = settingsModel.getAvailableConnections(true).stream()
List<String> dbNames = settingsModel.getAvailableConnectionsWithoutDuplicatedProfiles(true)
.stream()
.map(AvailableConnectionsModel::getDbName)
.distinct()
.collect(Collectors.toList());
.collect(Collectors.toList());;
dynamicCacheService.addEntity(DynamicCacheService.Keys.ENTITY_LOGGER_SETUP, 5, StbLogEntitySetup.class, dbNames);
}
@@ -106,7 +101,11 @@ public class EntityLoggerNewService {
return logEntitySetup.stream().anyMatch(x -> x.getEntityName().equalsIgnoreCase(entity.getTableName()));
}
@Scheduled(fixedDelay = 10, timeUnit = TimeUnit.SECONDS, zone = "Europe/Rome")
private synchronized void consumeQueue() throws Exception {
if (UtilityDebug.isDebugExecution())
return;
synchronized (queue) {
if (!queue.isEmpty()) {
MultiDBTransactionManager multiDBTransactionManager = new MultiDBTransactionManager(settingsController.getHistoryProfileDb());

View File

@@ -2,7 +2,6 @@ package it.integry.ems.sync;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
import it.integry.annotations.PostContextConstruct;
import it.integry.ems.datasource.DataSource;
import it.integry.ems.json.ResponseJSONObjectMapper;
import it.integry.ems.looper.service.LooperService;
@@ -24,6 +23,7 @@ import it.integry.ems_model.utility.UtilityLocalDate;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
@@ -33,6 +33,7 @@ import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
@@ -56,13 +57,6 @@ public class AsyncHistoryManager {
//Implement check list like distribuzione
private final AtomicReference<List<ExportHistoryGroupDTO>> currentlyInExecutionG = new AtomicReference<>(new ArrayList<>());
@PostContextConstruct(priority = 20)
public void init() {
this.looperService.add(this::consumeRetrieveQueue, 10 * 1000, "async-history-read");
this.looperService.add(this::consumeProcessedQueue, 2 * 1000, "async-history-save");
}
public void addToExportQueue(DataSource dataSource, long groupId, PublicationDTO publication) throws Exception {
tryAddInExecutionList(groupId, dataSource, new ExportHistoryItemDTO()
@@ -95,7 +89,7 @@ public class AsyncHistoryManager {
list.add(exportHistoryItemDTO);
}
@Scheduled(fixedDelay = 10, timeUnit = TimeUnit.SECONDS, zone = "Europe/Rome", initialDelay = 60)
private void consumeRetrieveQueue() {
for (ExportHistoryGroupDTO currentGroup : currentlyInExecutionG.get()) {
@@ -193,7 +187,7 @@ public class AsyncHistoryManager {
});
}
@Scheduled(fixedDelay = 2, timeUnit = TimeUnit.SECONDS, zone = "Europe/Rome", initialDelay = 60)
private void consumeProcessedQueue() {
List<ExportHistoryGroupDTO> list = currentlyInExecutionG.get();
for (int i = 0; i < list.size(); i++) {

View File

@@ -1,7 +1,6 @@
package it.integry.ems.sync;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
import it.integry.annotations.PostContextConstruct;
import it.integry.ems.json.ResponseJSONObjectMapper;
import it.integry.ems.looper.service.LooperService;
import it.integry.ems.settings.Model.AvailableConnectionsModel;
@@ -24,11 +23,13 @@ import org.apache.logging.log4j.Logger;
import org.josql.Query;
import org.josql.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Component
@@ -49,15 +50,11 @@ public class AsyncManager {
private final static ConcurrentLinkedQueue<Map.Entry<String, StbTransactionLog>> toBeSavedQueue = new ConcurrentLinkedQueue<>();
@PostContextConstruct
public void init() {
if ((!UtilityDebug.isDebugExecution() && !UtilityDebug.isIntegryServer())) {
looperService.add(this::internalCachePublicationsSetup, 5 * 60 * 1000, "sync-setup-cache");
looperService.add(this::consumeToBeSavedQueue, 20 * 1000, "sync-flush-data");
}
}
@Scheduled(fixedDelay = 5, timeUnit = TimeUnit.MINUTES, zone = "Europe/Rome")
private void internalCachePublicationsSetup() {
if(UtilityDebug.isDebugExecution() || UtilityDebug.isIntegryServer())
return;
String historyProfileDb = null;
try {
historyProfileDb = settingsController.getHistoryProfileDb();
@@ -66,31 +63,26 @@ public class AsyncManager {
}
String finalHistoryProfileDb = historyProfileDb;
Map<String, List<AvailableConnectionsModel>> databases = settingsModel.getAvailableConnections()
.stream()
.filter(AvailableConnectionsModel::getInternalDb)
.filter(x -> finalHistoryProfileDb != null && !finalHistoryProfileDb.equalsIgnoreCase(x.getProfileName()))
.collect(Collectors.groupingBy(AvailableConnectionsModel::getDbName));
List<AvailableConnectionsModel> databases = settingsModel.getAvailableConnectionsWithoutDuplicatedProfiles(true);
cachedSetup.clear();
for (String dbName : databases.keySet()) {
for (AvailableConnectionsModel connectionModel : databases) {
try {
String profileName = databases.get(dbName).get(0).getProfileName();
cacheSetup(dbName, profileName);
cacheSetup(connectionModel);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
}
}
private void cacheSetup(String dbName, String profileName) throws Exception {
private void cacheSetup(AvailableConnectionsModel connectionsModel) throws Exception {
try (MultiDBTransactionManager multiDBTransactionManager = new MultiDBTransactionManager(profileName, false)) {
try (MultiDBTransactionManager multiDBTransactionManager = new MultiDBTransactionManager(connectionsModel, false)) {
String sql = "SELECT * FROM " + StbPublicationsDetail.ENTITY + " WHERE active = 1";
List<StbPublicationsDetail> publications = UtilityDB.executeSimpleQueryDTO(multiDBTransactionManager.getPrimaryConnection(), sql, StbPublicationsDetail.class);
cachedSetup.putIfAbsent(dbName, publications);
cachedSetup.putIfAbsent(connectionsModel.getDbName(), publications);
}
}
@@ -166,8 +158,11 @@ public class AsyncManager {
return stbPublication.getLastGroupId();
}
@Scheduled(fixedDelay = 20, timeUnit = TimeUnit.SECONDS, zone = "Europe/Rome")
private void consumeToBeSavedQueue() {
if(UtilityDebug.isDebugExecution() || UtilityDebug.isIntegryServer())
return;
if (cachedSetup.entrySet().stream().anyMatch(x -> x.getValue() != null && x.getValue().stream().anyMatch(y -> !y.isReadyToTransmit())))
return;

View File

@@ -1,17 +1,14 @@
package it.integry.firebase.service;
import com.annimon.stream.Stream;
import it.integry.annotations.PostContextConstruct;
import it.integry.common.var.CommonConstants;
import it.integry.ems.json.ResponseJSONObjectMapper;
import it.integry.ems.looper.service.LooperService;
import it.integry.ems.response.EsitoType;
import it.integry.ems.response.ServiceRestResponse;
import it.integry.ems.service.HttpRestWrapper;
import it.integry.ems.settings.Model.AvailableConnectionsModel;
import it.integry.ems.settings.Model.SettingsModel;
import it.integry.ems.sync.MultiDBTransaction.MultiDBTransactionManager;
import it.integry.ems.utility.UtilityDebug;
import it.integry.ems_model.entity.WtbNotification;
import it.integry.ems_model.utility.UtilityDB;
import it.integry.ems_model.utility.UtilityDate;
@@ -20,12 +17,14 @@ import it.integry.ems_model.utility.UtilityServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
public class NotificationSenderComponent {
@@ -34,18 +33,11 @@ public class NotificationSenderComponent {
@Autowired
private SettingsModel settingsModel;
@Autowired
private LooperService looperService;
@Autowired
private ResponseJSONObjectMapper responseJSONObjectMapper;
@PostContextConstruct(priority = 11)
private void init() {
if (!UtilityDebug.isDebugExecution())
looperService.add(this::sendNotifications, 15 * 60 * 1000, NotificationSenderComponent.class.getName());
}
@Scheduled(fixedDelay = 15, timeUnit = TimeUnit.MINUTES, zone = "Europe/Rome", initialDelay = 1)
private void sendNotifications() {
MultiDBTransactionManager multiDBTransactionManager = null;

View File

@@ -39,7 +39,8 @@ public class LogController {
ServiceRestResponse deleteLogs(HttpServletRequest request,
@RequestParam(defaultValue = "30") Integer days) {
try {
return ServiceRestResponse.createPositiveResponse(logService.deleteLogs(days));
logService.deleteLogs(days);
return ServiceRestResponse.createPositiveResponse();
} catch (Exception e) {
logger.error(request.getRequestURI(), e);
return ServiceRestResponse.createNegativeResponse(e);

View File

@@ -4,28 +4,27 @@ import com.annimon.stream.Collectors;
import com.annimon.stream.Stream;
import com.google.common.base.CaseFormat;
import com.google.common.collect.ImmutableList;
import it.integry.annotations.PostContextConstruct;
import it.integry.core.log.dto.*;
import it.integry.core.log.enums.FilterMatchMode;
import it.integry.ems.looper.service.LooperService;
import it.integry.ems.settings.Model.SettingsModel;
import it.integry.ems.sync.MultiDBTransaction.Connection;
import it.integry.ems.utility.UtilityDirs;
import it.integry.ems_model.utility.UtilityDB;
import it.integry.ems_model.utility.UtilityString;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.File;
import java.math.BigDecimal;
import it.integry.ems.sync.MultiDBTransaction.Connection;
import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Service
public class LogService {
@@ -40,24 +39,6 @@ public class LogService {
private final ImmutableList<String> globalFilterFields = ImmutableList.of("logger", "message", "exception");
@PostContextConstruct
public void init() {
looperService.add(() -> {
try {
deleteLogs(settingsModel.getLoggerConfiguration().getDbDeleteDays());
} catch (Exception exception) {
logger.error(exception);
}
}, 24 * 60 * 60 * 1000, LogService.class.getName());
looperService.add(() -> {
try {
checkLogSize(settingsModel.getLoggerConfiguration().getDbMaxSize());
} catch (Exception exception) {
logger.error(exception);
}
}, 5 * 60 * 1000, LogService.class.getName());
}
public List<LogDTO> retrieveLogs(Integer page, Integer pageSize, LogDataDTO logData) throws Exception {
if (pageSize == null) {
pageSize = 100;
@@ -251,7 +232,13 @@ public class LogService {
}
}
public int deleteLogs(Integer days) throws Exception {
@Scheduled(fixedDelay = 1, timeUnit = TimeUnit.DAYS, zone = "Europe/Rome")
public void deleteLogs() throws Exception {
Integer days = settingsModel.getLoggerConfiguration().getDbDeleteDays();
deleteLogs(days);
}
public void deleteLogs(Integer days) throws Exception {
String sql = String.format("DELETE\n" +
"FROM app_logs\n" +
"WHERE entry_date < UNIXEPOCH(DATETIME('now', '%s day')) * 1000", -1 * days);
@@ -260,11 +247,12 @@ public class LogService {
PreparedStatement ps = connection.prepareStatement(sql)) {
int deletedRows = ps.executeUpdate();
logger.trace(String.format("Rimossi %d record dal log_db per obsolescenza", deletedRows));
return deletedRows;
}
}
public void checkLogSize(String maxSize) throws Exception {
@Scheduled(fixedDelay = 5, timeUnit = TimeUnit.MINUTES, zone = "Europe/Rome")
public void checkLogSize() throws Exception {
String maxSize = settingsModel.getLoggerConfiguration().getDbMaxSize();
BigDecimal numericValue = BigDecimal.ONE;
String um = "G";
if (!UtilityString.isNullOrEmpty(maxSize)) {