You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/08/10 15:00:42 UTC
svn commit: r1156161 - in
/httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn:
ManagedClientConnectionImpl.java PoolingClientConnectionManager.java
Author: olegk
Date: Wed Aug 10 13:00:41 2011
New Revision: 1156161
URL: http://svn.apache.org/viewvc?rev=1156161&view=rev
Log:
Simplified synchronization code in ManagedClientConnectionImpl
Modified:
httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn/ManagedClientConnectionImpl.java
httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn/PoolingClientConnectionManager.java
Modified: httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn/ManagedClientConnectionImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn/ManagedClientConnectionImpl.java?rev=1156161&r1=1156160&r2=1156161&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn/ManagedClientConnectionImpl.java (original)
+++ httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn/ManagedClientConnectionImpl.java Wed Aug 10 13:00:41 2011
@@ -31,8 +31,6 @@ import java.io.InterruptedIOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
@@ -56,7 +54,6 @@ import org.apache.http.protocol.HttpCont
@NotThreadSafe
class ManagedClientConnectionImpl implements ManagedClientConnection {
- private final Lock lock;
private final ClientConnectionManager manager;
private final ClientConnectionOperator operator;
private volatile HttpPoolEntry poolEntry;
@@ -77,7 +74,6 @@ class ManagedClientConnectionImpl implem
if (entry == null) {
throw new IllegalArgumentException("HTTP pool entry may not be null");
}
- this.lock = new ReentrantLock();
this.manager = manager;
this.operator = operator;
this.poolEntry = entry;
@@ -85,10 +81,6 @@ class ManagedClientConnectionImpl implem
this.duration = Long.MAX_VALUE;
}
- Lock getLock() {
- return this.lock;
- }
-
HttpPoolEntry getPoolEntry() {
return this.poolEntry;
}
@@ -106,27 +98,19 @@ class ManagedClientConnectionImpl implem
}
private OperatedClientConnection getConnection() {
- this.lock.lock();
- try {
- if (this.poolEntry == null) {
- return null;
- }
- return this.poolEntry.getConnection();
- } finally {
- this.lock.unlock();
+ HttpPoolEntry local = this.poolEntry;
+ if (local == null) {
+ return null;
}
+ return local.getConnection();
}
private OperatedClientConnection ensureConnection() {
- this.lock.lock();
- try {
- if (this.poolEntry == null) {
- throw new ConnectionShutdownException();
- }
- return this.poolEntry.getConnection();
- } finally {
- this.lock.unlock();
+ HttpPoolEntry local = this.poolEntry;
+ if (local == null) {
+ throw new ConnectionShutdownException();
}
+ return local.getConnection();
}
public void close() throws IOException {
@@ -270,9 +254,11 @@ class ManagedClientConnectionImpl implem
}
public HttpRoute getRoute() {
- synchronized (this.poolEntry) {
- return this.poolEntry.getEffectiveRoute();
+ HttpPoolEntry local = this.poolEntry;
+ if (local == null) {
+ throw new ConnectionShutdownException();
}
+ return poolEntry.getEffectiveRoute();
}
public void open(
@@ -286,8 +272,7 @@ class ManagedClientConnectionImpl implem
throw new IllegalArgumentException("HTTP parameters may not be null");
}
OperatedClientConnection conn;
- this.lock.lock();
- try {
+ synchronized (this) {
if (this.poolEntry == null) {
throw new ConnectionShutdownException();
}
@@ -296,8 +281,6 @@ class ManagedClientConnectionImpl implem
throw new IllegalStateException("Connection already open");
}
conn = this.poolEntry.getConnection();
- } finally {
- this.lock.unlock();
}
HttpHost proxy = route.getProxyHost();
@@ -307,8 +290,7 @@ class ManagedClientConnectionImpl implem
route.getLocalAddress(),
context, params);
- this.lock.lock();
- try {
+ synchronized (this) {
if (this.poolEntry == null) {
throw new InterruptedIOException();
}
@@ -318,8 +300,6 @@ class ManagedClientConnectionImpl implem
} else {
tracker.connectProxy(proxy, conn.isSecure());
}
- } finally {
- this.lock.unlock();
}
}
@@ -330,8 +310,7 @@ class ManagedClientConnectionImpl implem
}
HttpHost target;
OperatedClientConnection conn;
- this.lock.lock();
- try {
+ synchronized (this) {
if (this.poolEntry == null) {
throw new ConnectionShutdownException();
}
@@ -344,21 +323,16 @@ class ManagedClientConnectionImpl implem
}
target = tracker.getTargetHost();
conn = this.poolEntry.getConnection();
- } finally {
- this.lock.unlock();
}
conn.update(null, target, secure, params);
- this.lock.lock();
- try {
+ synchronized (this) {
if (this.poolEntry == null) {
throw new InterruptedIOException();
}
RouteTracker tracker = this.poolEntry.getTracker();
tracker.tunnelTarget(secure);
- } finally {
- this.lock.unlock();
}
}
@@ -371,8 +345,7 @@ class ManagedClientConnectionImpl implem
throw new IllegalArgumentException("HTTP parameters may not be null");
}
OperatedClientConnection conn;
- this.lock.lock();
- try {
+ synchronized (this) {
if (this.poolEntry == null) {
throw new ConnectionShutdownException();
}
@@ -381,21 +354,16 @@ class ManagedClientConnectionImpl implem
throw new IllegalStateException("Connection not open");
}
conn = this.poolEntry.getConnection();
- } finally {
- this.lock.unlock();
}
conn.update(null, next, secure, params);
- this.lock.lock();
- try {
+ synchronized (this) {
if (this.poolEntry == null) {
throw new InterruptedIOException();
}
RouteTracker tracker = this.poolEntry.getTracker();
tracker.tunnelProxy(next, secure);
- } finally {
- this.lock.unlock();
}
}
@@ -406,8 +374,7 @@ class ManagedClientConnectionImpl implem
}
HttpHost target;
OperatedClientConnection conn;
- this.lock.lock();
- try {
+ synchronized (this) {
if (this.poolEntry == null) {
throw new ConnectionShutdownException();
}
@@ -423,20 +390,15 @@ class ManagedClientConnectionImpl implem
}
target = tracker.getTargetHost();
conn = this.poolEntry.getConnection();
- } finally {
- this.lock.unlock();
}
this.operator.updateSecureConnection(conn, target, context, params);
- this.lock.lock();
- try {
+ synchronized (this) {
if (this.poolEntry == null) {
throw new InterruptedIOException();
}
RouteTracker tracker = this.poolEntry.getTracker();
tracker.layerProtocol(conn.isSecure());
- } finally {
- this.lock.unlock();
}
}
@@ -469,21 +431,17 @@ class ManagedClientConnectionImpl implem
}
public void releaseConnection() {
- this.lock.lock();
- try {
+ synchronized (this) {
if (this.poolEntry == null) {
return;
}
this.manager.releaseConnection(this, this.duration, TimeUnit.MILLISECONDS);
this.poolEntry = null;
- } finally {
- this.lock.unlock();
}
}
public void abortConnection() {
- this.lock.lock();
- try {
+ synchronized (this) {
if (this.poolEntry == null) {
return;
}
@@ -495,8 +453,6 @@ class ManagedClientConnectionImpl implem
}
this.manager.releaseConnection(this, this.duration, TimeUnit.MILLISECONDS);
this.poolEntry = null;
- } finally {
- this.lock.unlock();
}
}
Modified: httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn/PoolingClientConnectionManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn/PoolingClientConnectionManager.java?rev=1156161&r1=1156160&r2=1156161&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn/PoolingClientConnectionManager.java (original)
+++ httpcomponents/httpclient/branches/conn-mgmt-redesign/httpclient/src/main/java/org/apache/http/impl/conn/PoolingClientConnectionManager.java Wed Aug 10 13:00:41 2011
@@ -31,7 +31,6 @@ import java.util.concurrent.ExecutionExc
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
-import java.util.concurrent.locks.Lock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -229,9 +228,7 @@ public class PoolingClientConnectionMana
throw new IllegalStateException("Connection not obtained from this manager.");
}
- Lock lock = managedConn.getLock();
- lock.lock();
- try {
+ synchronized (managedConn) {
HttpPoolEntry entry = managedConn.getPoolEntry();
if (entry == null) {
return;
@@ -263,8 +260,6 @@ public class PoolingClientConnectionMana
if (this.log.isDebugEnabled()) {
this.log.debug("Connection released: " + format(entry) + formatStats(entry.getRoute()));
}
- } finally {
- lock.unlock();
}
}