确定将 JedisPool 与布尔变量一起使用

Determining to use JedisPool with boolean variable

我正在开发使用 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);
}