Browse Source

fix: Exception 대응 추가, 트래픽 LIMIT Exception일 경우 정지 추가

feature/address-coordinate
lkd9125(이경도) 8 months ago
parent
commit
8a66b21f40
  1. 40
      pav-server/src/main/java/com/palnet/biz/api/external/model/SunRiseSetErrorXmlRs.java
  2. 117
      pav-server/src/main/java/com/palnet/biz/scheduler/external/service/SunRiseSchedulerService.java
  3. 3
      pav-server/src/main/java/com/palnet/comn/code/ErrorCode.java

40
pav-server/src/main/java/com/palnet/biz/api/external/model/SunRiseSetErrorXmlRs.java vendored

@ -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;
}
}

117
pav-server/src/main/java/com/palnet/biz/scheduler/external/service/SunRiseSchedulerService.java vendored

@ -1,5 +1,6 @@
package com.palnet.biz.scheduler.external.service; 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.SunRiseSetRs;
import com.palnet.biz.api.external.model.SunRiseSetXmlRs; import com.palnet.biz.api.external.model.SunRiseSetXmlRs;
import com.palnet.biz.api.external.service.SunRiseSetMapper; import com.palnet.biz.api.external.service.SunRiseSetMapper;
@ -69,8 +70,12 @@ public class SunRiseSchedulerService {
} }
}; };
private int dayCallCount = 0;
public void sunsetSchedule(){ public void sunsetSchedule(){
this.dayCallCount = 0;
List<String> todayLocation = this.todayLocation(); List<String> todayLocation = this.todayLocation();
LocalDate today = LocalDate.now(); LocalDate today = LocalDate.now();
List<String> timeColumn = List.of("sunrise", "suntransit", "sunset", "moonrise", "moontransit", "moonset", "civilm", "civile", "nautm", "naute", "astm", "aste"); List<String> 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")); String locdate = today.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
for(int j = 0; j < todayLocation.size(); j ++){ for(int j = 0; j < todayLocation.size(); j ++){
String location = todayLocation.get(j); try {
String location = todayLocation.get(j);
List<SunRiseSetRs> results = callSunRiseSet(locdate, location);
// trim 제거 및 6자리로 맞추기 List<SunRiseSetRs> results = callSunRiseSet(locdate, location);
results.forEach(result -> { // trim 제거 및 6자리로 맞추기
Class<? extends SunRiseSetRs> aClass = result.getClass(); results.forEach(result -> {
Field[] declaredFields = aClass.getDeclaredFields(); Class<? extends SunRiseSetRs> aClass = result.getClass();
for (Field field: declaredFields){ Field[] declaredFields = aClass.getDeclaredFields();
if(timeColumn.contains(field.getName())){ for (Field field: declaredFields){
field.setAccessible(true); if(timeColumn.contains(field.getName())){
try { field.setAccessible(true);
Object o = field.get(result); try {
if(o instanceof String){ Object o = field.get(result);
String str = (String) o; if(o instanceof String){
if(!str.isEmpty()) { String str = (String) o;
String trim = str.trim(); if(!str.isEmpty()) {
if(trim.length() <= 6 && trim.length() >= 4 && trim.matches("-?\\d+(\\.\\d+)?")) { String trim = str.trim();
field.set(result, String.format("%-6s", trim).replace(' ', '0')); if(trim.length() <= 6 && trim.length() >= 4 && trim.matches("-?\\d+(\\.\\d+)?")) {
} else { field.set(result, String.format("%-6s", trim).replace(' ', '0'));
field.set(result, null); } else {
field.set(result, null);
}
} }
} }
}
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
}
} }
} }
}
ComRiseSetBasPK pk = SunRiseSetMapper.MAPPER.toEntityPk(result); ComRiseSetBasPK pk = SunRiseSetMapper.MAPPER.toEntityPk(result);
ComRiseSetBas entity = SunRiseSetMapper.MAPPER.toEntity(result); ComRiseSetBas entity = SunRiseSetMapper.MAPPER.toEntity(result);
entity.setId(pk); 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){ if(i%30 == 0){
@ -127,9 +141,11 @@ public class SunRiseSchedulerService {
sunRiseSet.clear(); sunRiseSet.clear();
} }
} }
log.info("dayCallCount => {}", this.dayCallCount);
} }
private List<SunRiseSetRs> callSunRiseSet(String locdate, String location) { private List<SunRiseSetRs> callSunRiseSet(String locdate, String location) throws CustomException {
String uriStr = UriComponentsBuilder.fromUriString(AREA_RISE_SEET_INFO_URL) String uriStr = UriComponentsBuilder.fromUriString(AREA_RISE_SEET_INFO_URL)
.queryParam("serviceKey", SUN_KEY) .queryParam("serviceKey", SUN_KEY)
@ -142,17 +158,37 @@ public class SunRiseSchedulerService {
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build(); .build();
ResponseEntity<SunRiseSetXmlRs> resp = client.get() try {
.uri(uriStr) ResponseEntity<SunRiseSetXmlRs> resp = client.get()
.retrieve() .uri(uriStr)
.toEntity(SunRiseSetXmlRs.class) .retrieve()
.block(); .toEntity(SunRiseSetXmlRs.class)
.block();
log.debug(">>> resp : {}", resp); log.debug(">>> resp : {}", resp);
List<SunRiseSetRs> items = resp.getBody().getBody().getItems();
List<SunRiseSetRs> items = resp.getBody().getBody().getItems();
return items;
} catch (Exception e){
ResponseEntity<SunRiseSetErrorXmlRs> 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<String> todayLocation (){ private List<String> todayLocation (){
@ -206,5 +242,4 @@ public class SunRiseSchedulerService {
return (day % 4) + 1; return (day % 4) + 1;
} }
} }

3
pav-server/src/main/java/com/palnet/comn/code/ErrorCode.java

@ -20,6 +20,9 @@ public enum ErrorCode {
EXTERNAL_API_ERROR("EA500", "외부서버 호출에 실패하였습니다."), EXTERNAL_API_ERROR("EA500", "외부서버 호출에 실패하였습니다."),
AUTH_NAUTHORIZED("AU001", "권한이 없습니다."), AUTH_NAUTHORIZED("AU001", "권한이 없습니다."),
LIMIT_CALL("LT001", "호출횟수가 초과되었습니다."),
// TS 연동 ERROR CODE // TS 연동 ERROR CODE
TS_SUCCESS("TS200", "정상적으로 수신되었습니다."), TS_SUCCESS("TS200", "정상적으로 수신되었습니다."),
TS_PARAM("TS300", "메시지 규격이 다릅니다."), // Json 포멧이 틀린 경우 TS_PARAM("TS300", "메시지 규격이 다릅니다."), // Json 포멧이 틀린 경우

Loading…
Cancel
Save