commit(1)
This commit is contained in:
parent
752e88c66e
commit
9e4dc4e00a
2 changed files with 47 additions and 25 deletions
|
|
@ -1,9 +1,9 @@
|
|||
package ervu.client.okopf;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
|
@ -14,7 +14,9 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.retry.annotation.Backoff;
|
||||
import org.springframework.retry.annotation.Recover;
|
||||
import org.springframework.retry.annotation.Retryable;
|
||||
import org.springframework.retry.support.RetrySynchronizationManager;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
|
|
@ -22,27 +24,45 @@ import org.springframework.stereotype.Component;
|
|||
*/
|
||||
@Component
|
||||
public class EsnsiOkopfClient {
|
||||
private static final Logger logger = LoggerFactory.getLogger(EsnsiOkopfClient.class);
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(EsnsiOkopfClient.class);
|
||||
|
||||
@Value("${esnsi.okopf.url}")
|
||||
private String url;
|
||||
@Value("${esnsi.okopf.connect.timeout:2000}")
|
||||
private int connectTimeout;
|
||||
@Value("${esnsi.okopf.read.timeout:4000}")
|
||||
private int readTimeout;
|
||||
|
||||
@Retryable(value = IOException.class, maxAttemptsExpression = "${esnsi.okopf.retry.max.attempts.load:3}", backoff =
|
||||
@Backoff(delayExpression = "${esnsi.okop.retry.delay.load:30000}"))
|
||||
public String getJsonOkopFormData() {
|
||||
try (BufferedInputStream in = new BufferedInputStream(new URL(url).openStream());
|
||||
ZipInputStream archiveStream = new ZipInputStream(in);
|
||||
BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(archiveStream, StandardCharsets.UTF_8))) {
|
||||
if (Objects.nonNull(archiveStream.getNextEntry())) {
|
||||
logger.info("Received an archive in response.");
|
||||
return br.lines().collect(Collectors.joining(System.lineSeparator()));
|
||||
@Retryable(value = {IOException.class}, maxAttemptsExpression = "${esnsi.okopf.retry.max.attempts.load:3}", backoff =
|
||||
@Backoff(delayExpression = "${esnsi.okop.retry.delay.load:2000}"))
|
||||
public String getJsonOkopFormData() throws IOException {
|
||||
int retryCount = RetrySynchronizationManager.getContext().getRetryCount() + 1;
|
||||
LOGGER.info("Attempt #{} to load json okopf form data from URL: {}", retryCount, url);
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
connection.setConnectTimeout(connectTimeout);
|
||||
connection.setReadTimeout(readTimeout);
|
||||
int statusCode = connection.getResponseCode();
|
||||
LOGGER.info("Connecting timeout set to {} ms, read timeout set to {}", connectTimeout, readTimeout);
|
||||
if (statusCode >= 200 && statusCode <= 202) {
|
||||
try (ZipInputStream archiveStream = new ZipInputStream(connection.getInputStream());
|
||||
BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(archiveStream, StandardCharsets.UTF_8))) {
|
||||
if (Objects.nonNull(archiveStream.getNextEntry())) {
|
||||
LOGGER.info("Received an archive in response.");
|
||||
return br.lines().collect(Collectors.joining(System.lineSeparator()));
|
||||
}
|
||||
LOGGER.info("Received an empty archive in response.");
|
||||
} catch (IOException e) {
|
||||
throw new IOException("Failed while loading json okopf form data from URL:" + url, e);
|
||||
}
|
||||
logger.info("Received an empty archive in response. Skipping load okpof file process");
|
||||
}
|
||||
catch (SecurityException | IOException e) {
|
||||
logger.error("Failed to send HTTP request {} or process the response for okopf file.", url, e);
|
||||
}
|
||||
throw new IOException("Skipping load okopf file process. HTTP status code: " + statusCode + " URL: " + url);
|
||||
}
|
||||
|
||||
@Recover
|
||||
public String recover(Exception e) {
|
||||
LOGGER.error("All retry attempts for IO operation failed for URL: {}. Triggering fallback logic.", url, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ import static org.springframework.util.StringUtils.hasText;
|
|||
@Service
|
||||
@DependsOn({"liquibase"})
|
||||
public class EsnsiOkopfSchedulerServiceImpl implements EsnsiOkopfSchedulerService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(
|
||||
EsnsiOkopfSchedulerServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
|
|
@ -48,11 +48,11 @@ public class EsnsiOkopfSchedulerServiceImpl implements EsnsiOkopfSchedulerServic
|
|||
@Transactional
|
||||
public void init() {
|
||||
if (!cronLoad.equals(CRON_DISABLED)) {
|
||||
logger.info("Synchronization with esnsi okopf enabled");
|
||||
LOGGER.info("Synchronization with esnsi okopf enabled");
|
||||
load();
|
||||
}
|
||||
else {
|
||||
logger.info("Synchronization with esnsi okopf disabled");
|
||||
LOGGER.info("Synchronization with esnsi okopf disabled");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -60,22 +60,24 @@ public class EsnsiOkopfSchedulerServiceImpl implements EsnsiOkopfSchedulerServic
|
|||
@SchedulerLock(name = "loadOkopf")
|
||||
@Transactional
|
||||
public void load() {
|
||||
logger.info("Loading okopf file");
|
||||
LOGGER.info("Loading okopf file");
|
||||
try {
|
||||
String data = esnsiOkopfClient.getJsonOkopFormData();
|
||||
if (hasText(data)) {
|
||||
logger.info("Parsing from json file to okopf model");
|
||||
LOGGER.info("Parsing from json file to okopf model");
|
||||
OkopfOrgModel orgModel = mapper.readValue(data, OkopfOrgModel.class);
|
||||
int currentVersion = mapper.readTree(data).findValue("version").asInt();
|
||||
List<OkopfModel> okopfRecords = mapToOkopfRecords(orgModel.getData(), currentVersion);
|
||||
logger.info("Finished parsing from json file to okopf model");
|
||||
logger.info("Loaded {} okopf records with version {}. Beginning to save okopf data", okopfRecords.size(), currentVersion);
|
||||
LOGGER.info("Finished parsing from json file to okopf model");
|
||||
LOGGER.info("Loaded {} okopf records with version {}. Beginning to save okopf data", okopfRecords.size(), currentVersion);
|
||||
okopfDao.saveOrUpdate(okopfRecords);
|
||||
logger.info("Successfully saved okopf data");
|
||||
LOGGER.info("Successfully saved okopf data");
|
||||
} else {
|
||||
LOGGER.info("Data is empty");
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error("Failed to load okopf data", e);
|
||||
LOGGER.error("Failed to load okopf data", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue