mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 17:08:40 +08:00
缓存部分3.0实现
This commit is contained in:
parent
e7cbf70fdd
commit
f4827da735
@ -38,7 +38,7 @@ public class SmsDaoDefaultImpl implements SmsDao {
|
||||
*
|
||||
* @return 唯一实例
|
||||
*/
|
||||
public SmsDaoDefaultImpl getInstance() {
|
||||
public static SmsDaoDefaultImpl getInstance() {
|
||||
if (null == INSTANCE) {
|
||||
synchronized (SmsDaoDefaultImpl.class) {
|
||||
if (null == INSTANCE) {
|
||||
|
||||
@ -7,16 +7,10 @@ package org.dromara.sms4j.comm.enumerate;
|
||||
* 2023/4/5 19:08
|
||||
**/
|
||||
public enum ConfigType {
|
||||
/** 配置文件*/
|
||||
CONFIG_FILE("configFile"),
|
||||
/** setting配置文件*/
|
||||
SETTINGS_FILE("settingsFile"),
|
||||
|
||||
/** 数据库配置*/
|
||||
SQL_CONFIG("sqlConfig"),
|
||||
|
||||
/** nacos配置*/
|
||||
NACOS_CONFIG("nacosConfig");
|
||||
/** yaml配置文件 */
|
||||
YAML("yaml"),
|
||||
/** 接口 */
|
||||
INTERFACE("interface");
|
||||
|
||||
private final String name;
|
||||
|
||||
|
||||
@ -1,151 +0,0 @@
|
||||
package org.dromara.sms4j.comm.utils;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.dromara.sms4j.comm.config.SmsSqlConfig;
|
||||
import org.dromara.sms4j.comm.exception.SmsSqlException;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* JDBCTool
|
||||
* <p> 数据库相关工具
|
||||
*
|
||||
* @author :Wind
|
||||
* 2023/4/4 22:34
|
||||
**/
|
||||
public class JDBCTool {
|
||||
|
||||
public static final String SELECT = "select {},{} from {} where {} = {}";
|
||||
private static final String ERR_CONFIG = "One configuration was expected, but {} was found. Please check your database configuration";
|
||||
private final SmsSqlConfig config;
|
||||
/**
|
||||
* 数据库链接
|
||||
* */
|
||||
private Connection connection;
|
||||
|
||||
|
||||
public JDBCTool(SmsSqlConfig config,Connection connection) {
|
||||
// if (config == null) {
|
||||
// throw new SmsSqlException("The configuration file failed to be loaded");
|
||||
// }
|
||||
this.config = config;
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public JDBCTool(SmsSqlConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public void setConnection(Connection connection){
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public static Map<String, String> selectConfig() {
|
||||
Map<String, String> select = BeanFactory.getJDBCTool().select();
|
||||
if (select.size() == 0) {
|
||||
throw new SmsSqlException("No valid configuration was scanned. Please check whether the configuration file or database link is normal");
|
||||
}
|
||||
return select;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取链接
|
||||
*/
|
||||
public Connection getConn() {
|
||||
Connection connection;
|
||||
String url;
|
||||
if (SmsUtil.isEmpty(config.getUrl())) {
|
||||
throw new SmsSqlException("The configuration file failed to be loaded");
|
||||
}
|
||||
try {
|
||||
if (config.getDatabaseName().isEmpty()){
|
||||
url = config.getUrl();
|
||||
}else{
|
||||
url = config.getUrl() + "/" + config.getDatabaseName();
|
||||
}
|
||||
connection = DriverManager.getConnection(url, config.getUsername(), config.getPassword());
|
||||
} catch (SQLException e) {
|
||||
throw new SmsSqlException(e.getMessage());
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* select
|
||||
* <p>查询封装
|
||||
* @param sql 要查询的sql语句
|
||||
* @author :Wind
|
||||
*/
|
||||
public Map<String, String> select(String sql) {
|
||||
// Connection conn = null;
|
||||
if (Objects.isNull(connection)){
|
||||
connection = getConn();
|
||||
}
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
Map<String, String> map = new Hashtable<>();
|
||||
try {
|
||||
// conn = getConn();
|
||||
preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
preparedStatement.setFetchSize(1000);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
String data = resultSet.getString(config.getConfigName());
|
||||
String supplier = resultSet.getString(config.getSupplierFieldName());
|
||||
map.put(supplier, data);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new SmsSqlException(e.getMessage());
|
||||
} finally {
|
||||
close(connection, preparedStatement, resultSet);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* close
|
||||
* <p>关闭链接方法
|
||||
* @author :Wind
|
||||
*/
|
||||
private void close(Connection conn, PreparedStatement stmt, ResultSet rs) {
|
||||
// 关闭连接
|
||||
if (conn != null) {
|
||||
// try {
|
||||
// conn.close();
|
||||
// } catch (SQLException e) {
|
||||
// throw new SmsSqlException(e.getMessage());
|
||||
// }
|
||||
}
|
||||
// 关闭 statement
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw new SmsSqlException(e.getMessage());
|
||||
}
|
||||
}
|
||||
// 关闭结果集
|
||||
if (rs != null) {
|
||||
try {
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
throw new SmsSqlException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> select() {
|
||||
String format = StrUtil.format(SELECT,
|
||||
config.getConfigName(),
|
||||
config.getSupplierFieldName(),
|
||||
config.getTableName(),
|
||||
config.getStartName(),
|
||||
config.getIsStart());
|
||||
return select(format);
|
||||
}
|
||||
}
|
||||
@ -2,21 +2,21 @@ package org.dromara.sms4j.core.factory;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.smsProxy.SmsInvocationHandler;
|
||||
import org.dromara.sms4j.api.universal.SupplierConfig;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
import org.dromara.sms4j.core.datainterface.SmsReadConfig;
|
||||
import org.dromara.sms4j.core.load.SmsLoad;
|
||||
import org.dromara.sms4j.core.smsProxy.SmsInvocationHandler;
|
||||
import org.dromara.sms4j.provider.config.BaseConfig;
|
||||
import org.dromara.sms4j.provider.factory.BaseProviderFactory;
|
||||
import org.dromara.sms4j.provider.factory.BeanFactory;
|
||||
import org.dromara.sms4j.provider.factory.ProviderFactoryHolder;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -35,7 +35,7 @@ public abstract class SmsFactory {
|
||||
* <p>key: configId,短信服务对象的唯一标识</p>
|
||||
* <p>value: 短信服务对象</p>
|
||||
*/
|
||||
private final static Map<String, SmsBlend> blends = new LinkedHashMap<>();
|
||||
private final static Map<String, SmsBlend> blends = new ConcurrentHashMap<>();
|
||||
|
||||
private SmsFactory() {
|
||||
}
|
||||
@ -48,55 +48,89 @@ public abstract class SmsFactory {
|
||||
* @author :Wind
|
||||
*/
|
||||
public static void createSmsBlend(SupplierConfig config) {
|
||||
SmsBlend sms = create(config);
|
||||
register(sms);
|
||||
SmsLoad.starConfig(sms, config);
|
||||
SmsBlend smsBlend = create(config);
|
||||
register(smsBlend);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* createSmsBlend
|
||||
* <p> 创建一个指定厂商开启短信拦截后的实例,isRestricted为true时,创建出来的实例带有短信拦截性质,拦截的参数取决于配置文件
|
||||
*
|
||||
* @param config 短信配置
|
||||
* @param isRestricted 是否为开启了短信拦截的实例
|
||||
* @author :Wind
|
||||
*/
|
||||
public static void createSmsBlend(SupplierConfig config, Boolean isRestricted) {
|
||||
SmsBlend sms = create(config);
|
||||
if (isRestricted) {
|
||||
sms = renderWithRestricted(sms);
|
||||
}
|
||||
register(sms);
|
||||
SmsLoad.starConfig(sms, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* createSmsBlend
|
||||
* <p>通过配置读取接口创建某个短信实例
|
||||
* <p>该方法创建的短信实例将会交给框架进行托管,后续可以通过getSmsBlend获取
|
||||
* <p>该方法会直接调用接口实现
|
||||
*
|
||||
* @param smsReadConfig 读取额外配置接口
|
||||
* @param configId 配置ID
|
||||
* @param configId 配置ID
|
||||
* @author :Wind
|
||||
*/
|
||||
public static void createSmsBlend(SmsReadConfig smsReadConfig,String configId){
|
||||
public static void createSmsBlend(SmsReadConfig smsReadConfig, String configId) {
|
||||
BaseConfig supplierConfig = smsReadConfig.getSupplierConfig(configId);
|
||||
SmsBlend smsBlend = create(supplierConfig);
|
||||
register(smsBlend);
|
||||
}
|
||||
|
||||
/**
|
||||
* createSmsBlend
|
||||
* createSmsBlend
|
||||
* <p>通过配置读取接口创建全部短信实例
|
||||
* <p>该方法创建的短信实例将会交给框架进行托管,后续可以通过getSmsBlend获取
|
||||
* <p>该方法会直接调用接口实现
|
||||
*
|
||||
* @param smsReadConfig 读取额外配置接口
|
||||
* @author :Wind
|
||||
*/
|
||||
public static void createSmsBlend(SmsReadConfig smsReadConfig){
|
||||
*/
|
||||
public static void createSmsBlend(SmsReadConfig smsReadConfig) {
|
||||
List<BaseConfig> supplierConfigList = smsReadConfig.getSupplierConfigList();
|
||||
supplierConfigList.forEach(f->{
|
||||
register(create(f));
|
||||
supplierConfigList.forEach(supplierConfig -> {
|
||||
SmsBlend smsBlend = create(supplierConfig);
|
||||
register(smsBlend);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* createRestrictedSmsBlend
|
||||
* <p> 创建一个指定厂商开启短信拦截后的实例,拦截的参数取决于配置参数
|
||||
*
|
||||
* @param config 短信配置
|
||||
* @author :Wind
|
||||
*/
|
||||
public static void createRestrictedSmsBlend(SupplierConfig config) {
|
||||
SmsBlend smsBlend = create(config);
|
||||
smsBlend = renderWithRestricted(smsBlend);
|
||||
register(smsBlend);
|
||||
}
|
||||
|
||||
/**
|
||||
* createRestrictedSmsBlend
|
||||
* <p>通过配置读取接口创建某个开启短信拦截后的短信实例
|
||||
* <p>该方法创建的短信实例将会交给框架进行托管,后续可以通过getSmsBlend获取
|
||||
* <p>该方法会直接调用接口实现
|
||||
*
|
||||
* @param smsReadConfig 读取额外配置接口
|
||||
* @param configId 配置ID
|
||||
* @author :Wind
|
||||
*/
|
||||
public static void createRestrictedSmsBlend(SmsReadConfig smsReadConfig, String configId) {
|
||||
BaseConfig supplierConfig = smsReadConfig.getSupplierConfig(configId);
|
||||
SmsBlend smsBlend = create(supplierConfig);
|
||||
smsBlend = renderWithRestricted(smsBlend);
|
||||
register(smsBlend);
|
||||
}
|
||||
|
||||
/**
|
||||
* createRestrictedSmsBlend
|
||||
* <p>通过配置读取接口创建全部开启短信拦截后的短信实例
|
||||
* <p>该方法创建的短信实例将会交给框架进行托管,后续可以通过getSmsBlend获取
|
||||
* <p>该方法会直接调用接口实现
|
||||
*
|
||||
* @param smsReadConfig 读取额外配置接口
|
||||
* @author :Wind
|
||||
*/
|
||||
public static void createRestrictedSmsBlend(SmsReadConfig smsReadConfig) {
|
||||
List<BaseConfig> supplierConfigList = smsReadConfig.getSupplierConfigList();
|
||||
supplierConfigList.forEach(supplierConfig -> {
|
||||
SmsBlend smsBlend = create(supplierConfig);
|
||||
smsBlend = renderWithRestricted(smsBlend);
|
||||
register(smsBlend);
|
||||
});
|
||||
}
|
||||
|
||||
@ -109,43 +143,16 @@ public abstract class SmsFactory {
|
||||
}
|
||||
|
||||
/**
|
||||
* renderWithRestricted
|
||||
* renderWithRestricted
|
||||
* <p> 构建smsBlend对象的代理对象
|
||||
*
|
||||
* @author :Wind
|
||||
*/
|
||||
*/
|
||||
private static SmsBlend renderWithRestricted(SmsBlend sms) {
|
||||
SmsInvocationHandler smsInvocationHandler = SmsInvocationHandler.newSmsInvocationHandler(sms, BeanFactory.getSmsConfig());
|
||||
return (SmsBlend) Proxy.newProxyInstance(sms.getClass().getClassLoader(), new Class[]{SmsBlend.class}, smsInvocationHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过供应商标识获取首个短信服务对象
|
||||
*
|
||||
* @param supplier 供应商标识
|
||||
* @return 返回短信服务对象。如果未找到则返回null
|
||||
*/
|
||||
public static SmsBlend getFirstBySupplier(String supplier) {
|
||||
if (StrUtil.isEmpty(supplier)) {
|
||||
throw new SmsBlendException("供应商标识不能为空");
|
||||
}
|
||||
return blends.values().stream().filter(smsBlend -> supplier.equals(smsBlend.getSupplier())).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过供应商标识获取短信服务对象列表
|
||||
*
|
||||
* @param supplier 供应商标识
|
||||
* @return 返回短信服务对象列表。如果未找到则返回空列表
|
||||
*/
|
||||
public static List<SmsBlend> getListBySupplier(String supplier) {
|
||||
List<SmsBlend> list = new ArrayList<>();
|
||||
if (StrUtil.isEmpty(supplier)) {
|
||||
throw new SmsBlendException("供应商标识不能为空");
|
||||
}
|
||||
list = blends.values().stream().filter(smsBlend -> supplier.equals(smsBlend.getSupplier())).collect(Collectors.toList());
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过负载均衡服务获取短信服务对象
|
||||
*
|
||||
@ -165,6 +172,35 @@ public abstract class SmsFactory {
|
||||
return blends.get(configId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过供应商标识获取单个短信服务对象
|
||||
* <p>当供应商有多个短信服务对象时无法保证获取顺序</p>
|
||||
*
|
||||
* @param supplier 供应商标识
|
||||
* @return 返回短信服务对象。如果未找到则返回null
|
||||
*/
|
||||
public static SmsBlend getBySupplier(String supplier) {
|
||||
if (StrUtil.isEmpty(supplier)) {
|
||||
throw new SmsBlendException("供应商标识不能为空");
|
||||
}
|
||||
return blends.values().stream().filter(smsBlend -> supplier.equals(smsBlend.getSupplier())).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过供应商标识获取短信服务对象列表
|
||||
*
|
||||
* @param supplier 供应商标识
|
||||
* @return 返回短信服务对象列表。如果未找到则返回空列表
|
||||
*/
|
||||
public static List<SmsBlend> getListBySupplier(String supplier) {
|
||||
List<SmsBlend> list = null;
|
||||
if (StrUtil.isEmpty(supplier)) {
|
||||
throw new SmsBlendException("供应商标识不能为空");
|
||||
}
|
||||
list = blends.values().stream().filter(smsBlend -> supplier.equals(smsBlend.getSupplier())).collect(Collectors.toList());
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全部短信服务对象
|
||||
*
|
||||
@ -184,6 +220,20 @@ public abstract class SmsFactory {
|
||||
throw new SmsBlendException("短信服务对象不能为空");
|
||||
}
|
||||
blends.put(smsBlend.getConfigId(), smsBlend);
|
||||
SmsLoad.starConfig(smsBlend, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册短信服务对象
|
||||
*
|
||||
* @param smsBlend 短信服务对象
|
||||
*/
|
||||
public static void register(SmsBlend smsBlend, Integer weight) {
|
||||
if (smsBlend == null) {
|
||||
throw new SmsBlendException("短信服务对象不能为空");
|
||||
}
|
||||
blends.put(smsBlend.getConfigId(), smsBlend);
|
||||
SmsLoad.starConfig(smsBlend, weight);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,20 +253,22 @@ public abstract class SmsFactory {
|
||||
return false;
|
||||
}
|
||||
blends.put(configId, smsBlend);
|
||||
SmsLoad.starConfig(smsBlend, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* registerIfAbsent
|
||||
* registerIfAbsent
|
||||
* <p> 以configId为标识,当短信服务对象不存在时,进行注册。并添加至系统的负载均衡器
|
||||
*
|
||||
* @param smsBlend 短信服务对象
|
||||
* @param weight 权重
|
||||
* @param weight 权重
|
||||
* @return 是否注册成功
|
||||
* <p>当对象不存在时,进行注册并返回true</p>
|
||||
* <p>当对象已存在时,返回false</p>
|
||||
* @author :Wind
|
||||
*/
|
||||
public static boolean registerIfAbsent(SmsBlend smsBlend,Integer weight) {
|
||||
*/
|
||||
public static boolean registerIfAbsent(SmsBlend smsBlend, Integer weight) {
|
||||
if (smsBlend == null) {
|
||||
throw new SmsBlendException("短信服务对象不能为空");
|
||||
}
|
||||
@ -225,13 +277,14 @@ public abstract class SmsFactory {
|
||||
return false;
|
||||
}
|
||||
blends.put(configId, smsBlend);
|
||||
SmsLoad.starConfig(smsBlend,weight);
|
||||
SmsLoad.starConfig(smsBlend, weight);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销短信服务对象
|
||||
*<p>与此同时会注销掉负载均衡器中已经存在的对象</p>
|
||||
* <p>与此同时会注销掉负载均衡器中已经存在的对象</p>
|
||||
*
|
||||
* @param configId 标识
|
||||
* @return 是否注销成功
|
||||
* <p>当configId存在时,进行注销并返回true</p>
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
package org.dromara.sms4j.api.smsProxy;
|
||||
package org.dromara.sms4j.core.smsProxy;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.dao.SmsDao;
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package org.dromara.sms4j.api.smsProxy;
|
||||
package org.dromara.sms4j.core.smsProxy;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
@ -9,28 +9,17 @@ import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.universal.SupplierConfig;
|
||||
import org.dromara.sms4j.cloopen.config.CloopenConfig;
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
import org.dromara.sms4j.provider.factory.BeanFactory;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||
import org.dromara.sms4j.emay.config.EmayConfig;
|
||||
import org.dromara.sms4j.huawei.config.HuaweiConfig;
|
||||
import org.dromara.sms4j.javase.util.YamlUtil;
|
||||
import org.dromara.sms4j.jdcloud.config.JdCloudConfig;
|
||||
import org.dromara.sms4j.netease.config.NeteaseConfig;
|
||||
import org.dromara.sms4j.provider.config.BaseConfig;
|
||||
import org.dromara.sms4j.provider.factory.BaseProviderFactory;
|
||||
import org.dromara.sms4j.provider.factory.ProviderFactoryHolder;
|
||||
import org.dromara.sms4j.tencent.config.TencentConfig;
|
||||
import org.dromara.sms4j.unisms.config.UniConfig;
|
||||
import org.dromara.sms4j.yunpian.config.YunpianConfig;
|
||||
import org.dromara.sms4j.zhutong.config.ZhutongConfig;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@ -98,6 +87,7 @@ public class SEInitializer {
|
||||
throw new SmsBlendException("初始化配置失败");
|
||||
}
|
||||
|
||||
//初始化SmsConfig整体配置文件
|
||||
this.initSmsConfig(smsConfig);
|
||||
// 解析供应商配置
|
||||
Map<String, Map<String, String>> blends = smsConfig.getBlends();
|
||||
@ -108,11 +98,16 @@ public class SEInitializer {
|
||||
BaseProviderFactory<SmsBlend, SupplierConfig> providerFactory = (BaseProviderFactory<SmsBlend, SupplierConfig>) ProviderFactoryHolder.requireForSupplier(supplier);
|
||||
if(providerFactory == null) {
|
||||
log.warn("创建\"{}\"的短信服务失败,未找到供应商为\"{}\"的服务", configId, supplier);
|
||||
continue;
|
||||
}
|
||||
SmsUtil.replaceKeysSeperator(configMap, "-", "_");
|
||||
JSONObject configJson = new JSONObject(configMap);
|
||||
SupplierConfig supplierConfig = JSONUtil.toBean(configJson, providerFactory.getConfigClass());
|
||||
providerFactory.createSms(supplierConfig);
|
||||
if(Boolean.TRUE.equals(smsConfig.getRestricted())) {
|
||||
SmsFactory.createRestrictedSmsBlend(supplierConfig);
|
||||
} else {
|
||||
SmsFactory.createSmsBlend(supplierConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -26,7 +26,7 @@ import java.util.stream.Collectors;
|
||||
@Slf4j
|
||||
public class JdCloudSmsImpl extends AbstractSmsBlend<JdCloudConfig> {
|
||||
|
||||
public static final String SUPPLIER = "jdCloud";
|
||||
public static final String SUPPLIER = "jdcloud";
|
||||
|
||||
private final SmsClient client;
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package org.dromara.sms4j.comm.config;
|
||||
package org.dromara.sms4j.provider.config;
|
||||
|
||||
public class SmsBanner {
|
||||
private static final String banner =
|
||||
@ -1,14 +1,16 @@
|
||||
package org.dromara.sms4j.comm.config;
|
||||
package org.dromara.sms4j.provider.config;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.sms4j.api.dao.SmsDao;
|
||||
import org.dromara.sms4j.api.dao.SmsDaoDefaultImpl;
|
||||
import org.dromara.sms4j.comm.enumerate.ConfigType;
|
||||
|
||||
@Data
|
||||
public class SmsConfig {
|
||||
|
||||
/** 配置源类型*/
|
||||
private ConfigType configType = ConfigType.CONFIG_FILE;
|
||||
private ConfigType configType = ConfigType.YAML;
|
||||
|
||||
/**
|
||||
* 打印banner
|
||||
@ -1,4 +1,4 @@
|
||||
package org.dromara.sms4j.comm.config;
|
||||
package org.dromara.sms4j.provider.config;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
@ -1,9 +1,11 @@
|
||||
package org.dromara.sms4j.comm.factory;
|
||||
package org.dromara.sms4j.provider.factory;
|
||||
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.config.SmsSqlConfig;
|
||||
import org.dromara.sms4j.api.dao.SmsDao;
|
||||
import org.dromara.sms4j.api.dao.SmsDaoDefaultImpl;
|
||||
import org.dromara.sms4j.comm.enumerate.ConfigType;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.config.SmsSqlConfig;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.utils.JDBCTool;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.Executor;
|
||||
@ -29,7 +31,7 @@ public class BeanFactory {
|
||||
private static SmsConfig smsConfig;
|
||||
|
||||
/** jdbc工具*/
|
||||
private static JDBCTool jdbcTool;
|
||||
// private static JDBCTool jdbcTool;
|
||||
|
||||
/** 数据库配置*/
|
||||
private static SmsSqlConfig smsSqlConfig;
|
||||
@ -79,10 +81,10 @@ public class BeanFactory {
|
||||
return smsSqlConfig;
|
||||
}
|
||||
|
||||
public static JDBCTool getJDBCTool(){
|
||||
if (jdbcTool == null){
|
||||
jdbcTool = new JDBCTool(getSmsSqlConfig());
|
||||
}
|
||||
return jdbcTool;
|
||||
}
|
||||
// public static JDBCTool getJDBCTool(){
|
||||
// if (jdbcTool == null){
|
||||
// jdbcTool = new JDBCTool(getSmsSqlConfig());
|
||||
// }
|
||||
// return jdbcTool;
|
||||
// }
|
||||
}
|
||||
@ -3,7 +3,9 @@ package org.dromara.sms4j.provider.factory;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.universal.SupplierConfig;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@ -14,17 +16,14 @@ import java.util.Set;
|
||||
*/
|
||||
public class ProviderFactoryHolder {
|
||||
|
||||
private static final Set<BaseProviderFactory<? extends SmsBlend, ? extends SupplierConfig>> factories = new HashSet<>();
|
||||
private static final Map<String, BaseProviderFactory<? extends SmsBlend, ? extends SupplierConfig>> factories = new HashMap<>();
|
||||
|
||||
public static void registerFactory(BaseProviderFactory<? extends SmsBlend, ? extends SupplierConfig> factory) {
|
||||
factories.add(factory);
|
||||
factories.put(factory.getSupplier(), factory);
|
||||
}
|
||||
|
||||
public static BaseProviderFactory<? extends SmsBlend, ? extends SupplierConfig> requireForSupplier(String supplier) {
|
||||
return factories.stream()
|
||||
.filter(f -> f.getSupplier().equals(supplier))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
return factories.getOrDefault(supplier, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ import org.dromara.sms4j.api.callback.CallBack;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.api.universal.SupplierConfig;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
import org.dromara.sms4j.provider.factory.BeanFactory;
|
||||
import org.dromara.sms4j.comm.utils.SmsHttpUtil;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
@ -24,7 +24,7 @@ import java.util.concurrent.Executor;
|
||||
@Slf4j
|
||||
public class TencentSmsImpl extends AbstractSmsBlend<TencentConfig> {
|
||||
|
||||
public static final String SUPPLIER = "alibaba";
|
||||
public static final String SUPPLIER = "tencent";
|
||||
private int retry = 0;
|
||||
|
||||
public TencentSmsImpl(TencentConfig tencentSmsConfig, Executor pool, DelayedTime delayed) {
|
||||
|
||||
@ -22,7 +22,7 @@ import java.util.concurrent.Executor;
|
||||
@Slf4j
|
||||
public class UniSmsImpl extends AbstractSmsBlend<UniConfig> {
|
||||
|
||||
public static final String SUPPLIER = "uniSms";
|
||||
public static final String SUPPLIER = "unisms";
|
||||
|
||||
public UniSmsImpl(UniConfig config, Executor pool, DelayedTime delayed) {
|
||||
super(config, pool, delayed);
|
||||
|
||||
@ -2,8 +2,8 @@ package org.dromara.sms4j.solon.aop;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.dao.SmsDao;
|
||||
import org.dromara.sms4j.api.smsProxy.RestrictedProcess;
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.core.smsProxy.RestrictedProcess;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.noear.solon.core.AopContext;
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
package org.dromara.sms4j.solon.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.smsProxy.SmsInvocationHandler;
|
||||
import org.dromara.sms4j.comm.config.SmsBanner;
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.config.SmsSqlConfig;
|
||||
import org.dromara.sms4j.core.smsProxy.SmsInvocationHandler;
|
||||
import org.dromara.sms4j.provider.config.SmsBanner;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.config.SmsSqlConfig;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
import org.dromara.sms4j.provider.factory.BeanFactory;
|
||||
import org.dromara.sms4j.solon.aop.SolonRestrictedProcess;
|
||||
import org.noear.solon.Solon;
|
||||
import org.noear.solon.Utils;
|
||||
|
||||
@ -46,7 +46,7 @@ class Sms4jTest {
|
||||
@Test
|
||||
public void alibabaSmsTest() {
|
||||
// 阿里
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(AlibabaSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(AlibabaSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
|
||||
@ -56,7 +56,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 华为
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(HuaweiSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(HuaweiSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -67,7 +67,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 容联云
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(CloopenSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(CloopenSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -78,7 +78,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 亿美软通
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(EmaySmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(EmaySmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -89,7 +89,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 京东云
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(JdCloudSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(JdCloudSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -100,7 +100,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 云片
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(YunPianSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(YunPianSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -111,7 +111,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 腾讯
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(TencentSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(TencentSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -122,7 +122,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 合一
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(UniSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(UniSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -133,7 +133,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 天翼云
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(CtyunSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(CtyunSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -144,7 +144,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 网易云短信
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(NeteaseSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(NeteaseSmsImpl.SUPPLIER).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -159,7 +159,7 @@ class Sms4jTest {
|
||||
}
|
||||
// 助通短信短信
|
||||
String msg = StrUtil.format("【图书商城】您好,你的验证码是{}:(5分钟失效)", SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(ZhutongSmsImpl.SUPPLIER).sendMessage(PHONE, msg);
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(ZhutongSmsImpl.SUPPLIER).sendMessage(PHONE, msg);
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -175,7 +175,7 @@ class Sms4jTest {
|
||||
// 助通短信短信
|
||||
LinkedHashMap<String, String> messages = new LinkedHashMap<>(1);
|
||||
messages.put("code", SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(ZhutongSmsImpl.SUPPLIER).sendMessage(PHONE, "59264", messages);
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(ZhutongSmsImpl.SUPPLIER).sendMessage(PHONE, "59264", messages);
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -190,7 +190,7 @@ class Sms4jTest {
|
||||
}
|
||||
// 助通短信短信
|
||||
String msg = StrUtil.format("【图书商城】您好,你的验证码是{}:(5分钟失效)", SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(ZhutongSmsImpl.SUPPLIER).massTexting(ListUtil.of(PHONE, "180****1111"), msg);
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(ZhutongSmsImpl.SUPPLIER).massTexting(ListUtil.of(PHONE, "180****1111"), msg);
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -206,7 +206,7 @@ class Sms4jTest {
|
||||
// 助通短信短信
|
||||
LinkedHashMap<String, String> messages = new LinkedHashMap<>(1);
|
||||
messages.put("code", SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getFirstBySupplier(ZhutongSmsImpl.SUPPLIER).massTexting(ListUtil.of(PHONE, "180****1111"), "59264", messages);
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(ZhutongSmsImpl.SUPPLIER).massTexting(ListUtil.of(PHONE, "180****1111"), "59264", messages);
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package org.dromara.sms4j.starter.aop;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.smsProxy.RestrictedProcess;
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.core.smsProxy.RestrictedProcess;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.api.dao.SmsDao;
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
package org.dromara.sms4j.starter.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.comm.config.SmsBanner;
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.config.SmsSqlConfig;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
import org.dromara.sms4j.provider.config.SmsBanner;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.config.SmsSqlConfig;
|
||||
import org.dromara.sms4j.provider.factory.BeanFactory;
|
||||
import org.dromara.sms4j.starter.utils.ConfigUtil;
|
||||
import org.dromara.sms4j.starter.utils.SmsSpringUtil;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@ -42,13 +42,13 @@ public class SmsAutowiredConfig {
|
||||
/** 注入一个定时器*/
|
||||
@Bean
|
||||
protected DelayedTime delayedTime(){
|
||||
return BeanFactory.getDelayedTime();
|
||||
return BeanFactory.getDelayedTime();
|
||||
}
|
||||
|
||||
/** 注入线程池*/
|
||||
@Bean("smsExecutor")
|
||||
protected Executor taskExecutor(SmsConfig config){
|
||||
return BeanFactory.setExecutor(config);
|
||||
return BeanFactory.setExecutor(config);
|
||||
}
|
||||
|
||||
/** 注入一个配置文件读取工具*/
|
||||
@ -59,7 +59,7 @@ public class SmsAutowiredConfig {
|
||||
|
||||
/** smsConfig参数意义为确保注入时smsConfig已经存在*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "sms", name = "config-type", havingValue = "config_file")
|
||||
@ConditionalOnProperty(prefix = "sms", name = "config-type", havingValue = "yaml")
|
||||
protected SupplierConfig supplierConfig(SmsConfig smsConfig){
|
||||
return new SupplierConfig();
|
||||
}
|
||||
@ -90,6 +90,7 @@ public class SmsAutowiredConfig {
|
||||
// for (org.dromara.sms4j.api.universal.SupplierConfig s : beansOfType.values()) {
|
||||
// SupplierFactory.setSupplierConfig(s);
|
||||
// }
|
||||
|
||||
//打印banner
|
||||
if (BeanFactory.getSmsConfig().getIsPrint()){
|
||||
SmsBanner.PrintBanner(Constant.VERSION);
|
||||
|
||||
@ -0,0 +1,53 @@
|
||||
package org.dromara.sms4j.starter.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.universal.SupplierConfig;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.factory.BaseProviderFactory;
|
||||
import org.dromara.sms4j.provider.factory.ProviderFactoryHolder;
|
||||
import org.dromara.sms4j.starter.utils.SmsSpringUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class SmsBlendsInitializer {
|
||||
|
||||
private final SmsConfig smsConfig;
|
||||
private final Map<String, Map<String, String>> blends;
|
||||
|
||||
@PostConstruct
|
||||
public void initBlends() {
|
||||
// 解析供应商配置
|
||||
for(String configId : blends.keySet()) {
|
||||
Map<String, String> configMap = blends.get(configId);
|
||||
String supplier = configMap.get(Constant.SUPPLIER_KEY);
|
||||
supplier = StrUtil.isEmpty(supplier) ? configId : supplier;
|
||||
BaseProviderFactory<SmsBlend, SupplierConfig> providerFactory = (BaseProviderFactory<SmsBlend, org.dromara.sms4j.api.universal.SupplierConfig>) ProviderFactoryHolder.requireForSupplier(supplier);
|
||||
if(providerFactory == null) {
|
||||
log.warn("创建\"{}\"的短信服务失败,未找到供应商为\"{}\"的服务", configId, supplier);
|
||||
continue;
|
||||
}
|
||||
SmsUtil.replaceKeysSeperator(configMap, "-", "_");
|
||||
JSONObject configJson = new JSONObject(configMap);
|
||||
org.dromara.sms4j.api.universal.SupplierConfig supplierConfig = JSONUtil.toBean(configJson, providerFactory.getConfigClass());
|
||||
if(Boolean.TRUE.equals(smsConfig.getRestricted())) {
|
||||
SmsFactory.createRestrictedSmsBlend(supplierConfig);
|
||||
} else {
|
||||
SmsFactory.createSmsBlend(supplierConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,119 +1,18 @@
|
||||
package org.dromara.sms4j.starter.config;
|
||||
|
||||
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
|
||||
import org.dromara.sms4j.cloopen.config.CloopenConfig;
|
||||
import org.dromara.sms4j.ctyun.config.CtyunConfig;
|
||||
import org.dromara.sms4j.emay.config.EmayConfig;
|
||||
import org.dromara.sms4j.huawei.config.HuaweiConfig;
|
||||
import org.dromara.sms4j.jdcloud.config.JdCloudConfig;
|
||||
import org.dromara.sms4j.netease.config.NeteaseConfig;
|
||||
import org.dromara.sms4j.tencent.config.TencentConfig;
|
||||
import org.dromara.sms4j.unisms.config.UniConfig;
|
||||
import org.dromara.sms4j.yunpian.config.YunpianConfig;
|
||||
import org.dromara.sms4j.zhutong.config.ZhutongConfig;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SupplierConfig {
|
||||
|
||||
|
||||
/**
|
||||
* 阿里差异化配置
|
||||
*/
|
||||
/** smsConfig参数意义为确保注入时smsConfig已经存在*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.alibaba")
|
||||
protected AlibabaConfig alibabaConfig() {
|
||||
return new AlibabaConfig();
|
||||
@ConfigurationProperties(prefix = "sms.blends")
|
||||
protected Map<String, Map<String, String>> blends(){
|
||||
return new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 华为差异化配置
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.huawei")
|
||||
protected HuaweiConfig huaweiConfig() {
|
||||
return new HuaweiConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 云片短信差异化配置
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.yunpian")
|
||||
protected YunpianConfig yunpianConfig() {
|
||||
return new YunpianConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 合一短信差异化配置
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.uni")
|
||||
protected UniConfig uniConfig() {
|
||||
return new UniConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 腾讯短信差异化配置
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.tencent")
|
||||
protected TencentConfig tencentConfig() {
|
||||
return new TencentConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 京东云短信差异化配置
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.jdcloud")
|
||||
protected JdCloudConfig jdCloudConfig() {
|
||||
return new JdCloudConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 容联云短信差异化配置
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.cloopen")
|
||||
protected CloopenConfig cloopenConfig() {
|
||||
return new CloopenConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 亿美软通短信差异化配置
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.emay")
|
||||
protected EmayConfig emayConfig() {
|
||||
return new EmayConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 天翼云短信差异化配置
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.ctyun")
|
||||
protected CtyunConfig ctyunConfig() {
|
||||
return new CtyunConfig();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 网易云信差异化配置
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.netease")
|
||||
protected NeteaseConfig neteaseConfig() {
|
||||
return new NeteaseConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 助通信差异化配置
|
||||
*/
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "sms.zhutong")
|
||||
protected ZhutongConfig zhutongConfig() {
|
||||
return new ZhutongConfig();
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,5 +2,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.dromara.sms4j.starter.config.SmsMainConfig,\
|
||||
org.dromara.sms4j.starter.config.SmsAutowiredConfig,\
|
||||
org.dromara.sms4j.starter.config.SupplierConfig,\
|
||||
org.dromara.sms4j.comm.config.SmsConfig,\
|
||||
org.dromara.sms4j.comm.config.SmsSqlConfig
|
||||
org.dromara.sms4j.provider.config.SmsConfig,\
|
||||
org.dromara.sms4j.provider.config.SmsSqlConfig
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
org.dromara.sms4j.starter.config.SmsMainConfig
|
||||
org.dromara.sms4j.starter.config.SmsAutowiredConfig
|
||||
org.dromara.sms4j.starter.config.SupplierConfig
|
||||
org.dromara.sms4j.comm.config.SmsConfig
|
||||
org.dromara.sms4j.comm.config.SmsSqlConfig
|
||||
org.dromara.sms4j.provider.config.SmsConfig
|
||||
org.dromara.sms4j.provider.config.SmsSqlConfig
|
||||
Loading…
x
Reference in New Issue
Block a user