diff --git a/src/main/java/com/eactive/ext/kjb/common/KjbProperty.java b/src/main/java/com/eactive/ext/kjb/common/KjbProperty.java index a777568..f848d93 100644 --- a/src/main/java/com/eactive/ext/kjb/common/KjbProperty.java +++ b/src/main/java/com/eactive/ext/kjb/common/KjbProperty.java @@ -9,49 +9,64 @@ import javax.validation.constraints.Pattern; @Data public class KjbProperty { + @KjbPropertyValue(key = "kjb.eai.batch.url") @Pattern(regexp = "^(https?://[^\\s/$.?#][^\\s]*)|\\s*$", message = "kjb.eai_batch.url 는 유효한 URL 형식이거나 공백이어야 합니다. 예: http://example.com/api") private String eaiBatchUrl; + @KjbPropertyValue(key = "kjb.eai.batch.connection_timeout", defaultValue = "5") @Min(1) @Max(10) private int eaiBatchConnectionTimeout = 5; + @KjbPropertyValue(key = "kjb.eai.batch.response_timeout", defaultValue = "5") @Min(1) @Max(30) private int eaiBatchResponseTimeout = 5; + @KjbPropertyValue(key = "kjb.eai.batch.timeout", defaultValue = "10") @Min(1) @Max(30) private int eaiBatchTimeout = 10; + @KjbPropertyValue(key = "kjb.eai.batch.charset", defaultValue = "euc-kr") @NotEmpty private String eaiBatchCharset = "euc-kr"; + @KjbPropertyValue(key = "kjb.ums.eai_batch.send_dir") @NotEmpty(message = "kjb.ums.eai_batch.send_dir 는 필수값입니다.") private String umsEaiBatchSendDir; + @KjbPropertyValue(key = "kjb.ums.eai_batch.backup_dir") @NotEmpty(message = "kjb.ums.eai_batch.backup_dir 는 필수값입니다.") private String umsEaiBatchBackupDir; + @KjbPropertyValue(key = "kjb.ums.eai_batch.error_dir") @NotEmpty(message = "kjb.ums.eai_batch.error_dir 는 필수값입니다.") private String umsEaiBatchErrorDir; + @KjbPropertyValue(key = "kjb.ums.eai_batch.interface_id") @Pattern(regexp = "^[a-zA-Z0-9]{13}$", message = "kjb.ums.eai_batch.interface_id 는 12자리 영문대소문자 및 숫자 조합이어야 합니다.") private String umsEaiBatchInterfaceId; + @KjbPropertyValue(key = "kjb.ums.eai_batch.retention_date", defaultValue = "60") @Min(1) @Max(365) private int umsEaiBatchRetentionDate = 60; + @KjbPropertyValue(key = "kjb.ums.host") @Pattern(regexp = "^(https?://[^\\s/$.?#][^\\s]*)|\\s*$", message = "kjb.ums.host_url 는 유효한 URL 형식이거나 공백이어야 합니다. 예: http://example.com/api") private String umsHostUrl; + @KjbPropertyValue(key = "kjb.ums.api_key") @Pattern(regexp = "^[a-zA-Z0-9_-]{36}$", message = "kjb.ums.api_key 입력 형식이 맞지 않습니다.") private String umsApiKey; + @KjbPropertyValue(key = "kjb.ums.connection_timeout", defaultValue = "5") @Min(1) @Max(10) private int umsConnectionTimeout = 5; + @KjbPropertyValue(key = "kjb.ums.response_timeout", defaultValue = "5") @Min(1) @Max(30) private int umsResponseTimeout = 5; + @KjbPropertyValue(key = "kjb.ums.timeout", defaultValue = "10") @Min(1) @Max(30) private int umsTimeout = 10; } diff --git a/src/main/java/com/eactive/ext/kjb/common/KjbPropertyValue.java b/src/main/java/com/eactive/ext/kjb/common/KjbPropertyValue.java new file mode 100644 index 0000000..0522f7d --- /dev/null +++ b/src/main/java/com/eactive/ext/kjb/common/KjbPropertyValue.java @@ -0,0 +1,13 @@ +package com.eactive.ext.kjb.common; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface KjbPropertyValue { + String key(); + String defaultValue() default ""; +} diff --git a/src/main/java/com/eactive/ext/kjb/eai/KjbEaiBatchModule.java b/src/main/java/com/eactive/ext/kjb/eai/KjbEaiBatchModule.java index d1f7a2e..72c57f3 100644 --- a/src/main/java/com/eactive/ext/kjb/eai/KjbEaiBatchModule.java +++ b/src/main/java/com/eactive/ext/kjb/eai/KjbEaiBatchModule.java @@ -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 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); + } + } } diff --git a/src/main/java/com/eactive/ext/kjb/utils/KjbPropertyUtil.java b/src/main/java/com/eactive/ext/kjb/utils/KjbPropertyUtil.java new file mode 100644 index 0000000..39f836a --- /dev/null +++ b/src/main/java/com/eactive/ext/kjb/utils/KjbPropertyUtil.java @@ -0,0 +1,35 @@ +package com.eactive.ext.kjb.utils; + +import com.eactive.ext.kjb.common.KjbProperty; +import com.eactive.ext.kjb.common.KjbPropertyValue; +import org.apache.commons.lang3.StringUtils; + +import java.lang.reflect.Field; + +public class KjbPropertyUtil { + public static void main(String[] args) { + Class clazz = KjbProperty.class; + + StringBuilder sb = new StringBuilder(); + + for (Field field : clazz.getDeclaredFields()) { + KjbPropertyValue anno = field.getAnnotation(KjbPropertyValue.class); + + if (anno != null) { + sb.append("insert into EMSADM.TSEAIRM24 (PRPTYGROUPNAME, PRPTYNAME, PRPTY2VAL) values ('Monitoring', "); + + String key = anno.key(); + String defValue = anno.defaultValue(); + if (StringUtils.isEmpty(defValue)) defValue = ""; + + sb.append("'").append(key).append("', '").append(addQuote(defValue)).append("');\n"); + } + } + + System.out.println(sb); + } + + private static String addQuote(String str) { + return str.replace("'", "''"); + } +}