diff --git a/core/src/main/java/org/jclouds/http/pool/HttpCommandConnectionPool.java b/core/src/main/java/org/jclouds/http/pool/HttpCommandConnectionPool.java index 0fb2858b9a..041bd6d395 100644 --- a/core/src/main/java/org/jclouds/http/pool/HttpCommandConnectionPool.java +++ b/core/src/main/java/org/jclouds/http/pool/HttpCommandConnectionPool.java @@ -31,11 +31,13 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; +import javax.inject.Inject; +import javax.inject.Named; + import org.jclouds.http.HttpCommandRendezvous; import org.jclouds.lifecycle.BaseLifeCycle; import com.google.inject.assistedinject.Assisted; -import javax.inject.Named; /** * // TODO: Adrian: Document this! @@ -63,11 +65,18 @@ public abstract class HttpCommandConnectionPool extends BaseLifeCycle { * inputOnly: nothing is taken from this queue. */ protected final BlockingQueue> resubmitQueue; - protected final int maxConnectionReuse; protected final AtomicInteger currentSessionFailures = new AtomicInteger(0); protected volatile boolean hitBottom = false; protected final URI endPoint; + @Inject(optional = true) + @Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTION_REUSE) + protected int maxConnectionReuse = 75; + + @Inject(optional = true) + @Named(PoolConstants.PROPERTY_POOL_MAX_SESSION_FAILURES) + protected int maxSessionFailures = 2; + public URI getEndPoint() { return endPoint; } @@ -77,13 +86,11 @@ public abstract class HttpCommandConnectionPool extends BaseLifeCycle { } public HttpCommandConnectionPool(ExecutorService executor, Semaphore allConnections, - BlockingQueue> rendezvousQueue, - @Named("maxConnectionReuse") int maxConnectionReuse, BlockingQueue available, + BlockingQueue> rendezvousQueue, BlockingQueue available, @Assisted URI endPoint, BaseLifeCycle... dependencies) { super(executor, dependencies); this.allConnections = allConnections; this.resubmitQueue = rendezvousQueue; - this.maxConnectionReuse = maxConnectionReuse; this.available = available; this.endPoint = endPoint; } diff --git a/core/src/main/java/org/jclouds/http/pool/config/ConnectionPoolCommandExecutorServiceModule.java b/core/src/main/java/org/jclouds/http/pool/config/ConnectionPoolCommandExecutorServiceModule.java index b8e14bc2b9..ae5b99b0a2 100644 --- a/core/src/main/java/org/jclouds/http/pool/config/ConnectionPoolCommandExecutorServiceModule.java +++ b/core/src/main/java/org/jclouds/http/pool/config/ConnectionPoolCommandExecutorServiceModule.java @@ -27,12 +27,14 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.Semaphore; import java.util.concurrent.atomic.AtomicInteger; +import javax.inject.Inject; +import javax.inject.Named; + import org.jclouds.http.pool.PoolConstants; import org.jclouds.lifecycle.config.LifeCycleModule; import com.google.inject.AbstractModule; import com.google.inject.Provides; -import javax.inject.Named; /** * @@ -40,15 +42,23 @@ import javax.inject.Named; */ public abstract class ConnectionPoolCommandExecutorServiceModule extends AbstractModule { + @Inject(optional = true) + @Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS) + protected int maxConnections = 12; + + @Inject(optional = true) + @Named(PoolConstants.PROPERTY_POOL_IO_WORKER_THREADS) + protected int maxWorkerThreads = 12; + protected void configure() { install(new LifeCycleModule()); bind(AtomicInteger.class).toInstance(new AtomicInteger());// max errors + binder().requestInjection(this); } @Provides // @Singleton per uri... - public abstract BlockingQueue provideAvailablePool( - @Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS) int max) throws Exception; + public abstract BlockingQueue provideAvailablePool() throws Exception; /** * controls production and destruction of real connections. @@ -60,8 +70,7 @@ public abstract class ConnectionPoolCommandExecutorServiceModule extends Abst */ @Provides // @Singleton per uri... - public Semaphore provideTotalConnectionSemaphore( - @Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS) int max) throws Exception { - return new Semaphore(max, true); + public Semaphore provideTotalConnectionSemaphore() throws Exception { + return new Semaphore(maxConnections, true); } } diff --git a/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/config/NioTransformingHttpCommandExecutorServiceModule.java b/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/config/NioTransformingHttpCommandExecutorServiceModule.java index 4a0a6144d7..9544eb9f9d 100644 --- a/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/config/NioTransformingHttpCommandExecutorServiceModule.java +++ b/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/config/NioTransformingHttpCommandExecutorServiceModule.java @@ -28,7 +28,6 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import javax.inject.Inject; -import javax.inject.Named; import javax.inject.Singleton; import org.apache.http.ConnectionReuseStrategy; @@ -58,7 +57,6 @@ import org.jclouds.http.config.ConfiguresHttpCommandExecutorService; import org.jclouds.http.httpnio.pool.NioHttpCommandConnectionPool; import org.jclouds.http.httpnio.pool.NioHttpCommandExecutionHandler; import org.jclouds.http.httpnio.pool.NioTransformingHttpCommandExecutorService; -import org.jclouds.http.pool.PoolConstants; import org.jclouds.http.pool.config.ConnectionPoolCommandExecutorServiceModule; import com.google.inject.Provides; @@ -140,17 +138,15 @@ public class NioTransformingHttpCommandExecutorServiceModule extends } @Override - public BlockingQueue provideAvailablePool( - @Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS) int max) throws Exception { - return new ArrayBlockingQueue(max, true); + public BlockingQueue provideAvailablePool() throws Exception { + return new ArrayBlockingQueue(maxConnections, true); } @Provides // @Singleton per uri... - public DefaultConnectingIOReactor provideDefaultConnectingIOReactor( - @Named(PoolConstants.PROPERTY_POOL_IO_WORKER_THREADS) int ioWorkerThreads, - HttpParams params) throws IOReactorException { - return new DefaultConnectingIOReactor(ioWorkerThreads, params); + public DefaultConnectingIOReactor provideDefaultConnectingIOReactor(HttpParams params) + throws IOReactorException { + return new DefaultConnectingIOReactor(maxWorkerThreads, params); } } \ No newline at end of file diff --git a/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/pool/NioHttpCommandConnectionPool.java b/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/pool/NioHttpCommandConnectionPool.java index a116426653..e97e54f70d 100644 --- a/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/pool/NioHttpCommandConnectionPool.java +++ b/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/pool/NioHttpCommandConnectionPool.java @@ -36,6 +36,7 @@ import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import javax.inject.Inject; import javax.net.ssl.SSLContext; import org.apache.http.HttpException; @@ -55,12 +56,9 @@ import org.jclouds.http.TransformingHttpCommand; import org.jclouds.http.TransformingHttpCommandExecutorService; import org.jclouds.http.pool.HttpCommandConnectionHandle; import org.jclouds.http.pool.HttpCommandConnectionPool; -import org.jclouds.http.pool.PoolConstants; import com.google.common.annotations.VisibleForTesting; -import javax.inject.Inject; import com.google.inject.assistedinject.Assisted; -import javax.inject.Named; /** * Connection Pool for HTTP requests that utilizes Apache HTTPNio @@ -74,7 +72,6 @@ public class NioHttpCommandConnectionPool extends HttpCommandConnectionPool { NioHttpCommandConnectionPool create(URI endPoint); @@ -84,11 +81,9 @@ public class NioHttpCommandConnectionPool extends HttpCommandConnectionPool> commandQueue, BlockingQueue available, AsyncNHttpClientHandler clientHandler, - DefaultConnectingIOReactor ioReactor, HttpParams params, - @Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTION_REUSE) int maxConnectionReuse, - @Named(PoolConstants.PROPERTY_POOL_MAX_SESSION_FAILURES) int maxSessionFailures, - @Assisted URI endPoint) throws Exception { - super(executor, allConnections, commandQueue, maxConnectionReuse, available, endPoint); + DefaultConnectingIOReactor ioReactor, HttpParams params, @Assisted URI endPoint) + throws Exception { + super(executor, allConnections, commandQueue, available, endPoint); String host = checkNotNull(checkNotNull(endPoint, "endPoint").getHost(), String.format( "Host null for endpoint %s", endPoint)); int port = endPoint.getPort(); @@ -104,7 +99,6 @@ public class NioHttpCommandConnectionPool extends HttpCommandConnectionPool 0, String.format("Port %d not in range for endpoint %s", endPoint .getPort(), endPoint)); this.ioReactor = ioReactor; - this.maxSessionFailures = maxSessionFailures; this.sessionCallback = new NHttpClientConnectionPoolSessionRequestCallback(); this.target = new InetSocketAddress(host, port); clientHandler.setEventListener(this); diff --git a/extensions/httpnio/src/test/java/org/jclouds/http/httpnio/pool/NioHttpCommandConnectionPoolTest.java b/extensions/httpnio/src/test/java/org/jclouds/http/httpnio/pool/NioHttpCommandConnectionPoolTest.java index c55122b998..cdda52be78 100644 --- a/extensions/httpnio/src/test/java/org/jclouds/http/httpnio/pool/NioHttpCommandConnectionPoolTest.java +++ b/extensions/httpnio/src/test/java/org/jclouds/http/httpnio/pool/NioHttpCommandConnectionPoolTest.java @@ -44,28 +44,28 @@ public class NioHttpCommandConnectionPoolTest { public void testConstructorGoodPort() throws Exception { NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null, createNiceMock(AsyncNHttpClientHandler.class), null, - createNiceMock(HttpParams.class), 0, 0, URI.create("http://localhost:80")); + createNiceMock(HttpParams.class), URI.create("http://localhost:80")); assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 80)); } public void testConstructorGoodSSLPort() throws Exception { NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null, createNiceMock(AsyncNHttpClientHandler.class), null, - createNiceMock(HttpParams.class), 0, 0, URI.create("https://localhost:443")); + createNiceMock(HttpParams.class), URI.create("https://localhost:443")); assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 443)); } public void testConstructorUnspecifiedPort() throws Exception { NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null, createNiceMock(AsyncNHttpClientHandler.class), null, - createNiceMock(HttpParams.class), 0, 0, URI.create("http://localhost")); + createNiceMock(HttpParams.class), URI.create("http://localhost")); assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 80)); } public void testConstructorUnspecifiedSSLPort() throws Exception { NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null, createNiceMock(AsyncNHttpClientHandler.class), null, - createNiceMock(HttpParams.class), 0, 0, URI.create("https://localhost")); + createNiceMock(HttpParams.class), URI.create("https://localhost")); assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 443)); } @@ -73,13 +73,13 @@ public class NioHttpCommandConnectionPoolTest { public void testConstructorNullURI() throws Exception { new NioHttpCommandConnectionPool(null, null, null, null, createNiceMock(AsyncNHttpClientHandler.class), null, - createNiceMock(HttpParams.class), 0, 0, null); + createNiceMock(HttpParams.class), null); } public void testConstructorWeirdName() throws Exception { NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null, createNiceMock(AsyncNHttpClientHandler.class), null, - createNiceMock(HttpParams.class), 0, 0, URI + createNiceMock(HttpParams.class), URI .create("http://adriancole.blobstore1138eu.s3-external-3.amazonaws.com")); assertEquals(pool.getTarget(), new InetSocketAddress( "adriancole.blobstore1138eu.s3-external-3.amazonaws.com", 80)); diff --git a/extensions/httpnio/src/test/java/org/jclouds/http/httpnio/pool/NioTransformingHttpCommandExecutorServiceTest.java b/extensions/httpnio/src/test/java/org/jclouds/http/httpnio/pool/NioTransformingHttpCommandExecutorServiceTest.java index 651d4c8f3b..bf79ba693e 100644 --- a/extensions/httpnio/src/test/java/org/jclouds/http/httpnio/pool/NioTransformingHttpCommandExecutorServiceTest.java +++ b/extensions/httpnio/src/test/java/org/jclouds/http/httpnio/pool/NioTransformingHttpCommandExecutorServiceTest.java @@ -27,7 +27,6 @@ import java.util.Properties; import org.jclouds.http.BaseHttpCommandExecutorServiceTest; import org.jclouds.http.httpnio.config.NioTransformingHttpCommandExecutorServiceModule; -import org.jclouds.http.pool.PoolConstants; import org.testng.annotations.Test; import com.google.inject.Module; @@ -38,18 +37,16 @@ import com.google.inject.Module; * @author Adrian Cole */ @Test -public class NioTransformingHttpCommandExecutorServiceTest extends BaseHttpCommandExecutorServiceTest { - - protected void addConnectionProperties(Properties properties) { - properties.setProperty(PoolConstants.PROPERTY_POOL_MAX_CONNECTION_REUSE, "75"); - properties.setProperty(PoolConstants.PROPERTY_POOL_MAX_SESSION_FAILURES, "2"); - properties.setProperty(PoolConstants.PROPERTY_POOL_REQUEST_INVOKER_THREADS, "1"); - properties.setProperty(PoolConstants.PROPERTY_POOL_IO_WORKER_THREADS, "2"); - properties.setProperty(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS, "12"); - } +public class NioTransformingHttpCommandExecutorServiceTest extends + BaseHttpCommandExecutorServiceTest { protected Module createClientModule() { return new NioTransformingHttpCommandExecutorServiceModule(); } + @Override + protected void addConnectionProperties(Properties props) { + + } + } \ No newline at end of file