This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Kirk True
committed
Apr 9, 2013
1 parent
1c10cfb
commit 3508ee6
Showing
4 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/com/mustardgrain/solr/SolrClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |