0

I'm working on a custom Boomi connector that uses Jedis in order to send data to a redis server. One thing I am trying to work on, is removing the parameter String parameters and still determining if I should use a pool, but I am unsure how I would go about this.

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."));
    }
}
Jacob
  • 371
  • 2
  • 18
  • 1
    First of all your naming convention is very confusing. If `noPool` parameter is provided, you have set the `isPool = true`. From your title i understand that you are trying to find whether to have a `JedisPool` or not based on a boolean variable. Could you please explain what is not working ? – Viswanath Lekshmanan Nov 15 '20 at 05:03
  • Hi @ViswanathLekshmanan, I've changed the name from `isPool` to `noPool` as suggested. The code works, however, I want to remove the `Boolean noPool` and still determine whether the client is best to use a pool or just the basic jedis implementation – Jacob Nov 15 '20 at 05:10
  • 1
    I also think I have solved my problem now. I believe, that there is no need to even use this parameter, I could set the default as using the pool, because that seems to be the common-practice. – Jacob Nov 15 '20 at 05:10
  • 1
    Using JedisPool is the normal practice (It will scale up and down). However you can set the max pool size to limit the number of connections without exhausting the resources. – Viswanath Lekshmanan Nov 15 '20 at 05:15

1 Answers1

0

I removed the code that determines if the parameters contain the variable noPool because it is wiser to use a connection pool rather than a single instance in a multi-threaded environment.

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);
}
Jacob
  • 371
  • 2
  • 18