Commit 37ae5c79 authored by matianhao's avatar matianhao

[瑞成接口] <fix> 注释重试机制和秘钥过期时间判断

parent 0fcc0c3c
...@@ -71,7 +71,7 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer { ...@@ -71,7 +71,7 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer {
triggerContext -> { triggerContext -> {
getRequestSecret(); getRequestSecret();
// 提前一分钟刷新 // 提前一分钟刷新
Instant instant = Instant.ofEpochMilli(requestSecretEndTime).minus(1, ChronoUnit.MINUTES); Instant instant = Instant.ofEpochMilli(requestSecretEndTime).minus(5, ChronoUnit.MINUTES);
return Date.from(instant); return Date.from(instant);
} }
); );
...@@ -105,11 +105,11 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer { ...@@ -105,11 +105,11 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer {
requestSecretEndTime = Long.parseLong(datas.getString("requestSecretEndTime")); requestSecretEndTime = Long.parseLong(datas.getString("requestSecretEndTime"));
refreshSecret = datas.getString("refreshSecret"); refreshSecret = datas.getString("refreshSecret");
refreshSecretEndTime = Long.parseLong(datas.getString("refreshSecretEndTime")); refreshSecretEndTime = Long.parseLong(datas.getString("refreshSecretEndTime"));
// 设置过期时间,30秒内不重新刷新秘钥 // // 设置过期时间,30秒内不重新刷新秘钥
String expireTime = String.valueOf(requestSecretEndTime - (14 * 60 * 1000 + 30 * 1000)); // String expireTime = String.valueOf(requestSecretEndTime - (14 * 60 * 1000 + 30 * 1000));
// redis保存 请求秘钥 和 过期时间 // redis保存 请求秘钥 和 过期时间
redisUtil.set(Constants.SJJ_REQUEST_SECRET_PREFIX, requestSecret); redisUtil.set(Constants.SJJ_REQUEST_SECRET_PREFIX, requestSecret);
redisUtil.set(Constants.SJJ_REQUEST_EXPIRE_TIME_PREFIX, expireTime); // redisUtil.set(Constants.SJJ_REQUEST_EXPIRE_TIME_PREFIX, expireTime);
} }
} }
......
...@@ -13,14 +13,11 @@ import org.springframework.util.LinkedMultiValueMap; ...@@ -13,14 +13,11 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import static com.mth.requestsecret.constant.Constants.*; import static com.mth.requestsecret.constant.Constants.SJJ_REQUEST_SECRET_PREFIX;
/** /**
* @author fyl * @author fyl
...@@ -111,32 +108,32 @@ public class RestTemplateService { ...@@ -111,32 +108,32 @@ public class RestTemplateService {
ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, request, String.class); ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, request, String.class);
log.info("api response:{}", responseEntity); log.info("api response:{}", responseEntity);
try { // try {
// 接口返回码 // // 接口返回码
String code = JSONObject.parseObject(responseEntity.getBody()).getString("code"); // String code = JSONObject.parseObject(responseEntity.getBody()).getString("code");
Integer count = threadLocal.get(); // Integer count = threadLocal.get();
String threadName = Thread.currentThread().getName(); // String threadName = Thread.currentThread().getName();
//
// 如非成功返回码,则重新获取秘钥并发送请求,限制重新请求次数 // // 如非成功返回码,则重新获取秘钥并发送请求,限制重新请求次数
if (!RESPONSE_CODE_00.equals(code) && count < REQUEST_LIMIT) { // if (!RESPONSE_CODE_00.equals(code) && count < REQUEST_LIMIT) {
threadLocal.set(++count); // threadLocal.set(++count);
log.info("线程名称:{},重新请求次数:{}", threadName, count); // log.info("线程名称:{},重新请求次数:{}", threadName, count);
// redis 中获取过期时间 // // redis 中获取过期时间
String expireTimeStr = redisUtil.get(SJJ_REQUEST_EXPIRE_TIME_PREFIX); // String expireTimeStr = redisUtil.get(SJJ_REQUEST_EXPIRE_TIME_PREFIX);
LocalDateTime expireTime = Instant.ofEpochMilli(Long.parseLong(expireTimeStr)).atZone(ZoneOffset.ofHours(8)).toLocalDateTime(); // LocalDateTime expireTime = Instant.ofEpochMilli(Long.parseLong(expireTimeStr)).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
LocalDateTime now = LocalDateTime.now(); // LocalDateTime now = LocalDateTime.now();
// 当前时间超过过期时间,重新请求秘钥,否则重新发送请求 // // 当前时间超过过期时间,重新请求秘钥,否则重新发送请求
if (now.isAfter(expireTime)) { // if (now.isAfter(expireTime)) {
log.info("redis中过期时间:{},当前时间:{}", expireTime, now); // log.info("redis中过期时间:{},当前时间:{}", expireTime, now);
// 重新获取秘钥 // // 重新获取秘钥
schedulerTask.getRequestSecret(); // schedulerTask.getRequestSecret();
} // }
// 重新调用请求 // // 重新调用请求
return commonSendRequest(paramMap, apiMethod); // return commonSendRequest(paramMap, apiMethod);
} // }
} finally { // } finally {
threadLocal.remove(); // threadLocal.remove();
} // }
return responseEntity; return responseEntity;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment