mirror of https://github.com/apache/jclouds.git
made default properties for NIO
git-svn-id: http://jclouds.googlecode.com/svn/trunk@1918 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
95409990b1
commit
e680ce6302
|
@ -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<C> extends BaseLifeCycle {
|
|||
* inputOnly: nothing is taken from this queue.
|
||||
*/
|
||||
protected final BlockingQueue<HttpCommandRendezvous<?>> 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<C> extends BaseLifeCycle {
|
|||
}
|
||||
|
||||
public HttpCommandConnectionPool(ExecutorService executor, Semaphore allConnections,
|
||||
BlockingQueue<HttpCommandRendezvous<?>> rendezvousQueue,
|
||||
@Named("maxConnectionReuse") int maxConnectionReuse, BlockingQueue<C> available,
|
||||
BlockingQueue<HttpCommandRendezvous<?>> rendezvousQueue, BlockingQueue<C> available,
|
||||
@Assisted URI endPoint, BaseLifeCycle... dependencies) {
|
||||
super(executor, dependencies);
|
||||
this.allConnections = allConnections;
|
||||
this.resubmitQueue = rendezvousQueue;
|
||||
this.maxConnectionReuse = maxConnectionReuse;
|
||||
this.available = available;
|
||||
this.endPoint = endPoint;
|
||||
}
|
||||
|
|
|
@ -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<C> 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<C> provideAvailablePool(
|
||||
@Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS) int max) throws Exception;
|
||||
public abstract BlockingQueue<C> provideAvailablePool() throws Exception;
|
||||
|
||||
/**
|
||||
* controls production and destruction of real connections.
|
||||
|
@ -60,8 +70,7 @@ public abstract class ConnectionPoolCommandExecutorServiceModule<C> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<NHttpConnection> provideAvailablePool(
|
||||
@Named(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS) int max) throws Exception {
|
||||
return new ArrayBlockingQueue<NHttpConnection>(max, true);
|
||||
public BlockingQueue<NHttpConnection> provideAvailablePool() throws Exception {
|
||||
return new ArrayBlockingQueue<NHttpConnection>(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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<NHtt
|
|||
private final DefaultConnectingIOReactor ioReactor;
|
||||
private final IOEventDispatch dispatch;
|
||||
private final InetSocketAddress target;
|
||||
private final int maxSessionFailures;
|
||||
|
||||
public static interface Factory extends HttpCommandConnectionPool.Factory<NHttpConnection> {
|
||||
NioHttpCommandConnectionPool create(URI endPoint);
|
||||
|
@ -84,11 +81,9 @@ public class NioHttpCommandConnectionPool extends HttpCommandConnectionPool<NHtt
|
|||
public NioHttpCommandConnectionPool(ExecutorService executor, Semaphore allConnections,
|
||||
BlockingQueue<HttpCommandRendezvous<?>> commandQueue,
|
||||
BlockingQueue<NHttpConnection> 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<NHtt
|
|||
checkArgument(port > 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);
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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) {
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue