refactor: 解耦模板管理器和通知器管理器

This commit is contained in:
椰子 2021-07-10 00:46:09 +08:00
parent cb84a0f86a
commit cb3529ce4c
24 changed files with 63 additions and 148 deletions

View File

@ -1,35 +0,0 @@
package com.cicdi.notify;
import com.cicdi.notify.template.Template;
import com.cicdi.notify.template.TemplateManager;
import java.util.Map;
/**
* 通知器抽象实现
* <p>实现了通用的发送方法还需要具体服务商继承此类实现全部的方法</p>
*
* @author xueye
*/
public abstract class AbstractNotifier<T extends Template> implements Notifier<T> {
/**
* 通知模板管理器用于获取当前通知类型使用的通知模板
*/
private final TemplateManager templateManager;
public AbstractNotifier(TemplateManager templateManager) {
this.templateManager = templateManager;
}
@Override
public void send(String templateId, Map<String, Object> context) {
Template template = templateManager.getTemplate(this.getType(), templateId);
if (null == template) {
throw new UnsupportedOperationException("指定的模版不存在:" + templateId);
}
this.send((T) template, context);
}
}

View File

@ -28,7 +28,7 @@ public abstract class AbstractNotifierManager implements NotifierManager {
@Override
public <T extends Template> Notifier<T> getNotifier(NotifyType type, String id) {
Notifier notifier;
Notifier<T> notifier;
if (null == notifiers.get(id)) {
notifier = createNotifier(getProperties(type, id));
} else {
@ -37,6 +37,24 @@ public abstract class AbstractNotifierManager implements NotifierManager {
return notifier;
}
@Override
public <T extends Template> Notifier<T> createNotifier(NotifierProperties properties) {
Map<String, NotifierProvider> providerMap = providers.get(properties.getType());
if (null == providerMap) {
throw new UnsupportedOperationException("不支持的通知类型: " + properties.getType());
}
NotifierProvider provider = providerMap.get(properties.getProvider());
if (null == provider) {
throw new UnsupportedOperationException("不支持的服务商: " + properties.getProvider());
}
Notifier notifier1 = provider.createNotifier(properties);
Notifier notifier2 = notifiers.put(properties.getId(), notifier1);
if (null != notifier2) {
notifier2.close();
}
return notifier1;
}
@Override
public void register(NotifierProvider provider) {
providers.computeIfAbsent(provider.getType().getId(), ignore -> new ConcurrentHashMap<>())
@ -62,27 +80,4 @@ public abstract class AbstractNotifierManager implements NotifierManager {
return configManager.getNotifyConfig(notifyType, id);
}
/**
* 根据配置创建通知器
*
* @param properties 通知器配置属性
* @return 通知器
*/
protected Notifier createNotifier(NotifierProperties properties) {
Map<String, NotifierProvider> providerMap = providers.get(properties.getType());
if (null == providerMap) {
throw new UnsupportedOperationException("不支持的通知类型: " + properties.getType());
}
NotifierProvider provider = providerMap.get(properties.getProvider());
if (null == provider) {
throw new UnsupportedOperationException("不支持的服务商: " + properties.getProvider());
}
Notifier notifier1 = provider.createNotifier(properties);
Notifier notifier2 = notifiers.put(properties.getId(), notifier1);
if (null != notifier2) {
notifier2.close();
}
return notifier1;
}
}

View File

@ -33,15 +33,6 @@ public interface Notifier<T extends Template> {
*/
Provider getProvider();
/**
* 指定通知模板ID进行发送
* <p>用于一些固定模板的消息发送例如验证码定时消息</p>
*
* @param templateId 通知模板ID
* @param context 模板上下文参数
*/
void send(String templateId, Map<String, Object> context);
/**
* 指定通知模板进行发送
* <p>提供一个通知模板进行消息发送比较灵活适用于一些一次性的消息推送</p>

View File

@ -18,6 +18,16 @@ public interface NotifierManager {
*/
<T extends Template> Notifier<T> getNotifier(NotifyType type, String id);
/**
* 根据配置创建通知器
*
* @param properties 通知器配置属性
* @param <T> 通知模板类型
* @return 通知器
*/
<T extends Template> Notifier<T> createNotifier(NotifierProperties properties);
/**
* 注册通知服务商
* <p>服务商经过注册之后才可以使用, 如果通知模板配置的服务商不在已注册集合中, 将会抛出{@link UnsupportedOperationException}</p>

View File

@ -5,6 +5,7 @@ package com.cicdi.notify;
*
* @author xueye
*/
@FunctionalInterface
public interface NotifyConfigManager {
/**

View File

@ -3,11 +3,10 @@ package com.cicdi.notify.dingtalk;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cicdi.notify.AbstractNotifier;
import com.cicdi.notify.DefaultNotifyType;
import com.cicdi.notify.Notifier;
import com.cicdi.notify.NotifyType;
import com.cicdi.notify.Provider;
import com.cicdi.notify.template.TemplateManager;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
@ -27,7 +26,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
public class DingTalkNotifier extends AbstractNotifier<DingTalkMessageTemplate> {
public class DingTalkNotifier implements Notifier<DingTalkMessageTemplate> {
private final AtomicReference<String> accessToken = new AtomicReference<>();
@ -48,8 +47,7 @@ public class DingTalkNotifier extends AbstractNotifier<DingTalkMessageTemplate>
return notifierId;
}
public DingTalkNotifier(String id, DingTalkProperties properties, TemplateManager templateManager) {
super(templateManager);
public DingTalkNotifier(String id, DingTalkProperties properties) {
this.properties = properties;
this.notifierId = id;
}

View File

@ -2,7 +2,6 @@ package com.cicdi.notify.dingtalk;
import com.alibaba.fastjson.JSON;
import com.cicdi.notify.*;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.template.TemplateProperties;
import com.cicdi.notify.template.TemplateProvider;
@ -11,12 +10,6 @@ import java.util.Objects;
public class DingTalkNotifierProvider implements NotifierProvider, TemplateProvider {
private final TemplateManager templateManager;
public DingTalkNotifierProvider(TemplateManager templateManager) {
this.templateManager = templateManager;
}
// public static final DefaultConfigMetadata notifierConfig = new DefaultConfigMetadata("通知配置", "")
// .add("appKey", "appKey", "", new StringType().expand(ConfigMetadataConstants.required.value(true)))
// .add("appSecret", "appSecret", "", new StringType());
@ -49,7 +42,7 @@ public class DingTalkNotifierProvider implements NotifierProvider, TemplateProvi
DingTalkProperties dingTalkProperties = new DingTalkProperties();
dingTalkProperties.setAppKey((String) Objects.requireNonNull(properties.getConfiguration().get("appKey"), "appKey不能为空"));
dingTalkProperties.setAppSecret((String) Objects.requireNonNull(properties.getConfiguration().get("appSecret"), "appSecret不能为空"));
return new DingTalkNotifier(properties.getId(), dingTalkProperties, templateManager);
return new DingTalkNotifier(properties.getId(), dingTalkProperties);
// return new DingTalkNotifier(properties.getId(), ValidatorUtils.tryValidate(dingTalkProperties), templateManager);
}

View File

@ -76,7 +76,7 @@ public class DingTalkTest {
};
// register
DingTalkNotifierProvider provider = new DingTalkNotifierProvider(templateManager);
DingTalkNotifierProvider provider = new DingTalkNotifierProvider();
notifierManager.register(provider);
templateManager.register(provider);
}

View File

@ -12,7 +12,7 @@ public enum EmailProvider implements Provider {
embedded("默认"),
;
private String name;
private final String name;
EmailProvider(String name) {
this.name = name;

View File

@ -75,7 +75,7 @@ public class EmailTemplateParsed {
public static Builder builder(){
return new Builder();
};
}
public static class Builder {
private final EmailTemplateParsed target;

View File

@ -5,7 +5,6 @@ import com.cicdi.notify.*;
import com.cicdi.notify.email.EmailProvider;
import com.cicdi.notify.email.EmailTemplate;
import com.cicdi.notify.email.EmailTemplateParsed;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.util.ExpressionUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
@ -36,7 +35,7 @@ import java.util.*;
* @author zhouhao
* @since 1.0
**/
public class DefaultEmailNotifier extends AbstractNotifier<EmailTemplate> {
public class DefaultEmailNotifier implements Notifier<EmailTemplate> {
private JavaMailSender javaMailSender;
@ -67,8 +66,7 @@ public class DefaultEmailNotifier extends AbstractNotifier<EmailTemplate> {
// public static Scheduler scheduler = Schedulers.newElastic("email-notifier");
public DefaultEmailNotifier(NotifierProperties properties, TemplateManager templateManager) {
super(templateManager);
public DefaultEmailNotifier(NotifierProperties properties) {
notifierId = properties.getId();
DefaultEmailProperties emailProperties = new JSONObject(properties.getConfiguration()).toJavaObject(DefaultEmailProperties.class);
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

View File

@ -4,18 +4,11 @@ import com.alibaba.fastjson.JSON;
import com.cicdi.notify.*;
import com.cicdi.notify.email.EmailProvider;
import com.cicdi.notify.email.EmailTemplate;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.template.TemplateProperties;
import com.cicdi.notify.template.TemplateProvider;
public class DefaultEmailNotifierProvider implements NotifierProvider, TemplateProvider {
private final TemplateManager templateManager;
public DefaultEmailNotifierProvider(TemplateManager templateManager) {
this.templateManager = templateManager;
}
@Override
public NotifyType getType() {
return DefaultNotifyType.email;
@ -98,7 +91,7 @@ public class DefaultEmailNotifierProvider implements NotifierProvider, TemplateP
@Override
public DefaultEmailNotifier createNotifier(NotifierProperties properties) {
return new DefaultEmailNotifier(properties, templateManager);
return new DefaultEmailNotifier(properties);
}
@Override

View File

@ -70,7 +70,7 @@ public class DefaultEmailTest {
};
NotifierManager notifierManager = new AbstractNotifierManager(notifyConfigManager){};
DefaultEmailNotifierProvider defaultEmailNotifierProvider = new DefaultEmailNotifierProvider(templateManager);
DefaultEmailNotifierProvider defaultEmailNotifierProvider = new DefaultEmailNotifierProvider();
// register
notifierManager.register(defaultEmailNotifierProvider);
templateManager.register(defaultEmailNotifierProvider);

View File

@ -11,17 +11,16 @@ import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.cicdi.notify.*;
import com.cicdi.notify.sms.SmsProvider;
import com.cicdi.notify.template.TemplateManager;
import java.util.Map;
import java.util.Objects;
public class AliyunSmsNotifier extends AbstractNotifier<AliyunSmsTemplate> {
public class AliyunSmsNotifier implements Notifier<AliyunSmsTemplate> {
private final IAcsClient client;
private final int connectTimeout = 1000;
private final int readTimeout = 5000;
private static final int connectTimeout = 1000;
private static final int readTimeout = 5000;
private String notifierId;
@ -32,8 +31,7 @@ public class AliyunSmsNotifier extends AbstractNotifier<AliyunSmsTemplate> {
private String domain = "dysmsapi.aliyuncs.com";
private String regionId = "cn-hangzhou";
public AliyunSmsNotifier(NotifierProperties profile, TemplateManager templateManager) {
super(templateManager);
public AliyunSmsNotifier(NotifierProperties profile) {
Map<String, Object> config = profile.getConfiguration();
DefaultProfile defaultProfile = DefaultProfile.getProfile(
this.regionId = (String) Objects.requireNonNull(config.get("regionId"), "regionId不能为空"),
@ -45,12 +43,11 @@ public class AliyunSmsNotifier extends AbstractNotifier<AliyunSmsTemplate> {
this.notifierId = profile.getId();
}
public AliyunSmsNotifier(IClientProfile profile, TemplateManager templateManager) {
this(new DefaultAcsClient(profile), templateManager);
public AliyunSmsNotifier(IClientProfile profile) {
this(new DefaultAcsClient(profile));
}
public AliyunSmsNotifier(IAcsClient client, TemplateManager templateManager) {
super(templateManager);
public AliyunSmsNotifier(IAcsClient client) {
this.client = client;
}

View File

@ -13,12 +13,6 @@ import com.cicdi.notify.template.TemplateProvider;
*/
public class AliyunSmsNotifierProvider implements NotifierProvider, TemplateProvider {
private final TemplateManager templateManager;
public AliyunSmsNotifierProvider(TemplateManager templateManager) {
this.templateManager = templateManager;
}
@Override
public Provider getProvider() {
return SmsProvider.aliyun;
@ -58,6 +52,6 @@ public class AliyunSmsNotifierProvider implements NotifierProvider, TemplateProv
@Override
public AliyunSmsNotifier createNotifier(NotifierProperties properties) {
return new AliyunSmsNotifier(properties, templateManager);
return new AliyunSmsNotifier(properties);
}
}

View File

@ -21,9 +21,9 @@ import java.util.Map;
*
* @author xueye
*/
public class TelecomSmsNotifier extends AbstractNotifier<TelecomSmsTemplate> {
public class TelecomSmsNotifier implements Notifier<TelecomSmsTemplate> {
private final String notifyApi = "http://www.js139.com.cn:8022/hysms/SendMsg";
private static final String notifyApi = "http://www.js139.com.cn:8022/hysms/SendMsg";
private final TelecomSmsNotifierConfiguration configuration;
@ -31,8 +31,7 @@ public class TelecomSmsNotifier extends AbstractNotifier<TelecomSmsTemplate> {
private final CloseableHttpClient httpClient;
public TelecomSmsNotifier(NotifierProperties properties, TemplateManager templateManager) {
super(templateManager);
public TelecomSmsNotifier(NotifierProperties properties) {
this.httpClient = HttpClientBuilder.create().build();
this.notifierId = properties.getId();
this.configuration = new JSONObject(properties.getConfiguration()).toJavaObject(TelecomSmsNotifierConfiguration.class);

View File

@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON;
import com.cicdi.notify.*;
import com.cicdi.notify.sms.SmsProvider;
import com.cicdi.notify.template.Template;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.template.TemplateProperties;
import com.cicdi.notify.template.TemplateProvider;
@ -13,12 +12,6 @@ import com.cicdi.notify.template.TemplateProvider;
*/
public class TelecomSmsNotifierProvider implements NotifierProvider, TemplateProvider {
private final TemplateManager templateManager;
public TelecomSmsNotifierProvider(TemplateManager templateManager) {
this.templateManager = templateManager;
}
@Override
public NotifyType getType() {
return DefaultNotifyType.sms;
@ -36,6 +29,6 @@ public class TelecomSmsNotifierProvider implements NotifierProvider, TemplatePro
@Override
public Notifier<? extends Template> createNotifier(NotifierProperties properties) {
return new TelecomSmsNotifier(properties, templateManager);
return new TelecomSmsNotifier(properties);
}
}

View File

@ -53,7 +53,7 @@ public class AliyunSmsTest {
};
NotifierManager notifierManager = new AbstractNotifierManager(notifyConfigManager){};
AliyunSmsNotifierProvider aliyunSmsNotifierProvider = new AliyunSmsNotifierProvider(templateManager);
AliyunSmsNotifierProvider aliyunSmsNotifierProvider = new AliyunSmsNotifierProvider();
// register
notifierManager.register(aliyunSmsNotifierProvider);
templateManager.register(aliyunSmsNotifierProvider);

View File

@ -57,7 +57,7 @@ public class TelecomSmsTest {
};
// register
TelecomSmsNotifierProvider telecomSmsNotifierProvider = new TelecomSmsNotifierProvider(templateManager);
TelecomSmsNotifierProvider telecomSmsNotifierProvider = new TelecomSmsNotifierProvider();
notifierManager.register(telecomSmsNotifierProvider);
templateManager.register(telecomSmsNotifierProvider);

View File

@ -2,8 +2,8 @@ package com.cicdi.notify.wechat.corp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cicdi.notify.AbstractNotifier;
import com.cicdi.notify.DefaultNotifyType;
import com.cicdi.notify.Notifier;
import com.cicdi.notify.NotifyType;
import com.cicdi.notify.Provider;
import com.cicdi.notify.template.TemplateManager;
@ -25,7 +25,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class WechatCorpNotifier extends AbstractNotifier<WechatCorpTemplate> {
public class WechatCorpNotifier implements Notifier<WechatCorpTemplate> {
private String accessToken;
@ -41,8 +41,7 @@ public class WechatCorpNotifier extends AbstractNotifier<WechatCorpTemplate> {
private final String notifierId;
public WechatCorpNotifier(String id, WechatCorpProperties properties, TemplateManager templateManager) {
super(templateManager);
public WechatCorpNotifier(String id, WechatCorpProperties properties) {
this.properties = properties;
this.notifierId = id;
}

View File

@ -3,7 +3,6 @@ package com.cicdi.notify.wechat.corp;
import com.alibaba.fastjson.JSON;
import com.cicdi.notify.*;
import com.cicdi.notify.template.Template;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.template.TemplateProperties;
import com.cicdi.notify.template.TemplateProvider;
import com.cicdi.notify.wechat.WechatProvider;
@ -12,12 +11,6 @@ import java.util.Map;
public class WechatCorpNotifierProvider implements NotifierProvider, TemplateProvider {
private final TemplateManager templateManager;
public WechatCorpNotifierProvider(TemplateManager templateManager) {
this.templateManager = templateManager;
}
// public static final DefaultConfigMetadata notifierConfig = new DefaultConfigMetadata("通知配置", "")
// .add("corpId", "corpId", "", new StringType().expand(ConfigMetadataConstants.required.value(true)))
// .add("corpSecret", "corpSecret", "", new StringType());
@ -50,7 +43,7 @@ public class WechatCorpNotifierProvider implements NotifierProvider, TemplatePro
WechatCorpProperties wechatCorpProperties = new WechatCorpProperties();
wechatCorpProperties.setCorpId((String) config.get("corpId"));
wechatCorpProperties.setCorpSecret((String) config.get("corpSecret"));
return new WechatCorpNotifier(properties.getId(), wechatCorpProperties, templateManager);
return new WechatCorpNotifier(properties.getId(), wechatCorpProperties);
}
// @Override

View File

@ -2,10 +2,7 @@ package com.cicdi.notify.wechat.subscription;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.hsf.HSFJSONUtils;
import com.cicdi.notify.*;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.wechat.WechatProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
@ -27,7 +24,7 @@ import java.util.concurrent.atomic.AtomicReference;
*
* @author xueye
*/
public class WechatSubscriptionNotifier extends AbstractNotifier<WechatSubscriptionTemplate> {
public class WechatSubscriptionNotifier implements Notifier<WechatSubscriptionTemplate> {
/**
* 通知器配置信息
@ -62,8 +59,7 @@ public class WechatSubscriptionNotifier extends AbstractNotifier<WechatSubscript
private final CloseableHttpClient httpClient;
public WechatSubscriptionNotifier(NotifierProperties properties, TemplateManager templateManager) {
super(templateManager);
public WechatSubscriptionNotifier(NotifierProperties properties) {
this.properties = properties;
this.httpClient = HttpClientBuilder.create().build();
}

View File

@ -58,7 +58,7 @@ public class WechatSubscriptionTest {
NotifierManager notifierManager = new AbstractNotifierManager(notifyConfigManager){};
// register
WechatCorpNotifierProvider provider = new WechatCorpNotifierProvider(templateManager);
WechatCorpNotifierProvider provider = new WechatCorpNotifierProvider();
notifierManager.register(provider);
templateManager.register(provider);

View File

@ -59,7 +59,7 @@ public class WechatTest {
NotifierManager notifierManager = new AbstractNotifierManager(notifyConfigManager){};
// register
WechatCorpNotifierProvider provider = new WechatCorpNotifierProvider(templateManager);
WechatCorpNotifierProvider provider = new WechatCorpNotifierProvider();
notifierManager.register(provider);
templateManager.register(provider);