Permalink
Browse files

14105: SolrJ client - Connection pooling

Added rudimentary connection pooling via the Apache commons-pool
library. Users will use the SolrServerPool and SolrClient classes to
perform queries.
  • Loading branch information...
kirktrue committed Apr 9, 2013
1 parent 1c10cfb commit 3508ee6eb3c203e414d9e60aeace2716d5d55e6a
View
12 pom.xml
@@ -4,7 +4,7 @@
<groupId>com.mustardgrain.solr</groupId>
<artifactId>solrj-ex</artifactId>
<packaging>jar</packaging>
- <version>1.0-alpha4</version>
+ <version>1.0-alpha5</version>
<name>SolrJ client based off of LBHttpSolrServer</name>
<dependencies>
<dependency>
@@ -22,6 +22,16 @@
<artifactId>httpmime</artifactId>
<version>4.2.1</version>
</dependency>
+ <dependency>
+ <groupId>commons-pool</groupId>
+ <artifactId>commons-pool</artifactId>
+ <version>1.6</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.4</version>
+ </dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
@@ -84,6 +84,11 @@
*
* <pre>
*
+ *
+ *
+ *
+ *
+ *
* SolrServer lbHttpSolrServer = new SolrClient(&quot;http://host1:8080/solr/&quot;,
* &quot;http://host2:8080/solr&quot;,
* &quot;http://host2:8080/solr&quot;);
@@ -113,6 +118,8 @@
*/
public class SolrClient extends SolrServer implements SolrClientMBean {
+ private final static AtomicInteger objectNameIdCounter = new AtomicInteger();
+
// keys to the maps are currently of the form "http://localhost:8983/solr"
// which should be equivalent to CommonsHttpSolrServer.getBaseURL()
private final Map<String, ServerWrapper> aliveServers = new LinkedHashMap<String, ServerWrapper>();
@@ -141,6 +148,8 @@
private final ResponseParser parser;
+ private ObjectName objectName;
+
private static final Log LOG = LogFactory.getLog(SolrClient.class);
static {
@@ -280,26 +289,38 @@ public SolrClient(HttpClient httpClient, ResponseParser parser, String... solrSe
}
private void registerMBean() {
- String name = getClass().getPackage().getName() + ":type=" + getClass().getSimpleName();
+ String name = getClass().getPackage().getName() + ":type="
+ + getClass().getSimpleName()
+ + "-"
+ + objectNameIdCounter.incrementAndGet();
try {
+ objectName = new ObjectName(name);
+ name = objectName.toString();
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
mbs.registerMBean(this, new ObjectName(name));
+
+ if (LOG.isInfoEnabled())
+ LOG.info("Registered " + objectName);
} catch (Exception e) {
if (LOG.isWarnEnabled())
LOG.warn("Couldn't add MBean " + name);
}
}
private void unregisterMBean() {
- String name = getClass().getPackage().getName() + ":type=" + getClass().getSimpleName();
+ if (objectName == null)
+ return;
try {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
- mbs.unregisterMBean(new ObjectName(name));
+ mbs.unregisterMBean(objectName);
+
+ if (LOG.isInfoEnabled())
+ LOG.info("Unregistered " + objectName);
} catch (Exception e) {
if (LOG.isWarnEnabled())
- LOG.warn("Couldn't remove MBean " + name);
+ LOG.warn("Couldn't remove MBean " + objectName.toString());
}
}
@@ -0,0 +1,96 @@
+package com.mustardgrain.solr;
+
+import java.net.InetAddress;
+import java.util.List;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.pool.PoolableObjectFactory;
+import org.apache.http.params.CoreConnectionPNames;
+import org.apache.http.params.HttpParams;
+import org.apache.solr.client.solrj.impl.HttpClientUtil;
+
+public class SolrClientFactory implements PoolableObjectFactory<SolrClient> {
+
+ private final String version;
+
+ private final String userAgent;
+
+ private final List<String> urls;
+
+ private final int connectTimeout;
+
+ private final int soTimeout;
+
+ private final boolean staleConnectionCheck;
+
+ private final int statsInterval;
+
+ private final boolean disableConnectonHeader;
+
+ public SolrClientFactory(String version,
+ String userAgent,
+ List<String> urls,
+ int connectTimeout,
+ int soTimeout,
+ boolean staleConnectionCheck,
+ int statsInterval,
+ boolean disableConnectonHeader) {
+ this.version = version;
+ this.userAgent = userAgent;
+ this.urls = urls;
+ this.connectTimeout = connectTimeout;
+ this.soTimeout = soTimeout;
+ this.staleConnectionCheck = staleConnectionCheck;
+ this.statsInterval = statsInterval;
+ this.disableConnectonHeader = disableConnectonHeader;
+ }
+
+ @Override
+ public SolrClient makeObject() throws Exception {
+ SolrClient solrServer = new SolrClient(urls.toArray(new String[0]));
+
+ if (connectTimeout != -1)
+ solrServer.setConnectionTimeout(connectTimeout);
+
+ if (soTimeout != -1)
+ solrServer.setSoTimeout(soTimeout);
+
+ HttpParams params = solrServer.getHttpClient().getParams();
+ params.setBooleanParameter(HttpClientUtil.PROP_USE_RETRY, false);
+ params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, staleConnectionCheck);
+
+ if (statsInterval != -1)
+ solrServer.setStatsInterval(statsInterval);
+
+ if (!disableConnectonHeader)
+ solrServer.disableConnectonHeader();
+
+ if (!StringUtils.isBlank(userAgent)) {
+ solrServer.setUserAgent(userAgent);
+ } else {
+ solrServer.setUserAgent(InetAddress.getLocalHost().getHostName() + ":"
+ + (!StringUtils.isBlank(version) ? version : "n/a"));
+ }
+
+ return solrServer;
+ }
+
+ @Override
+ public void destroyObject(SolrClient obj) throws Exception {
+ obj.shutdown();
+ }
+
+ @Override
+ public boolean validateObject(SolrClient obj) {
+ return true;
+ }
+
+ @Override
+ public void activateObject(SolrClient obj) throws Exception {
+ }
+
+ @Override
+ public void passivateObject(SolrClient obj) throws Exception {
+ }
+
+}
@@ -0,0 +1,32 @@
+package com.mustardgrain.solr;
+
+import org.apache.commons.pool.ObjectPool;
+import org.apache.commons.pool.PoolableObjectFactory;
+import org.apache.commons.pool.impl.GenericObjectPool;
+import org.apache.solr.client.solrj.SolrServer;
+
+public class SolrServerPool<T extends SolrServer> {
+
+ private ObjectPool<T> pool;
+
+ public SolrServerPool(PoolableObjectFactory<T> factory) {
+ this(factory, new GenericObjectPool.Config());
+ }
+
+ public SolrServerPool(PoolableObjectFactory<T> factory, GenericObjectPool.Config config) {
+ pool = new GenericObjectPool<T>(factory, config);
+ }
+
+ public T get() throws Exception {
+ return pool.borrowObject();
+ }
+
+ public void put(T solrServer) throws Exception {
+ pool.returnObject(solrServer);
+ }
+
+ public void close() throws Exception {
+ pool.close();
+ }
+
+}

0 comments on commit 3508ee6

Please sign in to comment.