From 8a66b21f40590257efa518efb49835665653fdef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lkd9125=28=EC=9D=B4=EA=B2=BD=EB=8F=84=29?= Date: Mon, 29 Jan 2024 14:56:22 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20Exception=20=EB=8C=80=EC=9D=91=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80,=20=ED=8A=B8=EB=9E=98=ED=94=BD=20LIMIT=20Exc?= =?UTF-8?q?eption=EC=9D=BC=20=EA=B2=BD=EC=9A=B0=20=EC=A0=95=EC=A7=80=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../external/model/SunRiseSetErrorXmlRs.java | 40 ++++++ .../service/SunRiseSchedulerService.java | 117 ++++++++++++------ .../java/com/palnet/comn/code/ErrorCode.java | 3 + 3 files changed, 119 insertions(+), 41 deletions(-) create mode 100644 pav-server/src/main/java/com/palnet/biz/api/external/model/SunRiseSetErrorXmlRs.java diff --git a/pav-server/src/main/java/com/palnet/biz/api/external/model/SunRiseSetErrorXmlRs.java b/pav-server/src/main/java/com/palnet/biz/api/external/model/SunRiseSetErrorXmlRs.java new file mode 100644 index 00000000..80568c49 --- /dev/null +++ b/pav-server/src/main/java/com/palnet/biz/api/external/model/SunRiseSetErrorXmlRs.java @@ -0,0 +1,40 @@ +package com.palnet.biz.api.external.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@XmlRootElement(name = "OpenAPI_ServiceResponse") +@XmlAccessorType(XmlAccessType.FIELD) +public class SunRiseSetErrorXmlRs { + + @XmlElement(name = "cmmMsgHeader") + private CmmMsgHeader cmmMsgHeader; + + @Data + @NoArgsConstructor + @AllArgsConstructor + @Builder + @XmlRootElement(name = "cmmMsgHeader") + @XmlAccessorType(XmlAccessType.FIELD) + public static class CmmMsgHeader { + @XmlElement(name = "errMsg") + private String errMsg; + + @XmlElement(name = "returnAuthMsg") + private String returnAuthMsg; + + @XmlElement(name = "returnReasonCode") + private String returnReasonCode; + } +} diff --git a/pav-server/src/main/java/com/palnet/biz/scheduler/external/service/SunRiseSchedulerService.java b/pav-server/src/main/java/com/palnet/biz/scheduler/external/service/SunRiseSchedulerService.java index 2875f163..a661973f 100644 --- a/pav-server/src/main/java/com/palnet/biz/scheduler/external/service/SunRiseSchedulerService.java +++ b/pav-server/src/main/java/com/palnet/biz/scheduler/external/service/SunRiseSchedulerService.java @@ -1,5 +1,6 @@ package com.palnet.biz.scheduler.external.service; +import com.palnet.biz.api.external.model.SunRiseSetErrorXmlRs; import com.palnet.biz.api.external.model.SunRiseSetRs; import com.palnet.biz.api.external.model.SunRiseSetXmlRs; import com.palnet.biz.api.external.service.SunRiseSetMapper; @@ -69,8 +70,12 @@ public class SunRiseSchedulerService { } }; + private int dayCallCount = 0; + public void sunsetSchedule(){ + this.dayCallCount = 0; + List todayLocation = this.todayLocation(); LocalDate today = LocalDate.now(); List timeColumn = List.of("sunrise", "suntransit", "sunset", "moonrise", "moontransit", "moonset", "civilm", "civile", "nautm", "naute", "astm", "aste"); @@ -83,42 +88,51 @@ public class SunRiseSchedulerService { String locdate = today.format(DateTimeFormatter.ofPattern("yyyyMMdd")); for(int j = 0; j < todayLocation.size(); j ++){ - String location = todayLocation.get(j); - - List results = callSunRiseSet(locdate, location); - // trim 제거 및 6자리로 맞추기 - results.forEach(result -> { - Class aClass = result.getClass(); - Field[] declaredFields = aClass.getDeclaredFields(); - for (Field field: declaredFields){ - if(timeColumn.contains(field.getName())){ - field.setAccessible(true); - try { - Object o = field.get(result); - if(o instanceof String){ - String str = (String) o; - if(!str.isEmpty()) { - String trim = str.trim(); - if(trim.length() <= 6 && trim.length() >= 4 && trim.matches("-?\\d+(\\.\\d+)?")) { - field.set(result, String.format("%-6s", trim).replace(' ', '0')); - } else { - field.set(result, null); + try { + String location = todayLocation.get(j); + + List results = callSunRiseSet(locdate, location); + // trim 제거 및 6자리로 맞추기 + results.forEach(result -> { + Class aClass = result.getClass(); + Field[] declaredFields = aClass.getDeclaredFields(); + for (Field field: declaredFields){ + if(timeColumn.contains(field.getName())){ + field.setAccessible(true); + try { + Object o = field.get(result); + if(o instanceof String){ + String str = (String) o; + if(!str.isEmpty()) { + String trim = str.trim(); + if(trim.length() <= 6 && trim.length() >= 4 && trim.matches("-?\\d+(\\.\\d+)?")) { + field.set(result, String.format("%-6s", trim).replace(' ', '0')); + } else { + field.set(result, null); + } } } - } - } catch (IllegalAccessException e) { - throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } } } - } - ComRiseSetBasPK pk = SunRiseSetMapper.MAPPER.toEntityPk(result); - ComRiseSetBas entity = SunRiseSetMapper.MAPPER.toEntity(result); - entity.setId(pk); + ComRiseSetBasPK pk = SunRiseSetMapper.MAPPER.toEntityPk(result); + ComRiseSetBas entity = SunRiseSetMapper.MAPPER.toEntity(result); + entity.setId(pk); + + sunRiseSet.add(entity); + }); + }catch (CustomException e) { + String errorCode = e.getErrorCode(); - sunRiseSet.add(entity); - }); + if(errorCode.equals("LT001")) throw new CustomException(ErrorCode.fromCode(errorCode)); + }catch (Exception e){ + log.error("API CALL Error :: location {} , locdate {}", locdate, locdate); + log.error("", e); + } } if(i%30 == 0){ @@ -127,9 +141,11 @@ public class SunRiseSchedulerService { sunRiseSet.clear(); } } + + log.info("dayCallCount => {}", this.dayCallCount); } - private List callSunRiseSet(String locdate, String location) { + private List callSunRiseSet(String locdate, String location) throws CustomException { String uriStr = UriComponentsBuilder.fromUriString(AREA_RISE_SEET_INFO_URL) .queryParam("serviceKey", SUN_KEY) @@ -142,17 +158,37 @@ public class SunRiseSchedulerService { .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) .build(); - ResponseEntity resp = client.get() - .uri(uriStr) - .retrieve() - .toEntity(SunRiseSetXmlRs.class) - .block(); - - log.debug(">>> resp : {}", resp); - - List items = resp.getBody().getBody().getItems(); + try { + ResponseEntity resp = client.get() + .uri(uriStr) + .retrieve() + .toEntity(SunRiseSetXmlRs.class) + .block(); + log.debug(">>> resp : {}", resp); + List items = resp.getBody().getBody().getItems(); + + return items; + + } catch (Exception e){ + ResponseEntity resp = client.get() + .uri(uriStr) + .retrieve() + .toEntity(SunRiseSetErrorXmlRs.class) + .block(); + + SunRiseSetErrorXmlRs errorModel = resp.getBody(); + + switch (errorModel.getCmmMsgHeader().getReturnReasonCode()){ + case "22" /* LIMITED_NUMBER_OF_SERVICE_REQUESTS_EXCEEDS_ERROR[API 콜 트래픽 10,000건 초과함] */ : + throw new CustomException(ErrorCode.LIMIT_CALL); + default: + break; + } - return items; + return null; + } finally { + this.dayCallCount++; + } } private List todayLocation (){ @@ -206,5 +242,4 @@ public class SunRiseSchedulerService { return (day % 4) + 1; } - } diff --git a/pav-server/src/main/java/com/palnet/comn/code/ErrorCode.java b/pav-server/src/main/java/com/palnet/comn/code/ErrorCode.java index 62d0c65d..092cdf67 100644 --- a/pav-server/src/main/java/com/palnet/comn/code/ErrorCode.java +++ b/pav-server/src/main/java/com/palnet/comn/code/ErrorCode.java @@ -20,6 +20,9 @@ public enum ErrorCode { EXTERNAL_API_ERROR("EA500", "외부서버 호출에 실패하였습니다."), AUTH_NAUTHORIZED("AU001", "권한이 없습니다."), + LIMIT_CALL("LT001", "호출횟수가 초과되었습니다."), + + // TS 연동 ERROR CODE TS_SUCCESS("TS200", "정상적으로 수신되었습니다."), TS_PARAM("TS300", "메시지 규격이 다릅니다."), // Json 포멧이 틀린 경우