add propper async mass import
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public class AdminController {
|
||||
|
||||
@PostMapping("/trigger-import")
|
||||
public ResponseEntity<String> triggerMassImport() {
|
||||
new Thread(massImportService::runImport).start();
|
||||
massImportService.runImportAsync();
|
||||
return ResponseEntity.ok("Massenimport gestartet.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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<File> fileOnDisk = findFileRecursive(filename);
|
||||
@@ -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 "";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user