packagecom.www.core.utils;
importjava.util.HashSet;
importjava.util.Properties;
importjava.util.Set;
importjava.util.regex.Pattern;
importorg.apache.commons.pool2.impl.GenericObjectPoolConfig;
importorg.springframework.beans.factory.FactoryBean;
importorg.springframework.beans.factory.InitializingBean;
importorg.springframework.core.io.Resource;
importredis.clients.jedis.HostAndPort;
importredis.clients.jedis.JedisCluster;
public classJedisClusterFactoryimplementsFactoryBean, InitializingBean {
privateString address;
privateJedisCluster jedisCluster;
privateInteger timeout;
privateInteger maxRedirections;
privateGenericObjectPoolConfig genericObjectPoolConfig;
privatePattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");
@Override
publicJedisCluster getObject()throwsException {
returnjedisCluster;
}
@Override
publicClass<?extendsJedisCluster> getObjectType() {
return(this.jedisCluster !=null?this.jedisCluster.getClass() : JedisCluster.class);
}
@Override
public booleanisSingleton() {
return true;
}
privateSet parseHostAndPort()throwsException {
try{
String[] addressArr=address.trim().split(",");
Set haps =newHashSet();
for(String addressStr:addressArr){
String[] ipAndPort = addressStr.trim().split(":");
HostAndPort hap =newHostAndPort(ipAndPort[0].trim(), Integer.parseInt(ipAndPort[1].trim()));
haps.add(hap);
}
returnhaps;
}catch(IllegalArgumentException ex) {
throwex;
}catch(Exception ex) {
throw newException("解析 jedis 配置文件失败", ex);
}
}
@Override
public voidafterPropertiesSet()throwsException {
Set haps =this.parseHostAndPort();
jedisCluster =newJedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);
}
public voidsetTimeout(inttimeout) {
this.timeout = timeout;
}
public voidsetMaxRedirections(intmaxRedirections) {
this.maxRedirections = maxRedirections;
}
/*** @Param String address to set*/
public voidsetAddress(String address) {
this.address = address;
}
public voidsetGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
this.genericObjectPoolConfig = genericObjectPoolConfig;
}
}