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")
|
@PostMapping("/trigger-import")
|
||||||
public ResponseEntity<String> triggerMassImport() {
|
public ResponseEntity<String> triggerMassImport() {
|
||||||
new Thread(massImportService::runImport).start();
|
massImportService.runImportAsync();
|
||||||
return ResponseEntity.ok("Massenimport gestartet.");
|
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.model.DocumentStatus;
|
||||||
import org.raddatz.familienarchiv.repository.DocumentRepository;
|
import org.raddatz.familienarchiv.repository.DocumentRepository;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import software.amazon.awssdk.core.sync.RequestBody;
|
import software.amazon.awssdk.core.sync.RequestBody;
|
||||||
@@ -38,13 +39,22 @@ public class MassImportService {
|
|||||||
private String bucketName;
|
private String bucketName;
|
||||||
|
|
||||||
// Konfiguration der Spalten (wie im ExcelService)
|
// Konfiguration der Spalten (wie im ExcelService)
|
||||||
@Value("${app.import.excel.col.filename:0}") private int colFilename;
|
@Value("${app.import.excel.col.filename:0}")
|
||||||
@Value("${app.import.excel.col.date:1}") private int colDate;
|
private int colFilename;
|
||||||
@Value("${app.import.excel.col.location:2}") private int colLocation;
|
@Value("${app.import.excel.col.date:1}")
|
||||||
@Value("${app.import.excel.col.transcription:3}") private int colTranscription;
|
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";
|
private static final String IMPORT_DIR = "/import";
|
||||||
|
|
||||||
|
@Async
|
||||||
|
public void runImportAsync() {
|
||||||
|
runImport();
|
||||||
|
}
|
||||||
|
|
||||||
public String runImport() {
|
public String runImport() {
|
||||||
try {
|
try {
|
||||||
// 1. Excel finden
|
// 1. Excel finden
|
||||||
@@ -74,7 +84,7 @@ public class MassImportService {
|
|||||||
private int processExcel(File excelFile) throws IOException {
|
private int processExcel(File excelFile) throws IOException {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
try (FileInputStream fis = new FileInputStream(excelFile);
|
try (FileInputStream fis = new FileInputStream(excelFile);
|
||||||
Workbook workbook = new XSSFWorkbook(fis)) {
|
Workbook workbook = new XSSFWorkbook(fis)) {
|
||||||
|
|
||||||
Sheet sheet = workbook.getSheetAt(0);
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
|
|
||||||
@@ -83,10 +93,12 @@ public class MassImportService {
|
|||||||
|
|
||||||
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
||||||
Row row = sheet.getRow(i);
|
Row row = sheet.getRow(i);
|
||||||
if (row == null) continue;
|
if (row == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
String filename = getCellValue(row.getCell(colFilename));
|
String filename = getCellValue(row.getCell(colFilename));
|
||||||
if (filename == null || filename.isBlank()) continue;
|
if (filename == null || filename.isBlank())
|
||||||
|
continue;
|
||||||
|
|
||||||
// Datei auf der Festplatte suchen
|
// Datei auf der Festplatte suchen
|
||||||
Optional<File> fileOnDisk = findFileRecursive(filename);
|
Optional<File> fileOnDisk = findFileRecursive(filename);
|
||||||
@@ -124,9 +136,9 @@ public class MassImportService {
|
|||||||
String s3Key = "documents/" + UUID.randomUUID() + "_" + file.getName();
|
String s3Key = "documents/" + UUID.randomUUID() + "_" + file.getName();
|
||||||
try {
|
try {
|
||||||
s3Client.putObject(PutObjectRequest.builder()
|
s3Client.putObject(PutObjectRequest.builder()
|
||||||
.bucket(bucketName)
|
.bucket(bucketName)
|
||||||
.key(s3Key)
|
.key(s3Key)
|
||||||
.build(),
|
.build(),
|
||||||
RequestBody.fromFile(file));
|
RequestBody.fromFile(file));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("S3 Upload Fehler für " + file.getName(), e);
|
log.error("S3 Upload Fehler für " + file.getName(), e);
|
||||||
@@ -162,9 +174,12 @@ public class MassImportService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getCellValue(Cell cell) {
|
private String getCellValue(Cell cell) {
|
||||||
if (cell == null) return null;
|
if (cell == null)
|
||||||
if (cell.getCellType() == CellType.STRING) return cell.getStringCellValue();
|
return null;
|
||||||
if (cell.getCellType() == CellType.NUMERIC) return String.valueOf((int)cell.getNumericCellValue());
|
if (cell.getCellType() == CellType.STRING)
|
||||||
|
return cell.getStringCellValue();
|
||||||
|
if (cell.getCellType() == CellType.NUMERIC)
|
||||||
|
return String.valueOf((int) cell.getNumericCellValue());
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user