mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
fix coide
This commit is contained in:
parent
f7f44771e7
commit
d52470e8e7
@ -433,6 +433,12 @@ public class NumberUtilTest {
|
||||
Assert.assertFalse(NumberUtil.isEven(a[4]));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void toBigIntegerTest(){
|
||||
final Number number=1123123;
|
||||
final Number number2=1123123.123;
|
||||
Assert.assertNotNull(NumberUtil.toBigInteger(number));
|
||||
Assert.assertNotNull(NumberUtil.toBigInteger(number2));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,11 +23,9 @@
|
||||
<tomcat-jdbc.version>10.0.20</tomcat-jdbc.version>
|
||||
<druid.version>1.2.9</druid.version>
|
||||
<hikariCP.version>2.4.13</hikariCP.version>
|
||||
<mongo4.version>4.6.0</mongo4.version>
|
||||
<sqlite.version>3.36.0.3</sqlite.version>
|
||||
<!-- 此处固定2.5.x,支持到JDK8 -->
|
||||
<hsqldb.version>2.5.2</hsqldb.version>
|
||||
<jedis.version>4.2.2</jedis.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -102,25 +100,6 @@
|
||||
</exclusions>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-sync</artifactId>
|
||||
<version>${mongo4.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- Redis Java客户端 -->
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>${jedis.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- 测试用依赖 -->
|
||||
<dependency>
|
||||
|
@ -1,397 +0,0 @@
|
||||
package cn.hutool.db.nosql.mongo;
|
||||
|
||||
import cn.hutool.core.exceptions.NotInitedException;
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.setting.Setting;
|
||||
import com.mongodb.MongoClientSettings;
|
||||
import com.mongodb.MongoCredential;
|
||||
import com.mongodb.ServerAddress;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.connection.ConnectionPoolSettings;
|
||||
import com.mongodb.connection.SocketSettings;
|
||||
import org.bson.Document;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* MongoDB4工具类
|
||||
*
|
||||
* @author VampireAchao
|
||||
*/
|
||||
public class MongoDS implements Closeable {
|
||||
|
||||
private final static Log log = Log.get();
|
||||
|
||||
/**
|
||||
* 默认配置文件
|
||||
*/
|
||||
public final static String MONGO_CONFIG_PATH = "config/mongo.setting";
|
||||
|
||||
// MongoDB配置文件
|
||||
private Setting setting;
|
||||
// MongoDB实例连接列表
|
||||
private String[] groups;
|
||||
// MongoDB单点连接信息
|
||||
private ServerAddress serverAddress;
|
||||
// MongoDB客户端对象
|
||||
private MongoClient mongo;
|
||||
|
||||
// --------------------------------------------------------------------------- Constructor start
|
||||
|
||||
/**
|
||||
* 构造MongoDB数据源<br>
|
||||
* 调用者必须持有MongoDS实例,否则会被垃圾回收导致写入失败!
|
||||
*
|
||||
* @param host 主机(域名或者IP)
|
||||
* @param port 端口
|
||||
*/
|
||||
public MongoDS(final String host, final int port) {
|
||||
this.serverAddress = createServerAddress(host, port);
|
||||
initSingle();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造MongoDB数据源<br>
|
||||
* 调用者必须持有MongoDS实例,否则会被垃圾回收导致写入失败!
|
||||
*
|
||||
* @param mongoSetting MongoDB的配置文件,如果是null则读取默认配置文件或者使用MongoDB默认客户端配置
|
||||
* @param host 主机(域名或者IP)
|
||||
* @param port 端口
|
||||
*/
|
||||
public MongoDS(final Setting mongoSetting, final String host, final int port) {
|
||||
this.setting = mongoSetting;
|
||||
this.serverAddress = createServerAddress(host, port);
|
||||
initSingle();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造MongoDB数据源<br>
|
||||
* 当提供多个数据源时,这些数据源将为一个副本集或者多个mongos<br>
|
||||
* 调用者必须持有MongoDS实例,否则会被垃圾回收导致写入失败! 官方文档: http://docs.mongodb.org/manual/administration/replica-sets/
|
||||
*
|
||||
* @param groups 分组列表,当为null或空时使用无分组配置,一个分组使用单一模式,否则使用副本集模式
|
||||
*/
|
||||
public MongoDS(final String... groups) {
|
||||
this.groups = groups;
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造MongoDB数据源<br>
|
||||
* 当提供多个数据源时,这些数据源将为一个副本集或者mongos<br>
|
||||
* 调用者必须持有MongoDS实例,否则会被垃圾回收导致写入失败!<br>
|
||||
* 官方文档: http://docs.mongodb.org/manual/administration/replica-sets/
|
||||
*
|
||||
* @param mongoSetting MongoDB的配置文件,必须有
|
||||
* @param groups 分组列表,当为null或空时使用无分组配置,一个分组使用单一模式,否则使用副本集模式
|
||||
*/
|
||||
public MongoDS(final Setting mongoSetting, final String... groups) {
|
||||
if (mongoSetting == null) {
|
||||
throw new DbRuntimeException("Mongo setting is null!");
|
||||
}
|
||||
this.setting = mongoSetting;
|
||||
this.groups = groups;
|
||||
init();
|
||||
}
|
||||
// --------------------------------------------------------------------------- Constructor end
|
||||
|
||||
/**
|
||||
* 初始化,当给定分组数大于一个时使用
|
||||
*/
|
||||
public void init() {
|
||||
if (groups != null && groups.length > 1) {
|
||||
initCloud();
|
||||
} else {
|
||||
initSingle();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化<br>
|
||||
* 设定文件中的host和端口有三种形式:
|
||||
*
|
||||
* <pre>
|
||||
* host = host:port
|
||||
* </pre>
|
||||
*
|
||||
* <pre>
|
||||
* host = host
|
||||
* port = port
|
||||
* </pre>
|
||||
*
|
||||
* <pre>
|
||||
* host = host
|
||||
* </pre>
|
||||
*/
|
||||
synchronized public void initSingle() {
|
||||
if (setting == null) {
|
||||
try {
|
||||
setting = new Setting(MONGO_CONFIG_PATH, true);
|
||||
} catch (final Exception e) {
|
||||
// 在single模式下,可以没有配置文件。
|
||||
}
|
||||
}
|
||||
|
||||
String group = StrUtil.EMPTY;
|
||||
if (null == this.serverAddress) {
|
||||
//存在唯一分组
|
||||
if (groups != null && groups.length == 1) {
|
||||
group = groups[0];
|
||||
}
|
||||
serverAddress = createServerAddress(group);
|
||||
}
|
||||
|
||||
final MongoCredential credentail = createCredentail(group);
|
||||
try {
|
||||
final MongoClientSettings.Builder clusterSettingsBuilder = MongoClientSettings.builder()
|
||||
.applyToClusterSettings(b -> b.hosts(Collections.singletonList(serverAddress)));
|
||||
buildMongoClientSettings(clusterSettingsBuilder, group);
|
||||
if (null != credentail) {
|
||||
clusterSettingsBuilder.credential(credentail);
|
||||
}
|
||||
mongo = MongoClients.create(clusterSettingsBuilder.build());
|
||||
} catch (final Exception e) {
|
||||
throw new DbRuntimeException(StrUtil.format("Init MongoDB pool with connection to [{}] error!", serverAddress), e);
|
||||
}
|
||||
|
||||
log.info("Init MongoDB pool with connection to [{}]", serverAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化集群<br>
|
||||
* 集群的其它客户端设定参数使用全局设定<br>
|
||||
* 集群中每一个实例成员用一个group表示,例如:
|
||||
*
|
||||
* <pre>
|
||||
* user = test1
|
||||
* pass = 123456
|
||||
* database = test
|
||||
* [db0]
|
||||
* host = 192.168.1.1:27117
|
||||
* [db1]
|
||||
* host = 192.168.1.1:27118
|
||||
* [db2]
|
||||
* host = 192.168.1.1:27119
|
||||
* </pre>
|
||||
*/
|
||||
synchronized public void initCloud() {
|
||||
if (groups == null || groups.length == 0) {
|
||||
throw new DbRuntimeException("Please give replication set groups!");
|
||||
}
|
||||
|
||||
if (setting == null) {
|
||||
// 若未指定配置文件,则使用默认配置文件
|
||||
setting = new Setting(MONGO_CONFIG_PATH, true);
|
||||
}
|
||||
|
||||
final List<ServerAddress> addrList = new ArrayList<>();
|
||||
for (final String group : groups) {
|
||||
addrList.add(createServerAddress(group));
|
||||
}
|
||||
|
||||
final MongoCredential credentail = createCredentail(StrUtil.EMPTY);
|
||||
try {
|
||||
final MongoClientSettings.Builder clusterSettingsBuilder = MongoClientSettings.builder()
|
||||
.applyToClusterSettings(b -> b.hosts(addrList));
|
||||
buildMongoClientSettings(clusterSettingsBuilder, StrUtil.EMPTY);
|
||||
if (null != credentail) {
|
||||
clusterSettingsBuilder.credential(credentail);
|
||||
}
|
||||
mongo = MongoClients.create(clusterSettingsBuilder.build());
|
||||
} catch (final Exception e) {
|
||||
log.error(e, "Init MongoDB connection error!");
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("Init MongoDB cloud Set pool with connection to {}", addrList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设定MongoDB配置文件
|
||||
*
|
||||
* @param setting 配置文件
|
||||
*/
|
||||
public void setSetting(final Setting setting) {
|
||||
this.setting = setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 获得MongoDB客户端对象
|
||||
*/
|
||||
public MongoClient getMongo() {
|
||||
return mongo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得DB
|
||||
*
|
||||
* @param dbName DB
|
||||
* @return DB
|
||||
*/
|
||||
public MongoDatabase getDb(final String dbName) {
|
||||
return mongo.getDatabase(dbName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得MongoDB中指定集合对象
|
||||
*
|
||||
* @param dbName 库名
|
||||
* @param collectionName 集合名
|
||||
* @return DBCollection
|
||||
*/
|
||||
public MongoCollection<Document> getCollection(final String dbName, final String collectionName) {
|
||||
return getDb(dbName).getCollection(collectionName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
mongo.close();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------- Private method start
|
||||
|
||||
/**
|
||||
* 创建ServerAddress对象,会读取配置文件中的相关信息
|
||||
*
|
||||
* @param group 分组,如果为{@code null}或者""默认为无分组
|
||||
* @return ServerAddress
|
||||
*/
|
||||
private ServerAddress createServerAddress(String group) {
|
||||
final Setting setting = checkSetting();
|
||||
|
||||
if (group == null) {
|
||||
group = StrUtil.EMPTY;
|
||||
}
|
||||
|
||||
final String tmpHost = setting.getByGroup("host", group);
|
||||
if (StrUtil.isBlank(tmpHost)) {
|
||||
throw new NotInitedException("Host name is empy of group: {}", group);
|
||||
}
|
||||
|
||||
final int defaultPort = setting.getInt("port", group, 27017);
|
||||
return new ServerAddress(NetUtil.buildInetSocketAddress(tmpHost, defaultPort));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建ServerAddress对象
|
||||
*
|
||||
* @param host 主机域名或者IP(如果为空默认127.0.0.1)
|
||||
* @param port 端口(如果为空默认为)
|
||||
* @return ServerAddress
|
||||
*/
|
||||
private ServerAddress createServerAddress(final String host, final int port) {
|
||||
return new ServerAddress(host, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建{@link MongoCredential},用于服务端验证<br>
|
||||
* 此方法会首先读取指定分组下的属性,用户没有定义则读取空分组下的属性
|
||||
*
|
||||
* @param group 分组
|
||||
* @return {@link MongoCredential},如果用户未指定用户名密码返回null
|
||||
* @since 4.1.20
|
||||
*/
|
||||
private MongoCredential createCredentail(final String group) {
|
||||
final Setting setting = this.setting;
|
||||
if (null == setting) {
|
||||
return null;
|
||||
}
|
||||
final String user = setting.getStr("user", group, setting.getStr("user"));
|
||||
final String pass = setting.getStr("pass", group, setting.getStr("pass"));
|
||||
final String database = setting.getStr("database", group, setting.getStr("database"));
|
||||
return createCredentail(user, database, pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建{@link MongoCredential},用于服务端验证
|
||||
*
|
||||
* @param userName 用户名
|
||||
* @param database 数据库名
|
||||
* @param password 密码
|
||||
* @return {@link MongoCredential}
|
||||
* @since 4.1.20
|
||||
*/
|
||||
private MongoCredential createCredentail(final String userName, final String database, final String password) {
|
||||
if (StrUtil.hasEmpty(userName, database, database)) {
|
||||
return null;
|
||||
}
|
||||
return MongoCredential.createCredential(userName, database, password.toCharArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构件MongoDB连接选项<br>
|
||||
*
|
||||
* @param group 分组,当分组对应的选项不存在时会读取根选项,如果也不存在使用默认值
|
||||
* @return Builder
|
||||
*/
|
||||
private MongoClientSettings.Builder buildMongoClientSettings(final MongoClientSettings.Builder builder, String group) {
|
||||
if (setting == null) {
|
||||
return builder;
|
||||
}
|
||||
|
||||
if (StrUtil.isEmpty(group)) {
|
||||
group = StrUtil.EMPTY;
|
||||
} else {
|
||||
group = group + StrUtil.DOT;
|
||||
}
|
||||
|
||||
// 每个主机答应的连接数(每个主机的连接池大小),当连接池被用光时,会被阻塞住
|
||||
Integer connectionsPerHost = setting.getInt(group + "connectionsPerHost");
|
||||
if (StrUtil.isBlank(group) == false && connectionsPerHost == null) {
|
||||
connectionsPerHost = setting.getInt("connectionsPerHost");
|
||||
}
|
||||
final ConnectionPoolSettings.Builder connectionPoolSettingsBuilder = ConnectionPoolSettings.builder();
|
||||
if (connectionsPerHost != null) {
|
||||
connectionPoolSettingsBuilder.maxSize(connectionsPerHost);
|
||||
log.debug("MongoDB connectionsPerHost: {}", connectionsPerHost);
|
||||
}
|
||||
|
||||
// 被阻塞线程从连接池获取连接的最长等待时间(ms) --int
|
||||
final Integer connectTimeout = setting.getInt(group + "connectTimeout");
|
||||
if (StrUtil.isBlank(group) == false && connectTimeout == null) {
|
||||
setting.getInt("connectTimeout");
|
||||
}
|
||||
if (connectTimeout != null) {
|
||||
connectionPoolSettingsBuilder.maxWaitTime(connectTimeout, TimeUnit.MILLISECONDS);
|
||||
log.debug("MongoDB connectTimeout: {}", connectTimeout);
|
||||
}
|
||||
builder.applyToConnectionPoolSettings(b -> b.applySettings(connectionPoolSettingsBuilder.build()));
|
||||
|
||||
// 套接字超时时间;该值会被传递给Socket.setSoTimeout(int)。默以为0(无穷) --int
|
||||
final Integer socketTimeout = setting.getInt(group + "socketTimeout");
|
||||
if (StrUtil.isBlank(group) == false && socketTimeout == null) {
|
||||
setting.getInt("socketTimeout");
|
||||
}
|
||||
if (socketTimeout != null) {
|
||||
final SocketSettings socketSettings = SocketSettings.builder().connectTimeout(socketTimeout, TimeUnit.MILLISECONDS).build();
|
||||
builder.applyToSocketSettings(b -> b.applySettings(socketSettings));
|
||||
log.debug("MongoDB socketTimeout: {}", socketTimeout);
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查Setting配置文件
|
||||
*
|
||||
* @return Setting配置文件
|
||||
*/
|
||||
private Setting checkSetting() {
|
||||
if (null == this.setting) {
|
||||
throw new DbRuntimeException("Please indicate setting file or create default [{}]", MONGO_CONFIG_PATH);
|
||||
}
|
||||
return this.setting;
|
||||
}
|
||||
// --------------------------------------------------------------------------- Private method end
|
||||
|
||||
}
|
@ -1,126 +0,0 @@
|
||||
package cn.hutool.db.nosql.mongo;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.RuntimeUtil;
|
||||
import cn.hutool.setting.Setting;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* {@link MongoDS}工厂类,用于创建
|
||||
*
|
||||
* @author Looly, VampireAchao
|
||||
*/
|
||||
public class MongoFactory {
|
||||
|
||||
/**
|
||||
* 各分组做组合key的时候分隔符
|
||||
*/
|
||||
private final static String GROUP_SEPRATER = ",";
|
||||
|
||||
/**
|
||||
* 数据源池
|
||||
*/
|
||||
private static final Map<String, MongoDS> DS_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
// JVM关闭前关闭MongoDB连接
|
||||
static {
|
||||
RuntimeUtil.addShutdownHook(MongoFactory::closeAll);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------ Get DS start
|
||||
|
||||
/**
|
||||
* 获取MongoDB数据源<br>
|
||||
*
|
||||
* @param host 主机
|
||||
* @param port 端口
|
||||
* @return MongoDB连接
|
||||
*/
|
||||
public static MongoDS getDS(final String host, final int port) {
|
||||
final String key = host + ":" + port;
|
||||
MongoDS ds = DS_MAP.get(key);
|
||||
if (null == ds) {
|
||||
// 没有在池中加入之
|
||||
ds = new MongoDS(host, port);
|
||||
DS_MAP.put(key, ds);
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取MongoDB数据源<br>
|
||||
* 多个分组名对应的连接组成集群
|
||||
*
|
||||
* @param groups 分组列表
|
||||
* @return MongoDB连接
|
||||
*/
|
||||
public static MongoDS getDS(final String... groups) {
|
||||
final String key = ArrayUtil.join(groups, GROUP_SEPRATER);
|
||||
MongoDS ds = DS_MAP.get(key);
|
||||
if (null == ds) {
|
||||
// 没有在池中加入之
|
||||
ds = new MongoDS(groups);
|
||||
DS_MAP.put(key, ds);
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取MongoDB数据源<br>
|
||||
*
|
||||
* @param groups 分组列表
|
||||
* @return MongoDB连接
|
||||
*/
|
||||
public static MongoDS getDS(final Collection<String> groups) {
|
||||
return getDS(groups.toArray(new String[0]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取MongoDB数据源<br>
|
||||
*
|
||||
* @param setting 设定文件
|
||||
* @param groups 分组列表
|
||||
* @return MongoDB连接
|
||||
*/
|
||||
public static MongoDS getDS(final Setting setting, final String... groups) {
|
||||
final String key = setting.getSettingPath() + GROUP_SEPRATER + ArrayUtil.join(groups, GROUP_SEPRATER);
|
||||
MongoDS ds = DS_MAP.get(key);
|
||||
if (null == ds) {
|
||||
// 没有在池中加入之
|
||||
ds = new MongoDS(setting, groups);
|
||||
DS_MAP.put(key, ds);
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取MongoDB数据源<br>
|
||||
*
|
||||
* @param setting 配置文件
|
||||
* @param groups 分组列表
|
||||
* @return MongoDB连接
|
||||
*/
|
||||
public static MongoDS getDS(final Setting setting, final Collection<String> groups) {
|
||||
return getDS(setting, groups.toArray(new String[0]));
|
||||
}
|
||||
// ------------------------------------------------------------------------ Get DS ends
|
||||
|
||||
/**
|
||||
* 关闭全部连接
|
||||
*/
|
||||
public static void closeAll() {
|
||||
if (MapUtil.isNotEmpty(DS_MAP)) {
|
||||
for (final MongoDS ds : DS_MAP.values()) {
|
||||
ds.close();
|
||||
}
|
||||
DS_MAP.clear();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
/**
|
||||
* MongoDB数据库操作的封装
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.db.nosql.mongo;
|
@ -1,7 +0,0 @@
|
||||
/**
|
||||
* NoSQL封装,包括Redis和MongoDB等数据库操作的封装
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.db.nosql;
|
@ -1,188 +0,0 @@
|
||||
package cn.hutool.db.nosql.redis;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.setting.Setting;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Jedis数据源
|
||||
*
|
||||
* @author looly
|
||||
* @since 3.2.3
|
||||
*/
|
||||
public class RedisDS implements Closeable, Serializable {
|
||||
private static final long serialVersionUID = -5605411972456177456L;
|
||||
/** 默认配置文件 */
|
||||
public final static String REDIS_CONFIG_PATH = "config/redis.setting";
|
||||
|
||||
/** 配置文件 */
|
||||
private Setting setting;
|
||||
/** Jedis连接池 */
|
||||
private JedisPool pool;
|
||||
|
||||
// --------------------------------------------------------------------------------- Static method start
|
||||
/**
|
||||
* 创建RedisDS,使用默认配置文件,默认分组
|
||||
*
|
||||
* @return RedisDS
|
||||
*/
|
||||
public static RedisDS create() {
|
||||
return new RedisDS();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建RedisDS,使用默认配置文件
|
||||
*
|
||||
* @param group 配置文件中配置分组
|
||||
* @return RedisDS
|
||||
*/
|
||||
public static RedisDS create(final String group) {
|
||||
return new RedisDS(group);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建RedisDS
|
||||
*
|
||||
* @param setting 配置文件
|
||||
* @param group 配置文件中配置分组
|
||||
* @return RedisDS
|
||||
*/
|
||||
public static RedisDS create(final Setting setting, final String group) {
|
||||
return new RedisDS(setting, group);
|
||||
}
|
||||
// --------------------------------------------------------------------------------- Static method end
|
||||
|
||||
/**
|
||||
* 构造,使用默认配置文件,默认分组
|
||||
*/
|
||||
public RedisDS() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造,使用默认配置文件
|
||||
*
|
||||
* @param group 配置文件中配置分组
|
||||
*/
|
||||
public RedisDS(final String group) {
|
||||
this(null, group);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param setting 配置文件
|
||||
* @param group 配置文件中配置分组
|
||||
*/
|
||||
public RedisDS(final Setting setting, final String group) {
|
||||
this.setting = setting;
|
||||
init(group);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化Jedis客户端
|
||||
*
|
||||
* @param group Redis服务器信息分组
|
||||
* @return this
|
||||
*/
|
||||
public RedisDS init(final String group) {
|
||||
if (null == setting) {
|
||||
setting = new Setting(REDIS_CONFIG_PATH, true);
|
||||
}
|
||||
|
||||
final JedisPoolConfig config = new JedisPoolConfig();
|
||||
// 共用配置
|
||||
setting.toBean(config);
|
||||
if (StrUtil.isNotBlank(group)) {
|
||||
// 特有配置
|
||||
setting.toBean(group, config);
|
||||
}
|
||||
|
||||
//issue#I54TZ9
|
||||
final Long maxWaitMillis = setting.getLong("maxWaitMillis");
|
||||
if(null != maxWaitMillis){
|
||||
//noinspection deprecation
|
||||
config.setMaxWaitMillis(maxWaitMillis);
|
||||
}
|
||||
|
||||
this.pool = new JedisPool(config,
|
||||
// 地址
|
||||
setting.getStr("host", group, Protocol.DEFAULT_HOST),
|
||||
// 端口
|
||||
setting.getInt("port", group, Protocol.DEFAULT_PORT),
|
||||
// 连接超时
|
||||
setting.getInt("connectionTimeout", group, setting.getInt("timeout", group, Protocol.DEFAULT_TIMEOUT)),
|
||||
// 读取数据超时
|
||||
setting.getInt("soTimeout", group, setting.getInt("timeout", group, Protocol.DEFAULT_TIMEOUT)),
|
||||
// 密码
|
||||
setting.getStr("password", group, null),
|
||||
// 数据库序号
|
||||
setting.getInt("database", group, Protocol.DEFAULT_DATABASE),
|
||||
// 客户端名
|
||||
setting.getStr("clientName", group, "Hutool"),
|
||||
// 是否使用SSL
|
||||
setting.getBool("ssl", group, false),
|
||||
// SSL相关,使用默认
|
||||
null, null, null);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从资源池中获取{@link Jedis}
|
||||
*
|
||||
* @return {@link Jedis}
|
||||
*/
|
||||
public Jedis getJedis() {
|
||||
return this.pool.getResource();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Redis中获取值
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public String getStr(final String key) {
|
||||
try (final Jedis jedis = getJedis()) {
|
||||
return jedis.get(key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Redis中获取值
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return 状态码
|
||||
*/
|
||||
public String setStr(final String key, final String value) {
|
||||
try (final Jedis jedis = getJedis()) {
|
||||
return jedis.set(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Redis中删除多个值
|
||||
*
|
||||
* @param keys 需要删除值对应的键列表
|
||||
* @return 删除个数,0表示无key可删除
|
||||
*/
|
||||
public Long del(final String... keys) {
|
||||
try (final Jedis jedis = getJedis()) {
|
||||
return jedis.del(keys);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
IoUtil.close(pool);
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
/**
|
||||
* Redis(Jedis)数据库操作的封装
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.db.nosql.redis;
|
@ -27,7 +27,7 @@
|
||||
<mail.version>1.6.2</mail.version>
|
||||
<jsch.version>0.1.55</jsch.version>
|
||||
<sshj.version>0.33.0</sshj.version>
|
||||
<zxing.version>3.4.1</zxing.version>
|
||||
<zxing.version>3.5.0</zxing.version>
|
||||
<net.version>3.8.0</net.version>
|
||||
<emoji-java.version>5.1.1</emoji-java.version>
|
||||
<servlet-api.version>4.0.1</servlet-api.version>
|
||||
|
@ -32,7 +32,7 @@ public class QrConfig {
|
||||
protected int foreColor = BLACK;
|
||||
/** 背景色,默认白色,null表示透明 */
|
||||
protected Integer backColor = WHITE;
|
||||
/** 边距1~4 */
|
||||
/** 边距0~4 */
|
||||
protected Integer margin = 2;
|
||||
/** 设置二维码中的信息量,可设置0-40的整数 */
|
||||
protected Integer qrVersion;
|
||||
|
@ -28,7 +28,7 @@ public class QrCodeUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
// @Ignore
|
||||
@Ignore
|
||||
public void generateCustomTest() {
|
||||
final QrConfig config = new QrConfig();
|
||||
config.setMargin(0);
|
||||
|
@ -27,7 +27,7 @@ public class AcceptHandler implements CompletionHandler<ServerSocketChannel, Nio
|
||||
}
|
||||
|
||||
// SocketChannel通道的可读事件注册到Selector中
|
||||
NioUtil.registerChannel(nioServer.getSelector(), socketChannel, Operation.READ);
|
||||
ChannelUtil.registerChannel(nioServer.getSelector(), socketChannel, Operation.READ);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -11,7 +11,7 @@ import java.nio.channels.Selector;
|
||||
*
|
||||
* @since 5.4.0
|
||||
*/
|
||||
public class NioUtil {
|
||||
public class ChannelUtil {
|
||||
|
||||
/**
|
||||
* 注册通道的指定操作到指定Selector上
|
Loading…
Reference in New Issue
Block a user