From 85156986031aff348b8ec00a5061d1ee4a7312d4 Mon Sep 17 00:00:00 2001 From: 2210045 <2210045@P9801Y.kjbank.dom> Date: Thu, 26 Feb 2026 10:21:38 +0900 Subject: [PATCH] =?UTF-8?q?=EC=98=A4=EB=9E=98=EB=90=9C=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20job?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/scheduler/OldFileDeleteJob.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/com/eactive/eai/common/scheduler/OldFileDeleteJob.java diff --git a/src/com/eactive/eai/common/scheduler/OldFileDeleteJob.java b/src/com/eactive/eai/common/scheduler/OldFileDeleteJob.java new file mode 100644 index 0000000..9df946e --- /dev/null +++ b/src/com/eactive/eai/common/scheduler/OldFileDeleteJob.java @@ -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()); + } + } + } + } + } +}