property 설정
This commit is contained in:
@@ -15,16 +15,27 @@ import org.apache.hc.core5.util.Timeout;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Comparator;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Slf4j
|
||||
public class KjbEaiBatchModule {
|
||||
private final KjbProperty property;
|
||||
private final CloseableHttpClient httpClient;
|
||||
private final RequestConfig requestConfig;
|
||||
private LocalDateTime lastFileRetentionCheckTime;
|
||||
|
||||
|
||||
|
||||
public KjbEaiBatchModule(@NotNull KjbProperty property) {
|
||||
@@ -104,4 +115,52 @@ public class KjbEaiBatchModule {
|
||||
return resMessage;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 1시간 주기로 파일 유지 및 삭제 처리
|
||||
*/
|
||||
public void processFileRetentionDays() {
|
||||
if (lastFileRetentionCheckTime == null) {
|
||||
lastFileRetentionCheckTime = LocalDateTime.now();
|
||||
}
|
||||
|
||||
// 현재 시간과 1시간 이상 차이 날때 동작
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
boolean isOver = Duration.between(lastFileRetentionCheckTime, now).abs().toHours() == 1;
|
||||
|
||||
if (!isOver) return;
|
||||
|
||||
this.walkDeletePath(property.getUmsEaiBatchSendDir());
|
||||
this.walkDeletePath(property.getUmsEaiBatchBackupDir());
|
||||
this.walkDeletePath(property.getUmsEaiBatchErrorDir());
|
||||
|
||||
lastFileRetentionCheckTime = now;
|
||||
}
|
||||
|
||||
private void walkDeletePath(String path) {
|
||||
final int retentionDays = property.getUmsEaiBatchRetentionDate();
|
||||
log.info("WalkDelete - {} / Retention days : {}", path, retentionDays);
|
||||
|
||||
Path dir = Paths.get(path);
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
try (Stream<Path> paths = Files.walk(dir)) {
|
||||
paths.sorted(Comparator.reverseOrder()).forEach(p -> {
|
||||
try {
|
||||
BasicFileAttributes attrs = Files.readAttributes(p, BasicFileAttributes.class);
|
||||
LocalDateTime created = attrs.creationTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
||||
|
||||
long over = Math.abs(Duration.between(created, now).toDays());
|
||||
if (over >= retentionDays) {
|
||||
Files.deleteIfExists(p);
|
||||
log.info("Delete over {} days - {}", retentionDays, p);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Delete fail - {}", p.getFileName().toString(), e);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
log.warn("Not found path : {}", path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user