|
|
@@ -2,10 +2,13 @@ package cn.reghao.autodop.dmaster.notification.service;
|
|
|
|
|
|
import cn.reghao.autodop.common.http.DefaultWebRequest;
|
|
|
import cn.reghao.autodop.common.http.WebRequest;
|
|
|
+import cn.reghao.autodop.dmaster.notification.entity.EmailAccount;
|
|
|
import cn.reghao.autodop.dmaster.notification.entity.NotifyType;
|
|
|
import cn.reghao.autodop.dmaster.notification.entity.NotifyGroup;
|
|
|
import cn.reghao.autodop.dmaster.common.thread.ThreadPoolWrapper;
|
|
|
+import cn.reghao.autodop.dmaster.notification.entity.SmsAccount;
|
|
|
import cn.reghao.autodop.dmaster.notification.repository.EmailAccountRepository;
|
|
|
+import cn.reghao.autodop.dmaster.notification.repository.NotifyGroupRepository;
|
|
|
import cn.reghao.autodop.dmaster.notification.repository.SmsAccountRepository;
|
|
|
import cn.reghao.autodop.dmaster.notification.service.notifier.Notify;
|
|
|
import cn.reghao.autodop.dmaster.notification.service.notifier.ding.DingMsg;
|
|
|
@@ -16,6 +19,7 @@ import cn.reghao.autodop.dmaster.notification.service.notifier.sms.SmsNotify;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
@@ -30,46 +34,128 @@ import java.util.concurrent.ExecutorService;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class NotifyService {
|
|
|
- private Map<String, Notify> notifyMap = new HashMap<>();
|
|
|
private ExecutorService threadPool = ThreadPoolWrapper.threadPool("notify-service");
|
|
|
- private DingNotify dingNotify;
|
|
|
- private SmsNotify smsNotify;
|
|
|
- private EmailNotify emailNotify;
|
|
|
+ private Map<NotifyGroup, Notify> notifierMap = new HashMap<>();
|
|
|
+ private WebRequest webRequest;
|
|
|
+ private NotifyGroupRepository groupRepository;
|
|
|
+ private EmailAccountRepository emailRepository;
|
|
|
+ private SmsAccountRepository smsRepository;
|
|
|
|
|
|
- public NotifyService(SmsAccountRepository smsRepository, EmailAccountRepository emailRepository)
|
|
|
- throws UnsupportedEncodingException {
|
|
|
- WebRequest webRequest = new DefaultWebRequest();
|
|
|
- this.dingNotify = new DingNotify(webRequest);
|
|
|
- /*this.smsNotify = new SmsNotify(webRequest, new SmsAccount());
|
|
|
- this.emailNotify = new EmailNotify(new EmailAccount());*/
|
|
|
+ public NotifyService(NotifyGroupRepository groupRepository,
|
|
|
+ EmailAccountRepository emailRepository,
|
|
|
+ SmsAccountRepository smsRepository) {
|
|
|
+ this.webRequest = new DefaultWebRequest();
|
|
|
+ this.groupRepository = groupRepository;
|
|
|
+ this.emailRepository = emailRepository;
|
|
|
+ this.smsRepository = smsRepository;
|
|
|
}
|
|
|
|
|
|
- public void notify(NotifyGroup notifyGroup, Object msg) {
|
|
|
- // TODO 调用 NotifyType.valueOf() 没有指定类型时会抛出异常
|
|
|
- switch (NotifyType.valueOf(notifyGroup.getNotifyType())) {
|
|
|
+ /**
|
|
|
+ * TODO 系统启动时初始化所有的通知组,这应该推迟到通知组第一次使用时才初始化
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2021-06-23 下午9:47
|
|
|
+ */
|
|
|
+ @PostConstruct
|
|
|
+ private void initNotifier() throws UnsupportedEncodingException {
|
|
|
+ for (NotifyGroup group : groupRepository.findAll()) {
|
|
|
+ addNotifier(group);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addNotifier(NotifyGroup notifyGroup) throws UnsupportedEncodingException {
|
|
|
+ Notify notify = notifierMap.get(notifyGroup);
|
|
|
+ if (notify != null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String notifyType = notifyGroup.getNotifyType();
|
|
|
+ String notifyAccountId = notifyGroup.getNotifyAccountId();
|
|
|
+ switch (NotifyType.valueOf(notifyType)) {
|
|
|
case ding:
|
|
|
- if (msg instanceof DingMsg) {
|
|
|
- DingMsg dingMsg = (DingMsg) msg;
|
|
|
- //threadPool.execute(new NotifyTask<>(dingNotify, notifyReceiver.getReceiver(), dingMsg));
|
|
|
+ notifierMap.put(notifyGroup, new DingNotify(webRequest));
|
|
|
+ break;
|
|
|
+ case email:
|
|
|
+ EmailAccount emailAccount = emailRepository.findEmailAccountByNotifyAccountId(notifyAccountId);
|
|
|
+ if (emailAccount != null) {
|
|
|
+ notifierMap.put(notifyGroup, new EmailNotify(emailAccount));
|
|
|
}
|
|
|
break;
|
|
|
+ case sms:
|
|
|
+ SmsAccount smsAccount = smsRepository.findSmsAccountByNotifyAccountId(notifyAccountId);
|
|
|
+ if (smsAccount != null) {
|
|
|
+ notifierMap.put(notifyGroup, new SmsNotify(webRequest, smsAccount));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ log.error("通知类型不存在...");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> void notify(NotifyGroup notifyGroup, T msg) {
|
|
|
+ Notify notify = notifierMap.get(notifyGroup);
|
|
|
+ if (notify == null) {
|
|
|
+ log.error("类型为 {}, 账户为 {} 的通知发送器不存在...",
|
|
|
+ notifyGroup.getNotifyType(), notifyGroup.getNotifyAccountId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String notifyType = notifyGroup.getNotifyType();
|
|
|
+ switch (NotifyType.valueOf(notifyType)) {
|
|
|
+ case ding:
|
|
|
+ if (!(msg instanceof DingMsg)) {
|
|
|
+ log.error("{} 消息格式不正确, 不是 DingMsg...", msg);
|
|
|
+ }
|
|
|
+ notifyGroup.getReceivers()
|
|
|
+ .forEach(receiver -> threadPool.execute(new NotifyTask(notify, receiver, msg)));
|
|
|
case email:
|
|
|
- if (msg instanceof EmailMsg) {
|
|
|
- EmailMsg emailMsg = (EmailMsg) msg;
|
|
|
- //threadPool.execute(new NotifyTask<>(emailNotify, notifyReceiver.getReceiver(), emailMsg));
|
|
|
+ if (!(msg instanceof EmailMsg)) {
|
|
|
+ log.error("{} 消息格式不正确, 不是 EmailMsg...", msg);
|
|
|
}
|
|
|
+ notifyGroup.getReceivers()
|
|
|
+ .forEach(receiver -> threadPool.execute(new NotifyTask(notify, receiver, msg)));
|
|
|
break;
|
|
|
case sms:
|
|
|
- if (msg instanceof String) {
|
|
|
- String smsMsg = (String) msg;
|
|
|
- //threadPool.execute(new NotifyTask<>(smsNotify, notifyReceiver.getReceiver(), smsMsg));
|
|
|
+ if (!(msg instanceof String)) {
|
|
|
+ log.error("{} 消息格式不正确, 不是 SMS 消息...", msg);
|
|
|
}
|
|
|
+ notifyGroup.getReceivers()
|
|
|
+ .forEach(receiver -> threadPool.execute(new NotifyTask(notify, receiver, msg)));
|
|
|
break;
|
|
|
default:
|
|
|
log.error("通知类型不存在...");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /*public void notify(NotifyGroup notifyGroup, String msg) {
|
|
|
+ Notify notify = notifierMap.get(notifyGroup);
|
|
|
+ if (notify == null) {
|
|
|
+ log.error("类型为 {}, 账户为 {} 的通知发送器不存在...",
|
|
|
+ notifyGroup.getNotifyType(), notifyGroup.getNotifyAccountId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String notifyType = notifyGroup.getNotifyType();
|
|
|
+ switch (NotifyType.valueOf(notifyType)) {
|
|
|
+ case ding:
|
|
|
+ DingMsg dingMsg = new DingMsg(msg);
|
|
|
+ notifyGroup.getReceivers()
|
|
|
+ .forEach(receiver -> threadPool.execute(new NotifyTask(notify, receiver, dingMsg)));
|
|
|
+ case email:
|
|
|
+ EmailMsg emailMsg = new EmailMsg(notifyGroup.getGroupId(), msg);
|
|
|
+ notifyGroup.getReceivers()
|
|
|
+ .forEach(receiver -> threadPool.execute(new NotifyTask(notify, receiver, emailMsg)));
|
|
|
+ break;
|
|
|
+ case sms:
|
|
|
+ notifyGroup.getReceivers()
|
|
|
+ .forEach(receiver -> threadPool.execute(new NotifyTask(notify, receiver, msg)));
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ log.error("通知类型不存在...");
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+
|
|
|
/**
|
|
|
* TODO 添加通知日志
|
|
|
*
|
|
|
@@ -93,7 +179,7 @@ public class NotifyService {
|
|
|
try {
|
|
|
notify.send(receiver, msg);
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("发送给 {} 的通知失败 -> {}", receiver, e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
}
|