|
|
@@ -1,23 +1,16 @@
|
|
|
package cn.reghao.devops.manager.notification.service;
|
|
|
|
|
|
-import cn.reghao.devops.manager.notification.db.query.DingAccountQuery;
|
|
|
-import cn.reghao.devops.manager.notification.db.query.NotifyGroupQuery;
|
|
|
-import cn.reghao.devops.manager.notification.model.po.DingAccount;
|
|
|
-import cn.reghao.devops.manager.notification.model.po.NotifyGroup;
|
|
|
-import cn.reghao.devops.manager.notification.model.po.NotifyType;
|
|
|
+import cn.reghao.devops.manager.config.WebhookProperties;
|
|
|
import cn.reghao.devops.manager.notification.service.notifier.Notify;
|
|
|
import cn.reghao.devops.manager.notification.service.notifier.ding.DingMsg;
|
|
|
-import cn.reghao.devops.manager.notification.service.notifier.ding.DingNotify;
|
|
|
-import cn.reghao.jutil.jdk.http.WebRequest;
|
|
|
import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
|
|
|
-import cn.reghao.jutil.tool.http.DefaultWebRequest;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.BeanFactory;
|
|
|
+import org.springframework.beans.factory.BeanFactoryAware;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
/**
|
|
|
@@ -28,77 +21,36 @@ import java.util.concurrent.ExecutorService;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
-public class NotifyService {
|
|
|
+public class NotifyService implements BeanFactoryAware {
|
|
|
+ private BeanFactory beanFactory;
|
|
|
private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("notify-service");
|
|
|
- private final Map<NotifyGroup, Notify> notifierMap = new HashMap<>();
|
|
|
- private final WebRequest webRequest;
|
|
|
- private final NotifyGroupQuery notifyGroupQuery;
|
|
|
- private final DingAccountQuery dingAccountQuery;
|
|
|
+ private final WebhookProperties webhookProperties;
|
|
|
+ private final Notify notify;
|
|
|
|
|
|
- public NotifyService(NotifyGroupQuery notifyGroupQuery, DingAccountQuery dingAccountQuery) {
|
|
|
- this.webRequest = new DefaultWebRequest();
|
|
|
- this.notifyGroupQuery = notifyGroupQuery;
|
|
|
- this.dingAccountQuery = dingAccountQuery;
|
|
|
+ public NotifyService(WebhookProperties webhookProperties, Notify notify) {
|
|
|
+ this.webhookProperties = webhookProperties;
|
|
|
+ this.notify = notify;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * TODO 系统启动时初始化所有的通知组,这应该推迟到通知组第一次使用时才初始化
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2021-06-23 下午9:47
|
|
|
- */
|
|
|
- @PostConstruct
|
|
|
- private void initNotifier() throws UnsupportedEncodingException {
|
|
|
- for (NotifyGroup group : notifyGroupQuery.findAll()) {
|
|
|
- addNotifier(group);
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
|
|
+ this.beanFactory = beanFactory;
|
|
|
}
|
|
|
|
|
|
- public void addNotifier(NotifyGroup notifyGroup) throws UnsupportedEncodingException {
|
|
|
- Notify notify = notifierMap.get(notifyGroup);
|
|
|
- if (notify != null) {
|
|
|
- return;
|
|
|
- }
|
|
|
+ public <T> void notify(T msg) {
|
|
|
+ /*Object object = beanFactory.getBean("webhookProperties");
|
|
|
+ if (object instanceof WebhookProperties) {
|
|
|
+ WebhookProperties webhookProperties = (WebhookProperties) object;
|
|
|
+ DingMsg dingMsg = (DingMsg) msg;
|
|
|
+ String receiver = webhookProperties.getUrl();
|
|
|
+ threadPool.execute(new NotifyTask<DingMsg>(notify, receiver, dingMsg));
|
|
|
+ }*/
|
|
|
|
|
|
- String notifyType = notifyGroup.getNotifyType();
|
|
|
- String notifyAccountId = notifyGroup.getNotifyAccountId();
|
|
|
- switch (NotifyType.valueOf(notifyType)) {
|
|
|
- case ding:
|
|
|
- DingAccount dingAccount = dingAccountQuery.findByNotifyAccountId(notifyAccountId);
|
|
|
- notifierMap.put(notifyGroup, new DingNotify(webRequest, dingAccount));
|
|
|
- break;
|
|
|
- default:
|
|
|
- log.error("{} 通知类型暂未实现", notifyType);
|
|
|
- }
|
|
|
+ DingMsg dingMsg = (DingMsg) msg;
|
|
|
+ String receiver = webhookProperties.getUrl();
|
|
|
+ threadPool.execute(new NotifyTask<DingMsg>(notify, receiver, dingMsg));
|
|
|
}
|
|
|
-
|
|
|
- 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);
|
|
|
- }
|
|
|
- DingMsg dingMsg = (DingMsg) msg;
|
|
|
- String receiver = notifyGroup.getReceiver();
|
|
|
- threadPool.execute(new NotifyTask<DingMsg>(notify, receiver, dingMsg));
|
|
|
- break;
|
|
|
- default:
|
|
|
- log.error("{} 通知类型暂未实现", notifyType);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
- * TODO 添加通知日志
|
|
|
- *
|
|
|
* @param
|
|
|
* @return
|
|
|
* @date 2021-06-23 上午9:29
|