【问题标题】:Determining to use JedisPool with boolean variable确定将 JedisPool 与布尔变量一起使用
【发布时间】:2021-02-26 15:55:36
【问题描述】:

我正在开发一个使用 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 参数,则您已设置isPool = true。从您的标题中,我了解到您正在尝试根据布尔变量查找是否有JedisPool。你能解释一下什么不起作用吗?
  • 嗨@ViswanathLekshmanan,我已按照建议将名称从isPool 更改为noPool。该代码有效,但是,我想删除 Boolean noPool 并仍然确定客户端最好使用池还是仅使用基本的绝地实现
  • 我也认为我现在已经解决了我的问题。我相信,甚至不需要使用这个参数,我可以将默认设置为使用池,因为这似乎是常见的做法。
  • 使用 JedisPool 是正常的做法(它会放大和缩小)。但是,您可以设置最大池大小来限制连接数,而不会耗尽资源。

标签: java jedis boomi apache-commons-pool


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2014-09-05
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    相关资源
    最近更新 更多