diff --git a/workspaces/backend/src/main/java/org/raddatz/familienarchiv/config/AsyncConfig.java b/workspaces/backend/src/main/java/org/raddatz/familienarchiv/config/AsyncConfig.java new file mode 100644 index 00000000..db23dca9 --- /dev/null +++ b/workspaces/backend/src/main/java/org/raddatz/familienarchiv/config/AsyncConfig.java @@ -0,0 +1,24 @@ +package org.raddatz.familienarchiv.config; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +@Configuration +@EnableAsync +public class AsyncConfig { + @Bean + public Executor taskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(1); + executor.setMaxPoolSize(1); + executor.setQueueCapacity(1); + executor.setThreadNamePrefix("Import-"); + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); + return executor; + } +} \ No newline at end of file diff --git a/workspaces/backend/src/main/java/org/raddatz/familienarchiv/controller/AdminController.java b/workspaces/backend/src/main/java/org/raddatz/familienarchiv/controller/AdminController.java index 65800c89..57d0c856 100644 --- a/workspaces/backend/src/main/java/org/raddatz/familienarchiv/controller/AdminController.java +++ b/workspaces/backend/src/main/java/org/raddatz/familienarchiv/controller/AdminController.java @@ -20,7 +20,7 @@ public class AdminController { @PostMapping("/trigger-import") public ResponseEntity triggerMassImport() { - new Thread(massImportService::runImport).start(); + massImportService.runImportAsync(); return ResponseEntity.ok("Massenimport gestartet."); } } diff --git a/workspaces/backend/src/main/java/org/raddatz/familienarchiv/service/MassImportService.java b/workspaces/backend/src/main/java/org/raddatz/familienarchiv/service/MassImportService.java index 92f61594..19fc5656 100644 --- a/workspaces/backend/src/main/java/org/raddatz/familienarchiv/service/MassImportService.java +++ b/workspaces/backend/src/main/java/org/raddatz/familienarchiv/service/MassImportService.java @@ -8,6 +8,7 @@ import org.raddatz.familienarchiv.model.Document; import org.raddatz.familienarchiv.model.DocumentStatus; import org.raddatz.familienarchiv.repository.DocumentRepository; import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import software.amazon.awssdk.core.sync.RequestBody; @@ -38,13 +39,22 @@ public class MassImportService { private String bucketName; // Konfiguration der Spalten (wie im ExcelService) - @Value("${app.import.excel.col.filename:0}") private int colFilename; - @Value("${app.import.excel.col.date:1}") private int colDate; - @Value("${app.import.excel.col.location:2}") private int colLocation; - @Value("${app.import.excel.col.transcription:3}") private int colTranscription; + @Value("${app.import.excel.col.filename:0}") + private int colFilename; + @Value("${app.import.excel.col.date:1}") + private int colDate; + @Value("${app.import.excel.col.location:2}") + private int colLocation; + @Value("${app.import.excel.col.transcription:3}") + private int colTranscription; private static final String IMPORT_DIR = "/import"; + @Async + public void runImportAsync() { + runImport(); + } + public String runImport() { try { // 1. Excel finden @@ -74,7 +84,7 @@ public class MassImportService { private int processExcel(File excelFile) throws IOException { int count = 0; try (FileInputStream fis = new FileInputStream(excelFile); - Workbook workbook = new XSSFWorkbook(fis)) { + Workbook workbook = new XSSFWorkbook(fis)) { Sheet sheet = workbook.getSheetAt(0); @@ -83,10 +93,12 @@ public class MassImportService { for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); - if (row == null) continue; + if (row == null) + continue; String filename = getCellValue(row.getCell(colFilename)); - if (filename == null || filename.isBlank()) continue; + if (filename == null || filename.isBlank()) + continue; // Datei auf der Festplatte suchen Optional fileOnDisk = findFileRecursive(filename); @@ -124,9 +136,9 @@ public class MassImportService { String s3Key = "documents/" + UUID.randomUUID() + "_" + file.getName(); try { s3Client.putObject(PutObjectRequest.builder() - .bucket(bucketName) - .key(s3Key) - .build(), + .bucket(bucketName) + .key(s3Key) + .build(), RequestBody.fromFile(file)); } catch (Exception e) { log.error("S3 Upload Fehler für " + file.getName(), e); @@ -162,9 +174,12 @@ public class MassImportService { } private String getCellValue(Cell cell) { - if (cell == null) return null; - if (cell.getCellType() == CellType.STRING) return cell.getStringCellValue(); - if (cell.getCellType() == CellType.NUMERIC) return String.valueOf((int)cell.getNumericCellValue()); + if (cell == null) + return null; + if (cell.getCellType() == CellType.STRING) + return cell.getStringCellValue(); + if (cell.getCellType() == CellType.NUMERIC) + return String.valueOf((int) cell.getNumericCellValue()); return ""; } }