决定使用带布尔变量的spool

axzmvihb  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(324)

我正在开发一个自定义的boomi连接器,它使用jedis向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."));
    }
}
ffvjumwh

ffvjumwh1#

我删除了确定参数是否包含变量的代码 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);
}

相关问题