Solrj client连接池

本文介绍了一种在Solr应用中实现连接池的方法,通过自定义连接池类管理HttpSolrClient实例,有效减少资源消耗并提高查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做solr的时候, 发现每次查询新建 删除 都要创建一个新的HttpSolrClient, 在网上也没有查到solrj的连接池, 就写了一个简单的, 下面贴代码

有不对的地方请大佬们指出来.

1.连接池代码


import java.util.List;
import java.util.Vector;

import org.apache.solr.client.solrj.impl.HttpSolrClient;

/**
 * solr连接池
 * 
 * @author a_bo
 * @version 创建时间:2019年4月1日 上午10:24:05
 */
public class SolrConnectionPool {

	// 空闲连接集合
	private List<HttpSolrClient> freeConnection = new Vector<HttpSolrClient>();
	// 活动连接集合
	private List<HttpSolrClient> activeConnection = new Vector<HttpSolrClient>();
	// 记录连接总数
	private static int connCount;
	// solr连接地址
	private String url;
	// 初始化连接数
	private int initialSize;
	// 最大空闲连接数
	private int maxIdleSize;
	// 最大活动连接数
	private int maxActiveSize;
	// 等待时间
	private int connTimeOut;

	public static int getConnCount() {
		return connCount;
	}

	public static void setConnCount(int connCount) {
		SolrConnectionPool.connCount = connCount;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getInitialSize() {
		return initialSize;
	}

	public void setInitialSize(int initialSize) {
		this.initialSize = initialSize;
	}

	public int getConnTimeOut() {
		return connTimeOut;
	}

	public void setConnTimeOut(int connTimeOut) {
		this.connTimeOut = connTimeOut;
	}

	public int getMaxIdleSize() {
		return maxIdleSize;
	}

	public void setMaxIdleSize(int maxIdleSize) {
		this.maxIdleSize = maxIdleSize;
	}

	public int getMaxActiveSize() {
		return maxActiveSize;
	}

	public void setMaxActiveSize(int maxActiveSize) {
		this.maxActiveSize = maxActiveSize;
	}

	// 初始化
	public void init() {
		try {
			for (int i = 0; i < initialSize; i++) {
				HttpSolrClient newConnection = newConnection();
				if (newConnection != null) {
					// 添加到空闲连接中...
					freeConnection.add(newConnection);
				}
			}
		} catch (Exception e) {
			e.getStackTrace();
			throw new RuntimeException("初始化Solr失败,请检查配置参数!");
		}
	}

	// 创建新的Connection
	private HttpSolrClient newConnection() {
		HttpSolrClient client = null;
		try {
			HttpSolrClient.Builder builder = new HttpSolrClient.Builder(url);
			client = builder.build();
		} catch (Exception e) {
			e.getStackTrace();
			throw new RuntimeException("创建Solr客户端失败!");
		}
		connCount++;
		return client;
	}

	public HttpSolrClient getConnection() {
		HttpSolrClient connection = null;
		try {
			if (connCount < maxActiveSize) {
				// 还有活动连接可以使用
				if (freeConnection.size() > 0) {
					connection = freeConnection.remove(0);
				} else {
					// 创建新的连接
					connection = newConnection();
				}
				if (isAvailable(connection)) {
					activeConnection.add(connection);
				} else {
					connCount--;
					connection = getConnection();
				}
			} else {
				synchronized (this) {
					// 大于最大活动连接,进行等待,重新获取连接
					wait(connTimeOut);
				}
				connection = getConnection();// 递归调用getConnection方法
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}

	// 判断连接是否可用
	public boolean isAvailable(HttpSolrClient connection) {
		// 此处没想好怎么判断连接是否可用
		if (connection == null) {
			return false;
		}
		return true;
	}

	// 关闭连接
	public void close(HttpSolrClient connection) {
		try {
			if (isAvailable(connection)) {
				// 判断空闲连接集合是否大于最大空闲连接数
				if (freeConnection.size() < maxIdleSize) {
					freeConnection.add(connection);
				} else {
					// 空闲连接数已经满了
					connection.close();
					connCount--;
				}
				activeConnection.remove(connection);
				synchronized (this) {
					notifyAll();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("删除连接出错!");
		}

	}

}

2.applicationContext-solr.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" default-lazy-init="false">

    <!-- solr 连接池 -->
	<bean class="com.huofutp.hdw.entity.bean.SolrConnectionPool" init-method="init">
		<property name="url" value="${solr.url}" />
		<property name="initialSize" value="${solr.initialSize}" />
		<property name="maxIdleSize" value="${solr.maxIdleSize}" />
		<property name="maxActiveSize" value="${solr.maxActiveSize}" />
		<property name="connTimeOut" value="${solr.maxWait}" />
	</bean>	
</beans>

3.solr.properties

# solr \u8FDE\u63A5\u5730\u5740
solr.url=http://192.168.1.219:8888/solr/customer
# init
solr.initialSize=5
# MaxSize
solr.maxIdleSize=5
# MaxActiveSize
solr.maxActiveSize=10
# wait time
solr.maxWait=60

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值