java - 确定将 JedisPool 与布尔变量一起使用
问题描述
我正在开发一个使用 Jedis 的自定义 Boomi 连接器,以便将数据发送到 redis 服务器。我正在尝试做的一件事是删除参数String parameters
并仍然确定是否应该使用池,但我不确定我将如何去做。
public RedisConnectionHandler(String hosts, String password, Integer timeout,
Integer retries, Boolean expiry, Integer timeToExpire, String parameters,
Boolean useSSL) {
if (!isNullOrEmpty(hosts)) {
if (hosts.contains(",")) {
// split into new array value where there is a comma, which indicates that there are more than one
// host to connect to
String[] pairs = hosts.split(",");
// set host and port in a new unique collection
Set<HostAndPort> jedisClusterNodes = new HashSet<>();
for (String s : pairs) {
// split into new array value where there is a semi-column, which contains the port
String[] pair = s.split(":");
// add the host and port into the collection
jedisClusterNodes.add(new HostAndPort(pair[0], Integer.parseInt(pair[1])));
}
JedisCluster jedisCluster;
if (isNullOrEmpty(password)) {
jedisCluster = new JedisCluster(jedisClusterNodes);
} else {
jedisCluster = new JedisCluster(jedisClusterNodes, timeout, timeout, retries, password, new GenericObjectPoolConfig());
}
try {
// provides information about, and dynamic access to, a single field of a class or an interface -
// in this case connectionHandler
Field connectionField = JedisCluster.class.getDeclaredField("connectionHandler");
// Disable java access checks on the connectionHandler field
connectionField.setAccessible(true);
// use the Field class and cast it to connection handler, and get the jedis cluster
jedisClusterConnectionHandler = (JedisClusterConnectionHandler) connectionField.get(jedisCluster);
} catch (Exception e) {
ErrorUtils.throwException(e);
}
} else {
// true if parameters is null and contains "noPool"
noPool = isNullOrEmpty(parameters) && parameters.contains("noPool");
// split string where any semi-column occurs
String[] pair = hosts.split(":");
// if noPool is true
if (noPool) {
// if password is not provided
if (isNullOrEmpty(password)) {
// new jedis connection
jedis = new Jedis(pair[0], Integer.parseInt(pair[1]), timeout, useSSL);
} else {
// new jedis connection, but authenticated
jedis = new Jedis(pair[0], Integer.parseInt(pair[1]), timeout, useSSL);
jedis.auth(password);
}
} else {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(poolSize);
poolConfig.setMaxWaitMillis(timeout);
if (isNullOrEmpty(password)) {
jedisPool = new JedisPool(poolConfig, pair[0], Integer.parseInt(pair[1]), timeout, useSSL);
} else {
jedisPool = new JedisPool(poolConfig, pair[0], Integer.parseInt(pair[1]), timeout,
password, useSSL);
}
}
}
} else {
ErrorUtils.throwException(new Exception("The redis host URL was not supplied."));
}
}
解决方案
我删除了确定参数是否包含变量的代码,noPool
因为在多线程环境中使用连接池而不是单个实例更明智。
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(poolSize);
poolConfig.setMaxWaitMillis(timeout);
if (isNullOrEmpty(password)) {
jedisPool = new JedisPool(poolConfig, pair[0], Integer.parseInt(pair[1]), timeout, useSSL);
} else {
jedisPool = new JedisPool(poolConfig, pair[0], Integer.parseInt(pair[1]), timeout,
password, useSSL);
}
推荐阅读
- pine-script - Pinescript 如何显示 strategy.account_currency?
- java - 在 Java 中将 Map 转换为 JSON
- c# - 使用 sql 提高 Web 应用程序中的搜索速度
- jq - 使用 --output 表时 OCI CLI 命令失败并出现解析错误
- yaml - 在 gitlab-ci.yml 文件中编写 for 循环脚本
- typescript - 如何在 TypeScript 中将 Union 变成元组?
- sharepoint - 手动启动工作流的页面不起作用
- c# - CSV到XML,数组字符串有问题
- python - 如何将熊猫系列中的值分离到字典中?
- r - 应用纵向分析上的 nlme::gls() R 代码,第 2 版网站需要一些调整