Updated code for maximum retries on login to odessa.

This commit is contained in:
rajesh
2025-05-13 19:39:11 +05:30
parent a4a4942634
commit 43ff10e7b6

View File

@@ -176,6 +176,11 @@ public class AppointmentDao {
} }
private HubEntity loginToOdessa(HubEntity hub, ApplicationEntity application) { private HubEntity loginToOdessa(HubEntity hub, ApplicationEntity application) {
int maxRetries = 3;
int attempt = 0;
boolean success = false;
while (attempt < maxRetries && !success) {
try { try {
//code to generate token with payload having "iat" epoch timestamp and secret key with no expiry and send in below method call //code to generate token with payload having "iat" epoch timestamp and secret key with no expiry and send in below method call
String authJwtToken = Utils.generateAuthTokenForLoginToOdessa(); String authJwtToken = Utils.generateAuthTokenForLoginToOdessa();
@@ -195,15 +200,16 @@ public class AppointmentDao {
hub.setAreaCode(parsedResponse.getAreaCode()); hub.setAreaCode(parsedResponse.getAreaCode());
hubRepository.save(hub); hubRepository.save(hub);
log.info("Saved new authToken and areaCode for Hub."); log.info("Saved new authToken and areaCode for Hub.");
success = true;
return hub; return hub;
} else { } else {
throw new RuntimeException("Login response is missing a valid tokenId for login to odessa system, please try again."); throw new RuntimeException("Login response is missing a valid tokenId for login to odessa system, please try again.");
} }
} }
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.ERROR_IN_GENERATING_NDG_TRY_AGAIN)); throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.ERROR_IN_GENERATING_NDG_TRY_AGAIN));
} } catch (FeignException.Forbidden forbiddenException) {
catch (FeignException.Forbidden forbiddenException) { attempt++;
logForbiddenError(); log.error("Failed to login to odessa due to some error");
// Extract raw response body // Extract raw response body
String responseBody = forbiddenException.contentUTF8(); // Extract raw JSON response String responseBody = forbiddenException.contentUTF8(); // Extract raw JSON response
@@ -239,15 +245,17 @@ public class AppointmentDao {
} catch (IOException e) { } catch (IOException e) {
log.error("Error parsing JSON response: {}", e.getMessage()); log.error("Error parsing JSON response: {}", e.getMessage());
} }
if (attempt >= maxRetries) {
// Regenerate the token and retry throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale("Maximum retry attempts reached while trying to login to Odessa."));
loginToOdessa(hub, application); } else {
} loginToOdessa(hub, application);
catch (Exception e) { }
} catch (Exception e) {
log.error("Failed to authenticate user on Odessa : {}", e.getMessage(), e); log.error("Failed to authenticate user on Odessa : {}", e.getMessage(), e);
throw new RuntimeException("Authentication failed on Odessa. try again", e); throw new RuntimeException("Authentication failed on Odessa. try again", e);
} }
return null; }
return null;
} }
private void startAsyncNdgProcessing(Long applicationId) { private void startAsyncNdgProcessing(Long applicationId) {