调整邮件发送问题

This commit is contained in:
wenc000 2020-07-31 23:44:11 +08:00
parent 3eae0c46b5
commit 2005b31ae9
17 changed files with 397 additions and 37 deletions

View File

@ -2,7 +2,10 @@ package com.cm.central.control.client.socket.startup;
import com.cm.central.control.client.socket.socket.SocketClientRunnable;
import com.cm.socket.pojo.SocketClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@ -20,11 +23,19 @@ import org.springframework.stereotype.Component;
@Component
public class SocketClientStartUp implements ApplicationRunner {
private static final Logger LOG = LoggerFactory.getLogger(SocketClientStartUp.class);
@Value("${socket.central-control.active:false}")
private Boolean socketServerActive;
@Autowired
private SocketClientRunnable socketClientRunnable;
@Override
public void run(ApplicationArguments args) throws Exception {
new Thread(socketClientRunnable).start();
if (socketServerActive != null && socketServerActive) {
new Thread(socketClientRunnable).start();
} else {
LOG.debug("Socket监控未激活");
}
}
}

View File

@ -21,7 +21,7 @@ public class SocketServerProperties {
private Integer port = 9999;
private Integer soBacklog = 1024;
private Integer readIdleTimeSecond = 10;
private Integer readIdleTimeSecond = 5;
private Integer offlineNoticeMinute = 10;
private String offlineNoticeType = "email";
private Integer offlineNoticeCount = 3;
@ -43,7 +43,7 @@ public class SocketServerProperties {
}
public Integer getReadIdleTimeSecond() {
return readIdleTimeSecond < 0 ? 5 : readIdleTimeSecond;
return readIdleTimeSecond < 5 ? 5 : readIdleTimeSecond;
}
public void setReadIdleTimeSecond(Integer readIdleTimeSecond) {
@ -51,7 +51,7 @@ public class SocketServerProperties {
}
public Integer getOfflineNoticeMinute() {
return offlineNoticeMinute == null || offlineNoticeMinute < 0 ? 10 : offlineNoticeMinute;
return offlineNoticeMinute == null || offlineNoticeMinute < 1 ? 10 : offlineNoticeMinute;
}
public void setOfflineNoticeMinute(Integer offlineNoticeMinute) {
@ -67,7 +67,7 @@ public class SocketServerProperties {
}
public Integer getOfflineNoticeCount() {
return offlineNoticeCount == null || offlineNoticeCount < 0 ? 3 : offlineNoticeCount;
return offlineNoticeCount == null || offlineNoticeCount < 3 ? 3 : offlineNoticeCount;
}
public void setOfflineNoticeCount(Integer offlineNoticeCount) {

View File

@ -1,5 +1,6 @@
package com.cm.central.control.dao.projectleader;
import com.cm.central.control.pojo.dtos.projectleader.ClientProjectLeaderDTO;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
@ -38,11 +39,11 @@ public interface IProjectLeaderDao {
void removeProjectLeader(Map<String, Object> params) throws RemoveException;
/**
* 删除项目负责人物理
*
* @param params
* @throws RemoveException
*/
* 删除项目负责人物理
*
* @param params
* @throws RemoveException
*/
void deleteProjectLeader(Map<String, Object> params) throws RemoveException;
/**
@ -72,12 +73,20 @@ public interface IProjectLeaderDao {
List<ProjectLeaderDTO> listProjectLeader(Map<String, Object> params) throws SearchException;
/**
* 项目负责人统计
*
* @param params
* @return
* @throws SearchException
*/
* 项目负责人统计
*
* @param params
* @return
* @throws SearchException
*/
Integer countProjectLeader(Map<String, Object> params) throws SearchException;
/**
* 获取项目负责人通过客户端ID
*
* @param clientId
* @return
* @throws SearchException
*/
ClientProjectLeaderDTO getProjectLeaderByClientId(String clientId) throws SearchException;
}

View File

@ -90,7 +90,7 @@ public class SocketServerManager {
public void removeOnlineClient(String token) {
SocketClientBO socketClientBO = getOnlineClientByToken(token);
if (socketClientBO != null) {
// 加入离线队列
// 加入离线队列清空通知队列
offLineClientMap.put(socketClientBO.getClientId(), socketClientBO);
}
onLineClientMap.remove(token);
@ -125,6 +125,11 @@ public class SocketServerManager {
return false;
}
/**
* 离线客户端列表
*
* @return
*/
public List<SocketClientBO> listOffLineClient() {
List<SocketClientBO> socketClientBOs = new ArrayList<>();
for (Map.Entry<String, SocketClientBO> kv : offLineClientMap.entrySet()) {

View File

@ -0,0 +1,44 @@
package com.cm.central.control.pojo.bos;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: ClientOffLineNoticeBO
* @Description: 客户端离线
* @Author: WangGeng
* @Date: 2020/7/31 8:37 下午
* @Version: 1.0
**/
public class ClientOffLineNoticeBO {
private String emailId;
private long sendTime;
public String getEmailId() {
return emailId == null ? "" : emailId.trim();
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public long getSendTime() {
return sendTime;
}
public void setSendTime(long sendTime) {
this.sendTime = sendTime;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"emailId\":")
.append("\"").append(emailId).append("\"");
sb.append(",\"sendTime\":")
.append(sendTime);
sb.append('}');
return sb.toString();
}
}

View File

@ -3,6 +3,8 @@ package com.cm.central.control.pojo.bos;
import io.netty.channel.Channel;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* When you feel like quitting. Think about why you started
@ -20,7 +22,7 @@ public class SocketClientBO implements Serializable {
private String clientId;
private Long lastTime;
private Channel channel;
private Integer emailNoticeCount = 0;
public String getClientId() {
return clientId == null ? "" : clientId.trim();
@ -45,4 +47,27 @@ public class SocketClientBO implements Serializable {
public void setChannel(Channel channel) {
this.channel = channel;
}
public Integer getEmailNoticeCount() {
return emailNoticeCount == null || emailNoticeCount < 0 ? 0 : emailNoticeCount;
}
public void setEmailNoticeCount(Integer emailNoticeCount) {
this.emailNoticeCount = emailNoticeCount;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"clientId\":")
.append("\"").append(clientId).append("\"");
sb.append(",\"lastTime\":")
.append(lastTime);
sb.append(",\"channel\":")
.append(channel);
sb.append(",\"emailNoticeCount\":")
.append(emailNoticeCount);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,60 @@
package com.cm.central.control.pojo.dtos.projectleader;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
*
* @ClassName: ProjectLeaderDTO
* @Description: 项目负责人
* @Author: WenG
* @Date: 2020-07-30 19:04
* @Version: 1.0
**/
@ApiModel
public class ClientProjectLeaderDTO extends ProjectLeaderDTO {
@ApiModelProperty(name = "clientName", value = "名称")
private String clientName;
@ApiModelProperty(name = "customerName", value = "客户名称")
private String customerName;
@ApiModelProperty(name = "serverIp", value = "服务器地址")
private String serverIp;
public String getClientName() {
return clientName == null ? "" : clientName.trim();
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public String getCustomerName() {
return customerName == null ? "" : customerName.trim();
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getServerIp() {
return serverIp == null ? "" : serverIp.trim();
}
public void setServerIp(String serverIp) {
this.serverIp = serverIp;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"clientName\":")
.append("\"").append(clientName).append("\"");
sb.append(",\"customerName\":")
.append("\"").append(customerName).append("\"");
sb.append(",\"serverIp\":")
.append("\"").append(serverIp).append("\"");
sb.append('}');
return sb.toString();
}
}

View File

@ -218,10 +218,4 @@ public interface IClientService {
*/
ClientBO getClientBOByClientId(String clientId) throws SearchException;
/**
* 检查离线客户端并发送邮件
*
* @throws Exception
*/
void checkOfflineClientAndSendEmail() throws Exception;
}

View File

@ -270,9 +270,4 @@ public class ClientServiceImpl extends AbstractService implements IClientService
return clientDao.getClientBO(params);
}
@Override
public void checkOfflineClientAndSendEmail() throws Exception {
}
}

View File

@ -1,14 +1,14 @@
package com.cm.central.control.service.projectleader;
import com.cm.central.control.pojo.dtos.projectleader.ClientProjectLeaderDTO;
import com.cm.central.control.pojo.dtos.projectleader.ProjectLeaderDTO;
import com.cm.central.control.pojo.vos.projectleader.ProjectLeaderVO;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.central.control.pojo.dtos.projectleader.ProjectLeaderDTO;
import com.cm.central.control.pojo.vos.projectleader.ProjectLeaderVO;
import java.util.List;
import java.util.Map;
@ -153,4 +153,12 @@ public interface IProjectLeaderService {
*/
SuccessResultData<Integer> countProjectLeader(Map<String, Object> params) throws SearchException;
/**
* 获取项目负责人
*
* @param clientId
* @return
* @throws SearchException
*/
ClientProjectLeaderDTO getProjectLeaderByClientId(String clientId) throws SearchException;
}

View File

@ -1,5 +1,6 @@
package com.cm.central.control.service.projectleader.impl;
import com.cm.central.control.pojo.dtos.projectleader.ClientProjectLeaderDTO;
import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SaveException;
@ -185,4 +186,9 @@ public class ProjectLeaderServiceImpl extends AbstractService implements IProjec
return new SuccessResultData<>(countNumberProjectLeader(params));
}
@Override
public ClientProjectLeaderDTO getProjectLeaderByClientId(String clientId) throws SearchException {
return projectLeaderDao.getProjectLeaderByClientId(clientId);
}
}

View File

@ -0,0 +1,23 @@
package com.cm.central.control.service.socket.offline;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: IOffLineService
* @Description: 离线业务
* @Author: WangGeng
* @Date: 2020/7/31 8:51 下午
* @Version: 1.0
**/
public interface IOffLineService {
String NOTICE_TYPE_EMAIL = "email";
String NOTICE_TYPE_SMS = "sms";
/**
* 检查离线客户端并发送邮件
* @throws Exception
*/
void checkOfflineClientAndSendEmail() throws Exception;
}

View File

@ -0,0 +1,131 @@
package com.cm.central.control.service.socket.offline.impl;
import com.alibaba.druid.util.StringUtils;
import com.cm.central.control.config.properties.SocketServerProperties;
import com.cm.central.control.manager.SocketServerManager;
import com.cm.central.control.pojo.bos.ClientOffLineNoticeBO;
import com.cm.central.control.pojo.bos.SocketClientBO;
import com.cm.central.control.pojo.dtos.projectleader.ClientProjectLeaderDTO;
import com.cm.central.control.service.BaseService;
import com.cm.central.control.service.client.IClientService;
import com.cm.central.control.service.projectleader.IProjectLeaderService;
import com.cm.central.control.service.socket.offline.IOffLineService;
import com.cm.common.constants.ISystemConstant;
import com.cm.manager.sms.pojo.vos.email.EmailVO;
import com.cm.manager.sms.service.IEmailService;
import com.cm.manager.sms.service.ISmsService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: OffLineServiceImpl
* @Description: 离线业务
* @Author: WangGeng
* @Date: 2020/7/31 8:51 下午
* @Version: 1.0
**/
@Service
public class OffLineServiceImpl extends BaseService implements IOffLineService {
@Autowired
private SocketServerProperties socketServerProperties;
@Autowired
private IClientService clientService;
@Autowired
private IProjectLeaderService projectLeaderService;
@Autowired
private IEmailService emailService;
@Autowired
private ISmsService smsService;
@Override
public void checkOfflineClientAndSendEmail() throws Exception {
// 获取当前离线的客户端
List<SocketClientBO> socketClientBOs = SocketServerManager.getInstance().listOffLineClient();
if (socketClientBOs.isEmpty()) {
LOG.debug("当前无离线客户端");
return;
}
boolean canNotice = StringUtils.equalsIgnoreCase(NOTICE_TYPE_EMAIL, socketServerProperties.getOfflineNoticeType());
if (!canNotice) {
LOG.debug("通知类型设置错误目前只支持email");
return;
}
Map<String, List<String>> noticeMap = new HashMap<>(16);
// 构建通知通知内容
for (SocketClientBO socketClientBO : socketClientBOs) {
int emailNoticeCount = socketClientBO.getEmailNoticeCount();
if (emailNoticeCount >= socketServerProperties.getOfflineNoticeCount()) {
// 超过通知次数的不再通知
continue;
}
if (System.currentTimeMillis() - socketClientBO.getLastTime() <= socketServerProperties.getOfflineNoticeMinute() * 60000) {
// 没有超过提醒时间的不通知
continue;
}
// 查询客户端负责人
ClientProjectLeaderDTO clientProjectLeaderDTO = projectLeaderService.getProjectLeaderByClientId(socketClientBO.getClientId());
if (clientProjectLeaderDTO == null) {
LOG.debug("ClientId: {},没有设置项目负责人");
continue;
}
String emailKey = clientProjectLeaderDTO.getLeaderEmail();
StringBuilder noticeContentSB = new StringBuilder();
noticeContentSB.append("<tr><td>").append(clientProjectLeaderDTO.getClientName()).append("</td><td>")
.append(clientProjectLeaderDTO.getCustomerName()).append("</td><td>").append(clientProjectLeaderDTO.getServerIp())
.append("</td><td>").append(new DateTime(socketClientBO.getLastTime()).toString(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD_HH_MM_SS))
.append("</td></tr>");
List<String> noticeList = noticeMap.get(emailKey);
if (noticeList == null) {
noticeList = new ArrayList<>();
noticeMap.put(emailKey, noticeList);
}
noticeList.add(noticeContentSB.toString());
socketClientBO.setEmailNoticeCount(emailNoticeCount + 1);
}
sendNoticeEmail(noticeMap);
}
/**
* 发送邮件
*
* @param noticeMap
* @throws Exception
*/
private void sendNoticeEmail(Map<String, List<String>> noticeMap) throws Exception {
if (noticeMap.isEmpty()) {
LOG.debug("无离线通知邮件发送");
return;
}
for (Map.Entry<String, List<String>> notice : noticeMap.entrySet()) {
String receiverEmail = notice.getKey();
StringBuilder noticeContentTable = new StringBuilder();
noticeContentTable.append("<div>您好,您所负责的项目出现异常离线情况,请及时处理,如果是主动离线请忽略</div><br/>");
noticeContentTable.append("<div>详细情况见下表:</div><br/>");
noticeContentTable.append("<table border=\"1\" cellspacing=\"0\">");
noticeContentTable.append("<thead><tr><th>系统</th><th>所属客户</th><th>服务器IP</th><th>离线时间</th></thread>");
noticeContentTable.append("<tbody>");
for (String tbodyTr : notice.getValue()) {
noticeContentTable.append(tbodyTr);
}
noticeContentTable.append("</tbody>");
noticeContentTable.append("</table>");
EmailVO emailVO = new EmailVO();
emailVO.setEmailReceiver(receiverEmail);
emailVO.setEmailSubject("项目离线通知");
emailVO.setEmailContent(noticeContentTable.toString());
emailVO.setEmailType("2");
emailService.saveAndSendSystemEmail(emailVO);
}
}
}

View File

@ -1,9 +1,13 @@
package com.cm.central.control.startup;
import com.cm.central.control.service.client.IClientService;
import com.cm.central.control.service.socket.offline.IOffLineService;
import com.cm.central.control.socket.SocketServerRunnable;
import com.cm.common.token.app.AppTokenManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.EnableScheduling;
@ -24,19 +28,25 @@ import org.springframework.stereotype.Component;
@EnableScheduling
public class SocketServerStartUp implements ApplicationRunner {
private static final Logger LOG = LoggerFactory.getLogger(SocketServerStartUp.class);
@Autowired
private IClientService clientService;
private IOffLineService offLineService;
@Autowired
private SocketServerRunnable socketServerRunnable;
@Value("${socket.central-control.active:false}")
private Boolean socketServerActive;
@Override
public void run(ApplicationArguments args) throws Exception {
new Thread(socketServerRunnable).start();
if (socketServerActive != null && socketServerActive) {
new Thread(socketServerRunnable).start();
} else {
LOG.debug("Socket监控未激活");
}
}
@Scheduled(cron = "0 0/1 * * * ?")
public void checkOfflineClientEmail() throws Exception {
clientService.checkOfflineClientAndSendEmail();
offLineService.checkOfflineClientAndSendEmail();
}
}

View File

@ -9,6 +9,12 @@
<result column="leader_email" property="leaderEmail"/>
</resultMap>
<resultMap id="clientProjectLeaderDTO" type="com.cm.central.control.pojo.dtos.projectleader.ClientProjectLeaderDTO" extends="projectLeaderDTO">
<result column="client_name" property="clientName"/>
<result column="customer_name" property="customerName"/>
<result column="server_ip" property="serverIp"/>
</resultMap>
<!-- 新增项目负责人 -->
<insert id="saveProjectLeader" parameterType="map">
INSERT INTO central_project_leader(
@ -143,4 +149,34 @@
t1.is_delete = 0
</select>
<!-- 获取项目负责人通过客户端ID -->
<select id="getProjectLeaderByClientId" parameterType="java.lang.String" resultMap="clientProjectLeaderDTO">
SELECT
t1.client_name,
t1.server_ip,
t2.leader_name,
t2.leader_phone,
t2.leader_email,
t2.project_leader_id,
t3.customer_name
FROM
central_client t1
LEFT JOIN
central_project_leader t2
ON
t2.project_leader_id = t1.project_leader_id
AND
t1.is_delete = 0
LEFT JOIN
central_customer t3
ON
t1.customer_id = t3.customer_id
AND
t3.is_delete = 0
WHERE
t2.is_delete = 0
AND
t1.client_id = #{_parameter}
</select>
</mapper>

View File

@ -126,7 +126,7 @@ public class SimpleMailSenderUtil {
mailInfo.setUserName("dpdbd001@163.com");
mailInfo.setPassword("WAASUQNNVITVXDJS");
mailInfo.setFromAddress("dpdbd001@163.com");
mailInfo.setToAddress("450292408@qq.com ");
mailInfo.setToAddress("450292408@qq.com");
mailInfo.setSubject("123");
mailInfo.setContent("123");
SimpleMailSenderUtil simpleMailSenderUtil = new SimpleMailSenderUtil();

View File

@ -9,6 +9,7 @@ import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.cm.common.utils.email.SimpleMailSenderUtil;
import com.cm.manager.sms.config.properties.EmailProperties;
import com.cm.manager.sms.dao.email.IEmailDao;
@ -64,8 +65,10 @@ public class EmailServiceImpl extends AbstractService implements IEmailService {
if (!sendSuccess) {
LOG.error("邮件发送失败");
}
emailVO.setEmailSender(emailProperties.getSenderEmail());
emailVO.setSendingStatus(sendSuccess ? 1 : 0);
Map<String, Object> params = HashMapUtil.beanToMap(emailVO);
params.put("emailId", UUIDUtil.getUUID());
setSaveInfoByUserId(params, "1");
emailDao.saveEmail(params);
return new SuccessResult();