记得上下班打卡 | git大法好,push需谨慎

Commit 789755e1 authored by wangyifan's avatar wangyifan

发送短信改成异步

parent d4aba725
......@@ -77,6 +77,12 @@ public class SmsMessage implements Serializable, Cloneable {
return this;
}
/** 无变量模板时传空对象 {},避免 templateParam 为 null */
public SmsMessage setEmptyTemplateParam() {
this.templateParam = JsonUtils.OM().createObjectNode();
return this;
}
public String toJson() {
return JsonUtils.toJson(this);
}
......
package com.liquidnet.service.kylin.utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.common.sms.constant.SmsEnum;
import com.liquidnet.common.sms.processor.SmsProcessor;
import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.SmsMessage;
import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.base.constant.RedisKeyExpireConst;
import com.liquidnet.service.kylin.constant.GroupBuyStatusConst;
import com.liquidnet.service.kylin.constant.KylinRedisConst;
......@@ -29,18 +31,20 @@ import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 拼团短信发送(kylin 侧)。总开关:{@link KylinRedisConst#GROUP_BUY_SMS_ENABLED}
* 拼团短信(kylin):入队 {@link MQConst.KylinQueue#SMS_NOTICE}(与购票短信一致,异步)
*/
@Component
@Slf4j
public class GroupBuySmsSender {
@Autowired
private SmsProcessor smsProcessor;
private QueueUtils queueUtils;
@Autowired
private RedisUtil redisUtil;
@Autowired
......@@ -58,8 +62,8 @@ public class GroupBuySmsSender {
}
ObjectNode params = buildSuccessParams(group);
for (String phone : phones) {
sendSafe(phone, SmsEnum.ADTemplate.SMS_510540063.name(),
params.toString(), group.getGroupOrderId(), "success");
enqueueSms(phone, SmsEnum.ADTemplate.SMS_510540063.name(), params,
group.getGroupOrderId(), "success");
}
}
......@@ -74,8 +78,8 @@ public class GroupBuySmsSender {
ObjectNode params = JsonUtils.OM().createObjectNode();
params.put("hours", resolveHours(group));
for (String phone : phones) {
sendSafe(phone, SmsEnum.ADTemplate.SMS_510615071.name(),
params.toString(), group.getGroupOrderId(), "failed");
enqueueSms(phone, SmsEnum.ADTemplate.SMS_510615071.name(), params,
group.getGroupOrderId(), "failed");
}
}
......@@ -87,10 +91,10 @@ public class GroupBuySmsSender {
if (phones.isEmpty()) {
return;
}
String templateParam = JsonUtils.OM().createObjectNode().toString();
ObjectNode empty = JsonUtils.OM().createObjectNode();
for (String phone : phones) {
sendSafe(phone, SmsEnum.ADTemplate.SMS_510285138.name(),
templateParam, group.getGroupOrderId(), "remind1h");
enqueueSms(phone, SmsEnum.ADTemplate.SMS_510285138.name(), empty,
group.getGroupOrderId(), "remind1h");
}
redisUtil.set(KylinRedisConst.GROUP_BUY_REMIND_1H.concat(group.getGroupOrderId()), "1",
RedisKeyExpireConst.KYLIN_GROUP_BUY_EXPIRE);
......@@ -197,14 +201,24 @@ public class GroupBuySmsSender {
return "24";
}
private void sendSafe(String phone, String templateCode, String templateParam, String groupOrderId, String scene) {
private void enqueueSms(String phone, String templateCode, ObjectNode params, String groupOrderId, String scene) {
try {
boolean ok = smsProcessor.send(phone, SmsEnum.ADSignName.M02.getVal(), templateCode, templateParam);
if (!ok) {
log.warn("拼团短信发送失败 scene={} groupOrderId={} phone={}", scene, groupOrderId, phone);
}
SmsMessage msg = SmsMessage.builder()
.setPhone(phone)
.setSignName(SmsEnum.ADSignName.M02.getVal())
.setTemplateCode(templateCode);
if (params == null || params.size() == 0) {
msg.setEmptyTemplateParam();
} else {
Iterator<Map.Entry<String, JsonNode>> it = params.fields();
while (it.hasNext()) {
Map.Entry<String, JsonNode> e = it.next();
msg.setTemplateParam(e.getKey(), e.getValue() == null ? "" : e.getValue().asText(""));
}
}
queueUtils.sendMsgByRedis(MQConst.KylinQueue.SMS_NOTICE.getKey(), msg.toJson());
} catch (Exception e) {
log.error("拼团短信发送异常 scene={} groupOrderId={} phone={}", scene, groupOrderId, phone, e);
log.error("拼团短信入队异常 scene={} groupOrderId={} phone={}", scene, groupOrderId, phone, e);
}
}
}
......@@ -3,8 +3,9 @@ package com.liquidnet.service.order.utils;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.common.sms.constant.SmsEnum;
import com.liquidnet.common.sms.processor.SmsProcessor;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.SmsMessage;
import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.kylin.constant.GroupBuyStatusConst;
import com.liquidnet.service.kylin.constant.KylinRedisConst;
import com.liquidnet.service.kylin.dto.vo.groupbuy.KylinGroupBuyActivityVo;
......@@ -25,18 +26,20 @@ import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 拼团短信:成功。复用 SmsProcessor;总开关见 Redis {@link KylinRedisConst#GROUP_BUY_SMS_ENABLED}
* 拼团成功短信:入队 {@link MQConst.KylinQueue#SMS_NOTICE}(与购票短信一致,异步)
*/
@Component
@Slf4j
public class GroupBuySmsSender {
@Autowired
private SmsProcessor smsProcessor;
private QueueUtils queueUtils;
@Autowired
private RedisUtil redisUtil;
@Autowired
......@@ -54,9 +57,8 @@ public class GroupBuySmsSender {
return;
}
ObjectNode params = buildSuccessParams(group);
String templateParam = params.toString();
for (String phone : phones) {
sendSafe(phone, SmsEnum.ADTemplate.SMS_510540063.name(), templateParam,
enqueueSms(phone, SmsEnum.ADTemplate.SMS_510540063.name(), params,
group.getGroupOrderId(), "success");
}
}
......@@ -70,9 +72,6 @@ public class GroupBuySmsSender {
return !("0".equals(s) || "false".equalsIgnoreCase(s));
}
/**
* @param joinedOnly true=仅 JOINED;false 且 includePendingPay=true 时含 JOINED+PENDING_PAY
*/
private List<String> collectPhones(KylinGroupBuyOrderVo group, boolean joinedOnly, boolean includePendingPay) {
Set<String> phones = new HashSet<>();
if (group == null || CollectionUtils.isEmpty(group.getMembers())) {
......@@ -164,14 +163,24 @@ public class GroupBuySmsSender {
return new String[]{timeTitle, ticketTitle};
}
private void sendSafe(String phone, String templateCode, String templateParam, String groupOrderId, String scene) {
private void enqueueSms(String phone, String templateCode, ObjectNode params, String groupOrderId, String scene) {
try {
boolean ok = smsProcessor.send(phone, SmsEnum.ADSignName.M02.getVal(), templateCode, templateParam);
if (!ok) {
log.warn("拼团短信发送失败 scene={} groupOrderId={} phone={}", scene, groupOrderId, phone);
}
SmsMessage msg = SmsMessage.builder()
.setPhone(phone)
.setSignName(SmsEnum.ADSignName.M02.getVal())
.setTemplateCode(templateCode);
if (params == null || params.size() == 0) {
msg.setEmptyTemplateParam();
} else {
Iterator<Map.Entry<String, com.fasterxml.jackson.databind.JsonNode>> it = params.fields();
while (it.hasNext()) {
Map.Entry<String, com.fasterxml.jackson.databind.JsonNode> e = it.next();
msg.setTemplateParam(e.getKey(), e.getValue() == null ? "" : e.getValue().asText(""));
}
}
queueUtils.sendMsgByRedis(MQConst.KylinQueue.SMS_NOTICE.getKey(), msg.toJson());
} catch (Exception e) {
log.error("拼团短信发送异常 scene={} groupOrderId={} phone={}", scene, groupOrderId, phone, e);
log.error("拼团短信入队异常 scene={} groupOrderId={} phone={}", scene, groupOrderId, phone, e);
}
}
}
package com.liquidnet.service.platform.utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.common.sms.constant.SmsEnum;
import com.liquidnet.common.sms.processor.SmsProcessor;
import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.SmsMessage;
import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.base.constant.RedisKeyExpireConst;
import com.liquidnet.service.kylin.constant.GroupBuyStatusConst;
import com.liquidnet.service.kylin.constant.KylinRedisConst;
......@@ -25,18 +27,20 @@ import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 拼团短信:失败 / 1h 提醒。复用 SmsProcessor;总开关 Redis {@link KylinRedisConst#GROUP_BUY_SMS_ENABLED}
* 拼团失败 / 1h 提醒短信:入队 {@link MQConst.KylinQueue#SMS_NOTICE}(与购票短信一致,异步)
*/
@Component
@Slf4j
public class GroupBuySmsSender {
@Autowired
private SmsProcessor smsProcessor;
private QueueUtils queueUtils;
@Autowired
private RedisUtil redisUtil;
@Autowired
......@@ -53,9 +57,8 @@ public class GroupBuySmsSender {
}
ObjectNode params = JsonUtils.OM().createObjectNode();
params.put("hours", resolveHours(group));
String templateParam = params.toString();
for (String phone : phones) {
sendSafe(phone, SmsEnum.ADTemplate.SMS_510615071.name(), templateParam,
enqueueSms(phone, SmsEnum.ADTemplate.SMS_510615071.name(), params,
group.getGroupOrderId(), "failed");
}
}
......@@ -69,9 +72,9 @@ public class GroupBuySmsSender {
log.info("拼团1h提醒短信无有效手机号 groupOrderId={}", group.getGroupOrderId());
return;
}
String templateParam = JsonUtils.OM().createObjectNode().toString();
ObjectNode empty = JsonUtils.OM().createObjectNode();
for (String phone : phones) {
sendSafe(phone, SmsEnum.ADTemplate.SMS_510285138.name(), templateParam,
enqueueSms(phone, SmsEnum.ADTemplate.SMS_510285138.name(), empty,
group.getGroupOrderId(), "remind1h");
}
redisUtil.set(KylinRedisConst.GROUP_BUY_REMIND_1H.concat(group.getGroupOrderId()), "1",
......@@ -157,14 +160,24 @@ public class GroupBuySmsSender {
return "24";
}
private void sendSafe(String phone, String templateCode, String templateParam, String groupOrderId, String scene) {
private void enqueueSms(String phone, String templateCode, ObjectNode params, String groupOrderId, String scene) {
try {
boolean ok = smsProcessor.send(phone, SmsEnum.ADSignName.M02.getVal(), templateCode, templateParam);
if (!ok) {
log.warn("拼团短信发送失败 scene={} groupOrderId={} phone={}", scene, groupOrderId, phone);
SmsMessage msg = SmsMessage.builder()
.setPhone(phone)
.setSignName(SmsEnum.ADSignName.M02.getVal())
.setTemplateCode(templateCode);
if (params == null || params.size() == 0) {
msg.setEmptyTemplateParam();
} else {
Iterator<Map.Entry<String, JsonNode>> it = params.fields();
while (it.hasNext()) {
Map.Entry<String, JsonNode> e = it.next();
msg.setTemplateParam(e.getKey(), e.getValue() == null ? "" : e.getValue().asText(""));
}
}
queueUtils.sendMsgByRedis(MQConst.KylinQueue.SMS_NOTICE.getKey(), msg.toJson());
} catch (Exception e) {
log.error("拼团短信发送异常 scene={} groupOrderId={} phone={}", scene, groupOrderId, phone, e);
log.error("拼团短信入队异常 scene={} groupOrderId={} phone={}", scene, groupOrderId, phone, e);
}
}
}
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