summaryrefslogtreecommitdiff
path: root/same/src
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-31 14:28:17 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-31 14:28:17 +0100
commitce13c0a718af8ae1e613c64d648dad6044518542 (patch)
treee823c355c1167d0c4e8c69654fc2b67f43e51535 /same/src
parenteacd71d700c402e79edec9427b2fe1fb5e6b278e (diff)
Reuse HTTP connections.
Diffstat (limited to 'same/src')
-rw-r--r--same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java17
1 files changed, 15 insertions, 2 deletions
diff --git a/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java b/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java
index 92b7588..2767003 100644
--- a/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java
+++ b/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java
@@ -7,6 +7,9 @@ import com.orbekk.paxos.PaxosService;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -18,6 +21,8 @@ import org.slf4j.LoggerFactory;
public class ConnectionManagerImpl implements ConnectionManager {
private int connectionTimeout;
private int readTimeout;
+ private Map<String, MyJsonRpcHttpClient> connectionCache =
+ new HashMap<String, MyJsonRpcHttpClient>();
private Logger logger = LoggerFactory.getLogger(getClass());
@@ -30,11 +35,19 @@ public class ConnectionManagerImpl implements ConnectionManager {
this.readTimeout = readTimeout;
}
+ private MyJsonRpcHttpClient getConnection(String url)
+ throws MalformedURLException {
+ if (!connectionCache.containsKey(url)) {
+ connectionCache.put(url, new MyJsonRpcHttpClient(new URL(url),
+ connectionTimeout, readTimeout));
+ }
+ return connectionCache.get(url);
+ }
+
private <T>T getClassProxy(String url, Class<T> clazz) {
T service = null;
try {
- MyJsonRpcHttpClient client = new MyJsonRpcHttpClient(new URL(url),
- connectionTimeout, readTimeout);
+ MyJsonRpcHttpClient client = getConnection(url);
service = ProxyUtil.createProxy(
this.getClass().getClassLoader(),
clazz,