Commit 96db4109 authored by 赵啸非's avatar 赵啸非

添加消息发送系统

parent 8a4b7790
...@@ -46,7 +46,8 @@ ...@@ -46,7 +46,8 @@
<profiles.active>test</profiles.active> <profiles.active>test</profiles.active>
<profiles.nacos.server-addr>192.168.0.252:8848</profiles.nacos.server-addr> <profiles.nacos.server-addr>192.168.0.252:8848</profiles.nacos.server-addr>
<profiles.trustedReferer>192.168.0.98,localhost,192.168.0.252</profiles.trustedReferer> <profiles.trustedReferer>192.168.0.98,localhost,192.168.0.252</profiles.trustedReferer>
<profiles.req.json.check>false</profiles.req.json.check> <profiles.sms.smsSendUrl>http://sms.wx3.com.cn</profiles.sms.smsSendUrl>
<profiles.sms.apiId>ADsUXLrS81vZDU95</profiles.sms.apiId>
</properties> </properties>
</profile> </profile>
...@@ -55,6 +56,8 @@ ...@@ -55,6 +56,8 @@
<properties> <properties>
<profiles.active>product</profiles.active> <profiles.active>product</profiles.active>
<profiles.nacos.server-addr>127.0.0.1:8848</profiles.nacos.server-addr> <profiles.nacos.server-addr>127.0.0.1:8848</profiles.nacos.server-addr>
<profiles.sms.smsSendUrl>http://sms.wx3.com.cn</profiles.sms.smsSendUrl>
<profiles.sms.apiId>ADsUXLrS81vZDU95</profiles.sms.apiId>
</properties> </properties>
</profile> </profile>
<profile> <profile>
...@@ -115,6 +118,16 @@ ...@@ -115,6 +118,16 @@
</properties> </properties>
</profile> </profile>
<profile>
<id>qionglai</id>
<properties>
<profiles.active>qionglai</profiles.active>
<profiles.nacos.server-addr>127.0.0.1:8848</profiles.nacos.server-addr>
<profiles.sms.smsSendUrl>http://sms.wx3.com.cn</profiles.sms.smsSendUrl>
<profiles.sms.apiId>ADsUXLrS81vZDU95</profiles.sms.apiId>
</properties>
</profile>
</profiles> </profiles>
......
...@@ -9,6 +9,7 @@ import com.mortals.xhx.common.utils.CipherUtil; ...@@ -9,6 +9,7 @@ import com.mortals.xhx.common.utils.CipherUtil;
import com.mortals.xhx.common.utils.SmsQueueManager; import com.mortals.xhx.common.utils.SmsQueueManager;
import com.mortals.xhx.module.message.model.MessageTaskEntity; import com.mortals.xhx.module.message.model.MessageTaskEntity;
import com.mortals.xhx.module.message.service.MessageTaskService; import com.mortals.xhx.module.message.service.MessageTaskService;
import com.mortals.xhx.thread.AlarmSendMsgThread;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
...@@ -49,12 +50,18 @@ public class DemoStartedService implements IApplicationStartedService { ...@@ -49,12 +50,18 @@ public class DemoStartedService implements IApplicationStartedService {
@Autowired @Autowired
private MessageTaskService messageTaskService; private MessageTaskService messageTaskService;
@Value("${sms.smsSendUrl:http://127.0.0.1:8089/api/index/index}")
private String smsSendUrl;
@Value("${sms.apiId:''}")
private String apiId;
protected Boolean stopped = false; protected Boolean stopped = false;
@Override @Override
public void start() { public void start() {
if(!ObjectUtils.isEmpty(trustedReferer)){ if (!ObjectUtils.isEmpty(trustedReferer)) {
cacheService.set(RedisKey.KEY_REFERERS_CACHE,trustedReferer); cacheService.set(RedisKey.KEY_REFERERS_CACHE, trustedReferer);
} }
//获取网卡并封装信息 //获取网卡并封装信息
...@@ -96,6 +103,10 @@ public class DemoStartedService implements IApplicationStartedService { ...@@ -96,6 +103,10 @@ public class DemoStartedService implements IApplicationStartedService {
} }
}); });
AlarmSendMsgThread alarmSendMsgThread = new AlarmSendMsgThread(smsSendUrl, apiId);
ThreadPool.getInstance().execute(alarmSendMsgThread);
log.info("开始服务..[配置已加载完成,并且所有框架都已经初始化]"); log.info("开始服务..[配置已加载完成,并且所有框架都已经初始化]");
} }
......
package com.mortals.xhx.thread; package com.mortals.xhx.thread;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.util.AbstractThread; import com.mortals.framework.util.AbstractThread;
import com.mortals.framework.util.ThreadPool; import com.mortals.framework.util.ThreadPool;
import com.mortals.xhx.busiz.req.SmsThirdPartyReq; import com.mortals.xhx.busiz.req.SmsThirdPartyReq;
import com.mortals.xhx.common.code.SendStatusEnum; import com.mortals.xhx.common.code.SendStatusEnum;
import com.mortals.xhx.common.code.YesNoEnum; import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.key.QueueKey;
import com.mortals.xhx.common.utils.SmsQueueManager; import com.mortals.xhx.common.utils.SmsQueueManager;
import com.mortals.xhx.module.alarm.model.AlarmRecordsEntity;
import com.mortals.xhx.module.alarm.model.AlarmRecordsQuery;
import com.mortals.xhx.module.alarm.service.AlarmRecordsService;
import com.mortals.xhx.module.message.model.MessageTaskEntity; import com.mortals.xhx.module.message.model.MessageTaskEntity;
import com.mortals.xhx.module.message.model.MessageTaskQuery; import com.mortals.xhx.module.message.model.MessageTaskQuery;
import com.mortals.xhx.module.message.service.MessageTaskService; import com.mortals.xhx.module.message.service.MessageTaskService;
import com.mortals.xhx.queue.*;
import com.mortals.xhx.utils.SpringUtils; import com.mortals.xhx.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -41,22 +33,21 @@ public class AlarmSendMsgThread extends AbstractThread { ...@@ -41,22 +33,21 @@ public class AlarmSendMsgThread extends AbstractThread {
private RabbitTemplate rabbitTemplate; private RabbitTemplate rabbitTemplate;
@Value("${sms.smsSendUrl:http://127.0.0.1:8089/api/index/index}")
private String smsSendUrl; private String smsSendUrl;
@Value("${sms.apiId:''}")
private String apiId; private String apiId;
public AlarmSendMsgThread(String smsSendUrl, String apiId) {
public AlarmSendMsgThread() { messageTaskService = SpringUtils.getBean(MessageTaskService.class);
messageTaskService =SpringUtils.getBean(MessageTaskService.class);
rabbitTemplate = SpringUtils.getBean(RabbitTemplate.class); rabbitTemplate = SpringUtils.getBean(RabbitTemplate.class);
this.smsSendUrl = smsSendUrl;
this.apiId = apiId;
} }
@Override @Override
protected int getSleepTime() { protected int getSleepTime() {
return 60*1000; return 30 * 1000;
} }
@Override @Override
...@@ -73,7 +64,7 @@ public class AlarmSendMsgThread extends AbstractThread { ...@@ -73,7 +64,7 @@ public class AlarmSendMsgThread extends AbstractThread {
SmsThirdPartyReq smsThirdPartyReq = new SmsThirdPartyReq(); SmsThirdPartyReq smsThirdPartyReq = new SmsThirdPartyReq();
smsThirdPartyReq.setAppid(apiId); smsThirdPartyReq.setAppid(apiId);
//模板号 //模板号
smsThirdPartyReq.setType(messageTask.getMessageConfigEntity().getTemplateId()+""); smsThirdPartyReq.setType(messageTask.getMessageConfigEntity().getTemplateId() + "");
smsThirdPartyReq.setAlarmSmsSendId(messageTask.getId()); smsThirdPartyReq.setAlarmSmsSendId(messageTask.getId());
smsThirdPartyReq.setPhone(messageTask.getRecipient()); smsThirdPartyReq.setPhone(messageTask.getRecipient());
String[] json = new String[1]; String[] json = new String[1];
......
package com.mortals.xhx.thread; package com.mortals.xhx.thread;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.util.HttpUtil;
import com.mortals.xhx.busiz.req.SmsThirdPartyReq; import com.mortals.xhx.busiz.req.SmsThirdPartyReq;
import com.mortals.xhx.busiz.rsp.ApiSmsResp; import com.mortals.xhx.busiz.rsp.ApiSmsResp;
import com.mortals.xhx.common.code.SendStatusEnum; import com.mortals.xhx.common.code.SendStatusEnum;
...@@ -11,11 +12,8 @@ import com.mortals.xhx.module.message.model.MessageTaskEntity; ...@@ -11,11 +12,8 @@ import com.mortals.xhx.module.message.model.MessageTaskEntity;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static com.mortals.framework.util.HttpUtil.HEADER_CONTENT_TYPE;
/** /**
* 短信发送任务 * 短信发送任务
* *
...@@ -43,22 +41,23 @@ public class SendSmsTask implements Runnable { ...@@ -43,22 +41,23 @@ public class SendSmsTask implements Runnable {
MessageTaskEntity messageTaskEntityResp = new MessageTaskEntity(); MessageTaskEntity messageTaskEntityResp = new MessageTaskEntity();
messageTaskEntityResp.setId(smsThirdPartyReq.getAlarmSmsSendId()); messageTaskEntityResp.setId(smsThirdPartyReq.getAlarmSmsSendId());
try { try {
Map<String, String> header = new HashMap<>(); Map<String, Object> form = BeanUtil.beanToMap(smsThirdPartyReq, false, true);
header.put(HEADER_CONTENT_TYPE, "application/json"); log.info("sms req sendUrl:{} params:{}", sendUrl, JSON.toJSONString(form));
resp = HttpUtil.doPost(sendUrl, header, JSON.toJSONString(smsThirdPartyReq)); resp = HttpUtil.post(sendUrl, form);
ApiSmsResp<Void> apiSmsResp = JSON.parseObject(resp, new TypeReference<ApiSmsResp<Void>>() { ApiSmsResp<Void> apiSmsResp = JSON.parseObject(resp, new TypeReference<ApiSmsResp<Void>>() {
}); });
if (apiSmsResp.getCode() == 1) { if (apiSmsResp.getCode() == 1) {
messageTaskEntityResp.setSendStatus(SendStatusEnum.成功.getValue()); messageTaskEntityResp.setSendStatus(SendStatusEnum.成功.getValue());
} else { } else {
messageTaskEntityResp.setSendStatus(SendStatusEnum.成功.getValue()); messageTaskEntityResp.setSendStatus(SendStatusEnum.失败.getValue());
messageTaskEntityResp.setErrorMsg(apiSmsResp.getMessage());
log.info("短信提交失败,req:{}, resp:{}", JSON.toJSONString(smsThirdPartyReq), resp); log.info("短信提交失败,req:{}, resp:{}", JSON.toJSONString(smsThirdPartyReq), resp);
} }
//发送成功添加到响应队列,由线程批量更新 //发送成功添加到响应队列,由线程批量更新
SmsQueueManager.offerRespQueue(messageTaskEntityResp); SmsQueueManager.offerRespQueue(messageTaskEntityResp);
log.debug("resp:{}", resp);
} catch (Exception e) { } catch (Exception e) {
messageTaskEntityResp.setSendStatus(SendStatusEnum.失败.getValue()); messageTaskEntityResp.setSendStatus(SendStatusEnum.失败.getValue());
messageTaskEntityResp.setErrorMsg(e.getMessage());
SmsQueueManager.offerRespQueue(messageTaskEntityResp); //todo SmsQueueManager.offerRespQueue(messageTaskEntityResp); //todo
log.error("异常:", e); log.error("异常:", 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