Skip to content

Commit

Permalink
Run data insert on ApplicationReadyEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
gmitaros committed Oct 6, 2024
1 parent 0222b92 commit 49751bb
Showing 1 changed file with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import com.gmitaros.vesselmetrics.service.ValidationService;
import com.gmitaros.vesselmetrics.service.VesselDataBatchService;
import com.gmitaros.vesselmetrics.util.Utils;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -56,28 +57,26 @@ public class CsvParserService implements DataParser {
private final MetricsCalculationService metricsCalculationService;

/**
* Initialization method that triggers the parsing of the CSV file and outlier detection.
* Called once the service is constructed.
* Listener for when the application is fully initialized and ready.
* It will trigger CSV parsing and data loading if required.
*/
@PostConstruct
@EventListener(ApplicationReadyEvent.class)
@Transactional
public void init() {
synchronized (this) {
long vesselData = vesselDataRepository.count();
log.info("Found {} vessel data in vessel_data db ", vesselData);
boolean loadData = vesselData <= 0 || loadCsvIfAlreadyHaveData;
if (loadData) {
log.info("CsvParserService will load data from {} file", vesselDataPath);
try (InputStream inputStream = getClass().getResourceAsStream(vesselDataPath)) {
parseAndSave(inputStream);
checkForOutliers();
} catch (Exception e) {
log.error("Error initializing data: ", e);
throw new RuntimeException("Failed to initialize data", e);
}
} else {
log.info("Skipping loading again vessel data from CSV file");
public void onApplicationReady() {
long vesselData = vesselDataRepository.count();
log.info("Found {} vessel data in vessel_data db ", vesselData);
boolean loadData = vesselData <= 0 || loadCsvIfAlreadyHaveData;
if (loadData) {
log.info("CsvParserService will load data from {} file", vesselDataPath);
try (InputStream inputStream = getClass().getResourceAsStream(vesselDataPath)) {
parseAndSave(inputStream);
checkForOutliers();
} catch (Exception e) {
log.error("Error initializing data: ", e);
throw new RuntimeException("Failed to initialize data", e);
}
} else {
log.info("Skipping loading again vessel data from CSV file");
}
}

Expand Down

0 comments on commit 49751bb

Please sign in to comment.