Commit 40df7acd authored by matianhao's avatar matianhao

[瑞成接口] <fix> 异常处理;判断接口失败条件修改

 - 十.浙江省公安户籍业务办理
 - redis分布式锁独占时间改为2000毫秒
 - 补充判断接口失败重试的条件
parent 97b8680e
......@@ -150,7 +150,7 @@ public class RequestController {
// 处理数值类型避免前端丢失精度
JSONObject body = JSONObject.parseObject(response.getBody());
JSONObject datas = JSONObject.parseObject(body.getString("datas"));
JSONObject datarkxx = datas.getJSONObject("datarkxx");
JSONObject datarkxx = datas == null ? null : datas.getJSONObject("datarkxx");
if (datarkxx != null) {
datarkxx.put("ryid", datarkxx.getString("ryid"));
......
......@@ -14,15 +14,16 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.client.RestTemplate;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static com.mth.requestsecret.constant.Constants.SJJ_DISTRIBUTED_LOCK;
/**
* @author MaTianHao
......@@ -71,7 +72,7 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer {
scheduledTaskRegistrar.addTriggerTask(
() -> System.out.println("定时任务获取requestSecret:" + LocalDateTime.now().toString()),
triggerContext -> {
getRequestSecret();
getRequestSecretWithLock();
// 提前一分钟刷新
Instant instant = Instant.ofEpochMilli(requestSecretEndTime).minus(5, ChronoUnit.MINUTES);
return Date.from(instant);
......@@ -112,4 +113,35 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer {
}
}
/**
* 刷新瑞成接口秘钥
*/
public void getRequestSecretWithLock() {
// 唯一值,用于校验是否为同一个线程的锁
String randomValue = UUID.randomUUID().toString().replace("-", "");
// 获取锁
boolean lock = redisUtil.getLock(SJJ_DISTRIBUTED_LOCK, randomValue, 2000);
if (lock) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
log.info("获取到锁 - 刷新秘钥");
try {
// 刷新秘钥
getRequestSecret();
} finally {
// 释放该线程的锁
redisUtil.releaseLock(SJJ_DISTRIBUTED_LOCK, randomValue);
stopWatch.stop();
log.info("redis刷新秘钥并释放锁花费时间:{}", stopWatch.getTotalTimeSeconds());
}
} else {
log.info("未获取到锁 - 等待2000毫秒");
try {
TimeUnit.MILLISECONDS.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
......@@ -21,8 +21,6 @@ import java.time.ZoneOffset;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static com.mth.requestsecret.constant.Constants.*;
......@@ -195,17 +193,29 @@ public class RestTemplateService {
log.info("api response:{}", responseEntity);
try {
// 接口返回
// 接口返回信息
String code = JSONObject.parseObject(responseEntity.getBody()).getString("code");
String msg = JSONObject.parseObject(responseEntity.getBody()).getString("msg");
String success = JSONObject.parseObject(responseEntity.getBody()).getString("success");
String message = JSONObject.parseObject(responseEntity.getBody()).getString("message");
Integer count = threadLocal.get();
String threadName = Thread.currentThread().getName();
// 大部分接口成功返回码都是 00
boolean successCondition1 = RESPONSE_CODE_00.equals(code);
// 接口:浙江杭州公积金贷款信息查询、浙江杭州公积金缴纳信息查询
boolean successCondition2 = "操作成功".equals(msg);
// 接口:浙江省杭州民政局婚姻信息查询
boolean successCondition3 = "查询成功!".equals(message) && "true".equals(success);
boolean successFlag = successCondition1 || successCondition2 || successCondition3;
// 如非成功返回码,则重新获取秘钥并发送请求,限制重新请求次数两次
if (!RESPONSE_CODE_00.equals(code) && count < 2) {
if (!successFlag && count < 2) {
threadLocal.set(++count);
log.info("线程名称:{},重新请求次数:{}", threadName, count);
// 刷新秘钥
getRequestSecret();
schedulerTask.getRequestSecretWithLock();
// 重新发起调用请求
return commonSendRequest(paramMap, apiMethod);
}
......@@ -215,33 +225,6 @@ public class RestTemplateService {
return responseEntity;
}
/**
* 刷新瑞成接口秘钥
*/
private void getRequestSecret() {
// 唯一值,用于校验是否为同一个线程的锁
String randomValue = UUID.randomUUID().toString().replace("-", "");
// 获取锁
boolean lock = redisUtil.getLock(SJJ_DISTRIBUTED_LOCK, randomValue, 1000);
if (lock) {
log.info("获取到锁 - 刷新秘钥");
try {
// 刷新秘钥
schedulerTask.getRequestSecret();
} finally {
// 释放该线程的锁
redisUtil.releaseLock(SJJ_DISTRIBUTED_LOCK, randomValue);
}
} else {
log.info("未获取到锁 - 等待1000毫秒");
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 招必得平台接口发送请求
......
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