updated token service
This commit is contained in:
@@ -8,7 +8,8 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
import com.main.ipks.service.errors.TokenFetchException;
|
import com.main.ipks.service.errors.TokenFetchException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class TokenService {
|
public class TokenService {
|
||||||
@@ -20,8 +21,12 @@ public class TokenService {
|
|||||||
@Value("${cedge.token.url}")
|
@Value("${cedge.token.url}")
|
||||||
private String tokenUrl;
|
private String tokenUrl;
|
||||||
|
|
||||||
private volatile String cachedToken;
|
// Per-bank cache
|
||||||
private volatile long tokenExpiryTimeMillis;
|
private final Map<String, String> tokenCache = new ConcurrentHashMap<>();
|
||||||
|
private final Map<String, Long> expiryCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private final Object tokenLock = new Object();
|
private final Object tokenLock = new Object();
|
||||||
|
|
||||||
private static final long DEFAULT_TOKEN_TTL_MS = 7L * 60 * 60 * 1000;
|
private static final long DEFAULT_TOKEN_TTL_MS = 7L * 60 * 60 * 1000;
|
||||||
@@ -32,23 +37,32 @@ public class TokenService {
|
|||||||
|
|
||||||
public String getToken(String bankId) {
|
public String getToken(String bankId) {
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
if (cachedToken != null && now < tokenExpiryTimeMillis) {
|
String cachedToken = tokenCache.get(bankId);
|
||||||
|
Long expiry = expiryCache.get(bankId);
|
||||||
|
|
||||||
|
if (cachedToken != null && expiry != null && now < expiry) {
|
||||||
return cachedToken;
|
return cachedToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (tokenLock) {
|
synchronized (tokenLock) {
|
||||||
now = System.currentTimeMillis();
|
now = System.currentTimeMillis();
|
||||||
if (cachedToken != null && now < tokenExpiryTimeMillis) {
|
|
||||||
|
cachedToken = tokenCache.get(bankId);
|
||||||
|
expiry = expiryCache.get(bankId);
|
||||||
|
|
||||||
|
if (cachedToken != null && expiry != null && now < expiry) {
|
||||||
return cachedToken;
|
return cachedToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String url = tokenUrl + "/" + bankId;
|
||||||
|
|
||||||
// Only 2 attempts total
|
// Only 2 attempts total
|
||||||
for (int attempt = 1; attempt <= 2; attempt++) {
|
for (int attempt = 1; attempt <= 2; attempt++) {
|
||||||
try {
|
try {
|
||||||
log.info("Fetching new C-EDGE token (attempt {}/2)", attempt);
|
log.info("Fetching new C-EDGE token (attempt {}/2)", attempt);
|
||||||
|
|
||||||
ResponseEntity<TokenResponse> resp =
|
ResponseEntity<TokenResponse> resp =
|
||||||
restTemplate.getForEntity(tokenUrl + "/" + bankId, TokenResponse.class);
|
restTemplate.getForEntity(url,TokenResponse.class);
|
||||||
|
|
||||||
TokenResponse body = resp.getBody();
|
TokenResponse body = resp.getBody();
|
||||||
|
|
||||||
@@ -79,17 +93,19 @@ public class TokenService {
|
|||||||
throw new TokenFetchException("Token string missing in response");
|
throw new TokenFetchException("Token string missing in response");
|
||||||
}
|
}
|
||||||
|
|
||||||
cachedToken = token;
|
// Store per bankId
|
||||||
tokenExpiryTimeMillis = System.currentTimeMillis() + DEFAULT_TOKEN_TTL_MS;
|
tokenCache.put(bankId, token);
|
||||||
|
expiryCache.put(bankId, now + DEFAULT_TOKEN_TTL_MS);
|
||||||
|
|
||||||
String preview = cachedToken.substring(0, Math.min(12, cachedToken.length())) + "...";
|
|
||||||
log.info("Token fetched successfully. Preview={}", preview);
|
String preview = token.substring(0, Math.min(12, token.length())) + "...";
|
||||||
return cachedToken;
|
log.info("Token fetched successfully for bank {}. Preview={}", bankId, preview);
|
||||||
|
|
||||||
|
return token;
|
||||||
|
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
// RestClientException also comes here because it extends RuntimeException
|
// RestClientException also comes here because it extends RuntimeException
|
||||||
log.warn("Token fetch failed on attempt {}/2: {}", attempt, e.getMessage());
|
log.warn("Token fetch failed for bank {} (attempt {}/2): {}", bankId, attempt, e.getMessage());
|
||||||
|
|
||||||
if (attempt == 1) {
|
if (attempt == 1) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(300); // small delay before retry
|
Thread.sleep(300); // small delay before retry
|
||||||
@@ -101,7 +117,7 @@ public class TokenService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// second attempt failed -> hard fail
|
// second attempt failed -> hard fail
|
||||||
log.error("Token generation failed after 2 attempts", e);
|
log.error("Token generation failed for bank {} after 2 attempts", bankId, e);
|
||||||
if (e instanceof TokenFetchException) {
|
if (e instanceof TokenFetchException) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
@@ -120,8 +136,8 @@ public class TokenService {
|
|||||||
*/
|
*/
|
||||||
public String refreshToken(String bankId) {
|
public String refreshToken(String bankId) {
|
||||||
synchronized (tokenLock) {
|
synchronized (tokenLock) {
|
||||||
cachedToken = null;
|
tokenCache.remove(bankId);
|
||||||
tokenExpiryTimeMillis = 0L;
|
expiryCache.remove(bankId);
|
||||||
}
|
}
|
||||||
return getToken(bankId);
|
return getToken(bankId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,16 +7,20 @@ logging.level.com.main.ipks.service=DEBUG
|
|||||||
|
|
||||||
|
|
||||||
# TOKEN
|
# TOKEN
|
||||||
cedge.token.url=https://142.79.251.107:443/BANCEDGEAPI/api/getToken
|
cedge.token.url=https://nabard.cedgeapiservices.in/BANCEDGEAPI/api/getToken
|
||||||
|
|
||||||
# Api for CIF-Creation
|
# Api for CIF-Creation
|
||||||
cedge.cif.url=https://142.79.251.107:443/BANCEDGEAPI/api/cifCreation
|
cedge.cif.url=https://nabard.cedgeapiservices.in/BANCEDGEAPI/api/cifCreation
|
||||||
|
|
||||||
|
|
||||||
# Api for account creation
|
# Api for account creation
|
||||||
|
|
||||||
# API1
|
# API1
|
||||||
cedge.account.url=https://142.79.251.107:443/BANCEDGEAPI/api/depositsCreateDepositAccount
|
cedge.account.url=https://nabard.cedgeapiservices.in/BANCEDGEAPI/api/depositsCreateDepositAccount
|
||||||
|
|
||||||
# API2
|
# API2
|
||||||
cedge.accountOk.url=https://142.79.251.107:443/BANCEDGEAPI/api/createDepositAccountOk
|
cedge.accountOk.url=https://nabard.cedgeapiservices.in/BANCEDGEAPI/api/createDepositAccountOk
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user