updated token service

This commit is contained in:
2026-06-17 18:00:06 +05:30
parent 689be386fb
commit 8b242b81fa
2 changed files with 40 additions and 20 deletions
@@ -8,7 +8,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.main.ipks.service.errors.TokenFetchException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class TokenService {
@@ -20,8 +21,12 @@ public class TokenService {
@Value("${cedge.token.url}")
private String tokenUrl;
private volatile String cachedToken;
private volatile long tokenExpiryTimeMillis;
// Per-bank cache
private final Map<String, String> tokenCache = new ConcurrentHashMap<>();
private final Map<String, Long> expiryCache = new ConcurrentHashMap<>();
private final Object tokenLock = new Object();
private static final long DEFAULT_TOKEN_TTL_MS = 7L * 60 * 60 * 1000;
@@ -32,23 +37,32 @@ public class TokenService {
public String getToken(String bankId) {
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;
}
synchronized (tokenLock) {
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;
}
String url = tokenUrl + "/" + bankId;
// Only 2 attempts total
for (int attempt = 1; attempt <= 2; attempt++) {
try {
log.info("Fetching new C-EDGE token (attempt {}/2)", attempt);
ResponseEntity<TokenResponse> resp =
restTemplate.getForEntity(tokenUrl + "/" + bankId, TokenResponse.class);
restTemplate.getForEntity(url,TokenResponse.class);
TokenResponse body = resp.getBody();
@@ -79,17 +93,19 @@ public class TokenService {
throw new TokenFetchException("Token string missing in response");
}
cachedToken = token;
tokenExpiryTimeMillis = System.currentTimeMillis() + DEFAULT_TOKEN_TTL_MS;
// Store per bankId
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);
return cachedToken;
String preview = token.substring(0, Math.min(12, token.length())) + "...";
log.info("Token fetched successfully for bank {}. Preview={}", bankId, preview);
return token;
} catch (RuntimeException e) {
// 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) {
try {
Thread.sleep(300); // small delay before retry
@@ -101,7 +117,7 @@ public class TokenService {
}
// 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) {
throw e;
}
@@ -120,8 +136,8 @@ public class TokenService {
*/
public String refreshToken(String bankId) {
synchronized (tokenLock) {
cachedToken = null;
tokenExpiryTimeMillis = 0L;
tokenCache.remove(bankId);
expiryCache.remove(bankId);
}
return getToken(bankId);
}
+8 -4
View File
@@ -7,16 +7,20 @@ logging.level.com.main.ipks.service=DEBUG
# 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
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
# API1
cedge.account.url=https://142.79.251.107:443/BANCEDGEAPI/api/depositsCreateDepositAccount
cedge.account.url=https://nabard.cedgeapiservices.in/BANCEDGEAPI/api/depositsCreateDepositAccount
# API2
cedge.accountOk.url=https://142.79.251.107:443/BANCEDGEAPI/api/createDepositAccountOk
cedge.accountOk.url=https://nabard.cedgeapiservices.in/BANCEDGEAPI/api/createDepositAccountOk