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."));
}
}
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);
}