perf: 优化短信发送有异常继续发送,最后合并返回错误

dev
huangfeng 1 week ago
parent e6073cc4e0
commit 638f97317a

@ -32,38 +32,29 @@ public class SmsAdapter {
@Resource @Resource
LwwlkjWeb lwwlkjWeb; LwwlkjWeb lwwlkjWeb;
public void sendSms(Integer type, List<String> iccidList, String content) { public void sendSms(Integer type, List<String> iccidList, String content, List<String> errList) {
String net = ""; if (Constants.NetType_Dahan.equals(type)) {
try { dahanWeb.sendSms(iccidList, content, errList);
if (Constants.NetType_Dahan.equals(type)) { } else if (Constants.NetType_Rabchaser.equals(type)) {
net = "dahan"; List<String> list = new ArrayList<>();
dahanWeb.sendSms(iccidList, content); for (String iccid : iccidList) {
} else if (Constants.NetType_Rabchaser.equals(type)) { list.add(iccid);
net = "rabchaser"; if (list.size() > 9) {
List<String> list = new ArrayList<>(); rabchaserApi.sendSms(list, content, errList);
for (String iccid : iccidList) { list.clear();
list.add(iccid); try {
if (list.size() > 9) { Thread.sleep(500);
rabchaserApi.sendSms(list, content); } catch (InterruptedException ignore) {
list.clear();
try {
Thread.sleep(500);
} catch (InterruptedException ignore) {
}
} }
} }
if (list.size() > 0) {
rabchaserApi.sendSms(list, content);
}
} else if (Constants.NetType_M2M10086.equals(type)) {
net = "m2m10086";
m2m10086Web.sendSms(iccidList, content);
} else if (Constants.NetType_LWWLKJ.equals(type)) {
net = "lwwlkj";
lwwlkjWeb.sendSms(iccidList, content);
} }
} catch (ApiException e) { if (list.size() > 0) {
throw new ApiException(net + e.getMessage()); rabchaserApi.sendSms(list, content, errList);
}
} else if (Constants.NetType_M2M10086.equals(type)) {
m2m10086Web.sendSms(iccidList, content, errList);
} else if (Constants.NetType_LWWLKJ.equals(type)) {
lwwlkjWeb.sendSms(iccidList, content, errList);
} }
} }

@ -65,7 +65,7 @@ public class DahanWeb {
} }
} }
public void sendSms(List<String> iccidList, String content) { public void sendSms(List<String> iccidList, String content, List<String> errList) {
String url = "/cardSendMt"; String url = "/cardSendMt";
try { try {
for (String iccid : iccidList) { for (String iccid : iccidList) {
@ -79,20 +79,20 @@ public class DahanWeb {
try { try {
result = JSONUtil.json2Object(json, HashMap.class); result = JSONUtil.json2Object(json, HashMap.class);
} catch (Exception e) { } catch (Exception e) {
throw new ApiException(json); errList.add(json);
return;
} }
if (result.get("code") != null && "200".equalsIgnoreCase(result.get("code").toString())) { if (result.get("code") != null && "200".equalsIgnoreCase(result.get("code").toString())) {
} else { } else {
String error = String.valueOf(result.get("message")); String err = iccid + result.get("message");
throw new ApiException(error); errList.add(err);
} }
} }
} catch (ApiException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
log.error("发送短信异常.", e); log.error("发送短信异常.", e);
throw new ApiException("发送短信异常," + e.getMessage()); String err = "dahan发送短信异常," + e.getMessage();
errList.add(err);
} }
} }

@ -55,7 +55,7 @@ public class LwwlkjWeb {
} }
} }
public void sendSms(List<String> msisdnList, String content) { public void sendSms(List<String> msisdnList, String content, List<String> errList) {
String url = "/message/send"; String url = "/message/send";
try { try {
String msisdns = msisdnList.stream().collect(Collectors.joining(",")); String msisdns = msisdnList.stream().collect(Collectors.joining(","));
@ -69,18 +69,18 @@ public class LwwlkjWeb {
try { try {
result = JSONUtil.json2Object(json, HashMap.class); result = JSONUtil.json2Object(json, HashMap.class);
} catch (Exception e) { } catch (Exception e) {
throw new ApiException(json); errList.add(json);
return;
} }
if (result.get("code") != null && "success".equalsIgnoreCase(result.get("code").toString())) { if (result.get("code") != null && "success".equalsIgnoreCase(result.get("code").toString())) {
} else { } else {
throw new ApiException(json); errList.add(json);
} }
} catch (ApiException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
log.error("发送短信异常.", e); log.error("发送短信异常.", e);
throw new ApiException("发送短信异常," + e.getMessage()); String err = "lwwlkj发送短信异常," + e.getMessage();
errList.add(err);
} }
} }

@ -68,7 +68,7 @@ public class M2m10086Web {
} }
} }
public void sendSms(List<String> iccidList, String content) { public void sendSms(List<String> iccidList, String content, List<String> errList) {
String url = "/sms/smsRecord/saveSmsRecord"; String url = "/sms/smsRecord/saveSmsRecord";
try { try {
for (String iccid : iccidList) { for (String iccid : iccidList) {
@ -84,20 +84,20 @@ public class M2m10086Web {
try { try {
result = JSONUtil.json2Object(json, HashMap.class); result = JSONUtil.json2Object(json, HashMap.class);
} catch (Exception e) { } catch (Exception e) {
throw new ApiException(json); errList.add(json);
return;
} }
if (result.get("code") != null && "0".equalsIgnoreCase(result.get("code").toString())) { if (result.get("code") != null && "0".equalsIgnoreCase(result.get("code").toString())) {
} else { } else {
String error = String.valueOf(result.get("message")); String err = iccid + result.get("message");
throw new ApiException(error); errList.add(err);
} }
} }
} catch (ApiException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
log.error("发送短信异常.", e); log.error("发送短信异常.", e);
throw new ApiException("发送短信异常," + e.getMessage()); String err = "m2m10086发送短信异常," + e.getMessage();
errList.add(err);
} }
} }

@ -34,7 +34,7 @@ public class RabchaserApi {
return map; return map;
} }
public void sendSms(List<String> iccidList, String content) { public void sendSms(List<String> iccidList, String content, List<String> errList) {
String url = "/simCards/sendSms"; String url = "/simCards/sendSms";
try { try {
Map<String, String> headMap = this.signHead(url); Map<String, String> headMap = this.signHead(url);
@ -45,16 +45,17 @@ public class RabchaserApi {
String json = HttpClient.post(baseUrl + url, dataMap, headMap); String json = HttpClient.post(baseUrl + url, dataMap, headMap);
log.info("发送短信完成:" + json); log.info("发送短信完成:" + json);
List<Map> list = JSONUtil.json2Object(json, List.class); List<Map> list = JSONUtil.json2Object(json, List.class);
Map map = list.get(0); for (Map map : list) {
String code = map.get("code").toString(); String code = map.get("code").toString();
if (!"0".equals(code)) { if (!"0".equals(code)) {
throw new ApiException(map.get("message").toString()); String err = map.get("iccid").toString() + map.get("message").toString();
errList.add(err);
}
} }
} catch (ApiException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
log.error("发送短信异常.", e); log.error("发送短信异常.", e);
throw new ApiException("发送短信异常," + e.getMessage()); String err = "rabchaser发送短信异常," + e.getMessage();
errList.add(err);
} }
} }

@ -42,17 +42,21 @@ public class SmsServiceImpl implements SmsService {
this.buildiccidList(dahanList, rabchaserList, m2mList, lkjList, this.buildiccidList(dahanList, rabchaserList, m2mList, lkjList,
model.getTermIdList()); model.getTermIdList());
List<String> errList = new ArrayList<>();
if (!CollectionUtils.isEmpty(dahanList)) { if (!CollectionUtils.isEmpty(dahanList)) {
smsAdapter.sendSms(Constants.NetType_Dahan, dahanList, model.getContent()); smsAdapter.sendSms(Constants.NetType_Dahan, dahanList, model.getContent(), errList);
} }
if (!CollectionUtils.isEmpty(rabchaserList)) { if (!CollectionUtils.isEmpty(rabchaserList)) {
smsAdapter.sendSms(Constants.NetType_Rabchaser, rabchaserList, model.getContent()); smsAdapter.sendSms(Constants.NetType_Rabchaser, rabchaserList, model.getContent(), errList);
} }
if (!CollectionUtils.isEmpty(m2mList)) { if (!CollectionUtils.isEmpty(m2mList)) {
smsAdapter.sendSms(Constants.NetType_M2M10086, m2mList, model.getContent()); smsAdapter.sendSms(Constants.NetType_M2M10086, m2mList, model.getContent(), errList);
} }
if (!CollectionUtils.isEmpty(lkjList)) { if (!CollectionUtils.isEmpty(lkjList)) {
smsAdapter.sendSms(Constants.NetType_LWWLKJ, lkjList, model.getContent()); smsAdapter.sendSms(Constants.NetType_LWWLKJ, lkjList, model.getContent(), errList);
}
if (errList.size() > 0) {
throw new ApiException(String.join("\r\n", errList));
} }
} }

Loading…
Cancel
Save