backup-s-server

public class SolrClientFactory {

 

private static Map<String, SolrClient> SERVERS = new ConcurrentHashMap<String, SolrClient>();

 

/**

* Get a solrClient by a specified core/collection name

* @param coreName

* @return a SolrClient

*/

public static SolrClient getSolrClient(String coreName)

{

if(StringUtils.isBlank(coreName))

{

log.error("Failed to get available core name:"+coreName);

throw new SolrServerException("No found out any solr server for a EMPTY core/collection name.");

}

 

SolrClient solrClient = null;

if(!SERVERS.containsKey(coreName))

{

solrClient = SolrClientSingleton.getSolrClientInstance(coreName);

SERVERS.put(coreName, solrClient);

}

else

{

solrClient = SERVERS.get(coreName);

}

 

if(solrClient == null)

{

log.error("No found any solr server for a specified core/collection name:"+coreName);

throw new SolrServerException("No found any solr server for a specified core/collection name:"+coreName);

}

 

return solrClient;

}

}

 

 

public class SolrClientSingleton

{

 

private static ISolrConfiguration config = new SolrConfiguration();

 

private SolrClientSingleton() {}

 

private static class SingletonHolder 

{  

private static final SolrClientSingleton INSTANCE = new SolrClientSingleton();  

}  

 

/**

* Get HttpSolrClient for a single node mode

* @returnSolrClient

*/

public static SolrClient getSolrClientInstance(String coreName)

{

return SingletonHolder.INSTANCE.createSolrClient(coreName); 

}

 

 

private SolrClient createSolrClient(String coreName) 

{

try

{

if(config.isCloudMode())

{

CloudSolrClient cloudSolrClient = new CloudSolrClient(config.getZkHosts());

cloudSolrClient.setDefaultCollection(coreName);

cloudSolrClient.connect();

cloudSolrClient.setZkConnectTimeout(config.getZkConnectionTimeout());

cloudSolrClient.setZkClientTimeout(config.getZkClientTimeout());

return cloudSolrClient;

}

else

{

 

HttpSolrClient httpSolrClient = new HttpSolrClient(getSolrURL(config.getUrl(),coreName));

httpSolrClient.setConnectionTimeout(config.getConnectionTimeout());

httpSolrClient.setDefaultMaxConnectionsPerHost(config.getMaxConnectionsPerHost());

httpSolrClient.setMaxTotalConnections(config.getMaxTotalConnections());

 

return httpSolrClient;

}

}

catch(Exception e)

{

log.error("Failed to connect to the solr server due to:"+e.toString());

throw new SolrServerException("Failed to connect to the solr server.");

}

}

 

/**

* Get a String of which is combined by Solr server url  and core name

* @param url

* @param coreName

* @return a url string

*/

private String getSolrURL(String url,String coreName)

{

if(StringUtils.isBlank(url) )

{

log.error("Solr url cannot be EMPTY.");

return null;

}

 

StringBuilder sb = new StringBuilder(url);

if(!url.endsWith("/"))

{

sb.append("/");

}

 

if(!StringUtils.isBlank(coreName))

{

sb.append(coreName);

}

 

return sb.toString();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值