Skip to content
Browse files

Use multithreaded connection manager so that http client can be reuse…

…d. End result? connection pooling!
  • Loading branch information...
1 parent 99997ca commit 44da7c6b2829977f7fdd48e75cf0daf1444c5f2b @i386 i386 committed Aug 1, 2009
Showing with 102 additions and 80 deletions.
  1. +79 −72 pom.xml
  2. +22 −7 src/java/com/fourspaces/couchdb/Session.java
  3. +1 −1 src/test/com/fourspaces/couchdb/test/TestSession.java
View
151 pom.xml
@@ -1,65 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
-xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>couchdb4j</groupId>
- <artifactId>couchdb4j</artifactId>
- <name>CouchDB4J</name>
- <version>0.3.0-SNAPSHOT</version>
- <url>http://code.google.com/p/couchdb4j/</url>
-
- <build>
- <sourceDirectory>src/java</sourceDirectory>
- <testSourceDirectory>src/test</testSourceDirectory>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>couchdb4j</groupId>
+ <artifactId>couchdb4j</artifactId>
+ <name>CouchDB4J</name>
+ <version>0.3.0-i386-1</version>
+ <url>http://code.google.com/p/couchdb4j/</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.5</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.1</version>
- <scope>compile</scope>
- <exclusions>
- <exclusion>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- </exclusion>
- <exclusion>
- <groupId>logkit</groupId>
- <artifactId>logkit</artifactId>
- </exclusion>
- <exclusion>
- <groupId>avalon-framework</groupId>
- <artifactId>avalon-framework</artifactId>
- </exclusion>
- <exclusion>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- </exclusion>
- </exclusions>
-
- </dependency>
+ <build>
+ <sourceDirectory>src/java</sourceDirectory>
+ <testSourceDirectory>src/test</testSourceDirectory>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.5</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ <version>1.1.1</version>
+ <scope>compile</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>logkit</groupId>
+ <artifactId>logkit</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>avalon-framework</groupId>
+ <artifactId>avalon-framework</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ </exclusion>
+ </exclusions>
+
+ </dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
- <version>2.2.2</version>
+ <version>2.3</version>
<scope>compile</scope>
<classifier>jdk15</classifier>
<exclusions>
@@ -77,27 +77,34 @@ http://maven.apache.org/maven-v4_0_0.xsd">
</exclusion>
</exclusions>
</dependency>
- <dependency>
+ <dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.4</version>
<scope>compile</scope>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- <version>4.0-beta2</version>
- </dependency>
- </dependencies>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.0-beta2</version>
+ </dependency>
+ </dependencies>
+
+ <pluginRepositories>
+ <pluginRepository>
+ <id>codehaus.snapshots</id>
+ <name>Codehaus Maven Snapshot Repository</name>
+ <url>http://snapshots.repository.codehaus.org</url>
+ <snapshots>
+ </snapshots>
+ </pluginRepository>
+ </pluginRepositories>
- <pluginRepositories>
- <pluginRepository>
- <id>codehaus.snapshots</id>
- <name>Codehaus Maven Snapshot Repository</name>
- <url>http://snapshots.repository.codehaus.org</url>
- <snapshots>
- </snapshots>
- </pluginRepository>
- </pluginRepositories>
-
+ <distributionManagement>
+ <repository>
+ <id>nexus</id>
+ <name>My Nexus</name>
+ <url>http://localhost:8081/nexus/content/repositories/thirdparty/</url>
+ </repository>
+ </distributionManagement>
</project>
View
29 src/java/com/fourspaces/couchdb/Session.java
@@ -30,6 +30,12 @@
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.PlainSocketFactory;
+import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.BasicHttpParams;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
@@ -42,6 +48,7 @@
import org.apache.http.client.params.ClientPNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
@@ -60,7 +67,7 @@
* @author brennanjubb - HTTP-Auth username/pass
*/
public class Session {
- private static final String DEFAULT_CHARSET = HTTP.UTF_8;
+ private static final String DEFAULT_CHARSET = "UTF-8";
private static final String MIME_TYPE_JSON = "application/json";
@@ -75,6 +82,7 @@
protected CouchResponse lastResponse;
protected HttpClient httpClient;
+ protected HttpParams httpParams;
/**
* Constructor for obtaining a Session with an HTTP-AUTH username/password and (optionally) a secure connection
@@ -92,8 +100,15 @@ public Session(String host, int port, String user, String pass, boolean usesAuth
this.pass = pass;
this.usesAuth = usesAuth;
this.secure = secure;
-
- DefaultHttpClient defaultClient = new DefaultHttpClient();
+
+ httpParams = new BasicHttpParams();
+ SchemeRegistry schemeRegistry = new SchemeRegistry();
+
+ schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
+ schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
+
+ ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
+ DefaultHttpClient defaultClient = new DefaultHttpClient(connManager, httpParams);
if (user != null) {
defaultClient.getCredentialsProvider().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(user, pass) );
}
@@ -379,7 +394,7 @@ CouchResponse get(String url, String queryString) {
* @return the CouchResponse (status / error / json document)
*/
protected CouchResponse http(HttpRequestBase req) {
-
+
HttpResponse httpResponse = null;
HttpEntity entity = null;
@@ -415,17 +430,17 @@ public CouchResponse getLastResponse() {
public void setUserAgent(String ua)
{
- this.httpClient.getParams().setParameter(AllClientPNames.USER_AGENT, ua);
+ httpParams.setParameter(AllClientPNames.USER_AGENT, ua);
}
public void setConnectionTimeout(int milliseconds)
{
- httpClient.getParams().setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, milliseconds);
+ httpParams.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, milliseconds);
}
public void setSocketTimeout(int milliseconds)
{
- httpClient.getParams().setIntParameter(AllClientPNames.SO_TIMEOUT, milliseconds);
+ httpParams.setIntParameter(AllClientPNames.SO_TIMEOUT, milliseconds);
}
protected String encodeParameter(String paramValue) {
View
2 src/test/com/fourspaces/couchdb/test/TestSession.java
@@ -15,7 +15,7 @@ public static Session getTestSession() {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("couchdb-test.properties");
props.load(is);
return new Session(props.getProperty("host"),Integer.parseInt(props.getProperty("port")));
- } catch (IOException e) {
+ } catch (Exception e) {
return new Session("localhost",5984);
//throw new RuntimeException(e);
}

0 comments on commit 44da7c6

Please sign in to comment.
Something went wrong with that request. Please try again.