오래된파일삭제 job

This commit is contained in:
2210045
2026-02-26 10:21:38 +09:00
parent 8cd8281fd6
commit 8515698603
@@ -0,0 +1,99 @@
package com.eactive.eai.common.scheduler;
import java.io.File;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.eactive.eai.common.util.Logger;
public class OldFileDeleteJob implements Job {
static Logger logger = Logger.getLogger(Logger.LOGGER_SCHEDULER);
public void execute(JobExecutionContext context) throws JobExecutionException {
String dirValue = context.getJobDetail().getJobDataMap().getString("DIR");
String msg = context.getJobDetail().getJobDataMap().getString("MSG");
if (dirValue == null || dirValue .trim().isEmpty()) {
logger.error("DIR 값이 없습니다. 작업 중단.");
return;
}
if (msg == null || msg.trim().isEmpty()) {
logger.error("MSG 값이 없습니다. 작업 중단.");
return;
}
int durationDays;
try {
durationDays = Integer.parseInt(msg.trim());
} catch (NumberFormatException e) {
logger.error("MSG 값이 숫자가 아닙니다. 작업 중단. MSG=" + msg);
return;
}
if (durationDays <= 0) {
logger.error("보관일수는 1 이상이어야 합니다. 작업 중단. MSG=" + msg);
return;
}
String[] dirs = dirValue .split(",");
for (String dir : dirs) {
String trimmedDir = dir.trim();
if (trimmedDir.isEmpty()) continue;
try {
logger.info("파일 정리 시작: " + trimmedDir + " (보관일수: " + durationDays + ")");
deleteOldFilesRecursively(durationDays, trimmedDir);
} catch (Exception e) {
logger.error("파일 정리 실패: " + trimmedDir, e);
}
}
}
public static void deleteOldFilesRecursively(int durationDays, String dir) {
File baseDir = new File(dir);
if (!baseDir.exists() || !baseDir.isDirectory()) {
throw new IllegalArgumentException("Invalid directory: " + dir);
}
long expireMillis = durationDays * 24L * 60 * 60 * 1000;
deleteFilesOnly(baseDir, expireMillis);
}
private static void deleteFilesOnly(File dir, long expireMillis) {
File[] list = dir.listFiles();
if (list == null) {
logger.warn("디렉터리 접근 실패: " + dir.getAbsolutePath());
return;
}
long now = System.currentTimeMillis();
for (File file : list) {
if (file.isDirectory()) {
deleteFilesOnly(file, expireMillis);
}
else if (file.isFile()) {
long age = now - file.lastModified();
if (age > expireMillis) {
boolean deleted = file.delete();
if (!deleted) {
logger.warn("파일 삭제 실패: " + file.getAbsolutePath());
} else {
logger.info("파일 삭제 완료: " + file.getAbsolutePath());
}
}
}
}
}
}