Skip to content

Commit

Permalink
fix: 실패시 재시도 도입 및 에러 로깅
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjerry committed Oct 8, 2023
1 parent 3b16a78 commit 24ae88b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
34 changes: 26 additions & 8 deletions src/main/java/com/toggn/mma/itp/client/EnterpriseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import static org.slf4j.LoggerFactory.getLogger;

@Component
public class EnterpriseClient implements OpenAPIClient {

private static final Logger log = getLogger(EnterpriseClient.class);

@Value("${mma.itp.enterprise.client.requestUrl}")
private String requestUrl;
@Value("${mma.itp.enterprise.client.secretKey}")
Expand All @@ -21,14 +28,25 @@ public class EnterpriseClient implements OpenAPIClient {
public Document request() {
final String url = requestUrl + "?ServiceKey=" + secretKey + "&numOfRows=" + ALL_ROWS_SIZE;

final Connection connect = Jsoup.connect(url)
.timeout(60 * 1000)
.maxBodySize(0);

try {
return Jsoup.parse(connect.get().html(), "", Parser.xmlParser());
} catch (final IOException e) {
throw new IllegalArgumentException("Invalid url: " + url, e);
for (int i = 0; i < MAXIMUM_TRY_COUNT; i++) {
final Connection connect = getConnection(url);
try {
return Jsoup.parse(connect.get().html(), "", Parser.xmlParser());
} catch (final SocketException e) {
log.error("EnterpriseClient request failed by SocketException");
} catch (final SocketTimeoutException e) {
log.error("EnterpriseClient request failed by SocketTimeoutException");
} catch (final IOException e) {
log.error("EnterpriseClient request failed by IOException");
}
}

throw new RuntimeException("EnterpriseClient request fail");
}

private Connection getConnection(final String url) {
return Jsoup.connect(url)
.timeout(TIMEOUT_MILLI_SEC)
.maxBodySize(MAX_BODY_SIZE);
}
}
33 changes: 26 additions & 7 deletions src/main/java/com/toggn/mma/itp/client/NoticeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import static org.slf4j.LoggerFactory.getLogger;

@Component
public class NoticeClient implements OpenAPIClient {

private static final Logger log = getLogger(NoticeClient.class);

@Value("${mma.itp.notice.client.requestUrl}")
private String requestUrl;
@Value("${mma.itp.notice.client.secretKey}")
Expand All @@ -21,14 +28,26 @@ public class NoticeClient implements OpenAPIClient {
public Document request() {
final String url = requestUrl + "?ServiceKey=" + secretKey + "&numOfRows=" + ALL_ROWS_SIZE;

final Connection connect = Jsoup.connect(url)
.timeout(60 * 1000)
.maxBodySize(0);
for (int i = 0; i < MAXIMUM_TRY_COUNT; i++) {
final Connection connect = getConnection(url);

try {
return Jsoup.parse(connect.get().html(), "", Parser.xmlParser());
} catch (final IOException e) {
throw new IllegalArgumentException("Invalid url: " + url, e);
try {
return Jsoup.parse(connect.get().html(), "", Parser.xmlParser());
} catch (final SocketException e) {
log.error("NoticeClient request failed by SocketException");
} catch (final SocketTimeoutException e) {
log.error("NoticeClient request failed by SocketTimeoutException");
} catch (final IOException e) {
log.error("NoticeClient request failed by IOException");
}
}

throw new RuntimeException("NoticeClient request fail");
}

private Connection getConnection(final String url) {
return Jsoup.connect(url)
.timeout(TIMEOUT_MILLI_SEC)
.maxBodySize(MAX_BODY_SIZE);
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/toggn/mma/itp/client/OpenAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
public interface OpenAPIClient {

String ALL_ROWS_SIZE = "99999";
int MAXIMUM_TRY_COUNT = 5;
int TIMEOUT_MILLI_SEC = 60 * 1000;
int MAX_BODY_SIZE = 0;

Document request();
}

0 comments on commit 24ae88b

Please sign in to comment.