@ThreadSafe public abstract class ConnectionPool extends Object implements AutoCloseable
A ConnectionPool manages multiple concurrent connections to Concourse
for a single user. Generally speaking, a ConnectionPool is handy in long
running server API endpoints that want to reduce the overhead of creating a
new client connection for every asynchronous request. Using a ConnectionPool,
those applications can maintain a finite number of connections while ensuring
the resources are disconnected gracefully when necessary.
Concourse concourse = pool.request();
try {
...
}
finally {
pool.release(concourse);
}
...
// All the threads with connections are done
pool.close()
| Modifier and Type | Field and Description |
|---|---|
protected Queue<Concourse> |
available
A FIFO queue of connections that are available to be leased.
|
protected static int |
DEFAULT_POOL_SIZE
The default connection pool size.
|
| Modifier | Constructor and Description |
|---|---|
protected |
ConnectionPool(String host,
int port,
String username,
String password,
int poolSize)
Construct a new instance.
|
protected |
ConnectionPool(String host,
int port,
String username,
String password,
String environment,
int poolSize)
Construct a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
protected abstract Queue<Concourse> |
buildQueue(int size)
Return the
Queue that will hold the connections. |
void |
close() |
protected void |
forceClose()
Force the connection pool to close regardless of whether it is or is not
in a
closable state. |
protected abstract Concourse |
getConnection()
Get a connection from the queue of
available ones. |
boolean |
hasAvailableConnection()
Return
true if the pool has any available connections. |
boolean |
isClosed()
Return
true if this ConnectionPool has been closed. |
static ConnectionPool |
newCachedConnectionPool()
Return a
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance described in the
concourse_client.prefs file located in the working directory or
the default connection info if no such file exists, but will try to use
previously created connections before establishing new ones for any
request. |
static ConnectionPool |
newCachedConnectionPool(String prefs)
Return a
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance at host:
port on behalf of the user identified by username and
password, but will try to use previously created connections
before establishing new ones for any request. |
static ConnectionPool |
newCachedConnectionPool(String host,
int port,
String username,
String password)
Return a
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance defined in the client
prefs on behalf of the user defined in the client prefs,
but will try to use previously created connections before establishing
new ones for any request. |
static ConnectionPool |
newCachedConnectionPool(String host,
int port,
String username,
String password,
String environment)
Return a
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance defined in the client
prefs on behalf of the user defined in the client prefs,
but will try to use previously created connections before establishing
new ones for any request. |
static ConnectionPool |
newConnectionPool(String prefs)
Deprecated.
As of version 0.3.2, replaced by
newFixedConnectionPool(String, int). |
static ConnectionPool |
newConnectionPool(String prefs,
int poolSize)
Deprecated.
As of version 0.3.2, replaced by
newFixedConnectionPool(String, int). |
static ConnectionPool |
newConnectionPool(String host,
int port,
String username,
String password)
Deprecated.
As of version 0.3.2, replaced by
newFixedConnectionPool(String, int, String, String, int)
. |
static ConnectionPool |
newConnectionPool(String host,
int port,
String username,
String password,
int poolSize)
Deprecated.
As of version 0.3.2, replaced by
newFixedConnectionPool(String, int, String, String, int)
. |
static ConnectionPool |
newFixedConnectionPool(int poolSize)
Return a new
ConnectionPool with a fixed number of connections to
the Concourse instance defined in the concourse_client.prefs file
located in the working directory or using the default connection info if
no such file exists. |
static ConnectionPool |
newFixedConnectionPool(String prefs,
int poolSize)
Return a new
ConnectionPool with a fixed number of
connections to the Concourse instance defined in the client prefs
on behalf of the user defined in the client prefs. |
static ConnectionPool |
newFixedConnectionPool(String host,
int port,
String username,
String password,
int poolSize)
Return a new
ConnectionPool with a fixed number of connections to
the Concourse instance at host:port on behalf of the user
identified by username and password. |
static ConnectionPool |
newFixedConnectionPool(String host,
int port,
String username,
String password,
String environment,
int poolSize)
Return a new
ConnectionPool with a fixed number of connections to
the Concourse instance at host:port on behalf of the user
identified by username and password. |
void |
release(Concourse connection)
Return a previously requested connection back to the pool.
|
Concourse |
request()
Request a connection from the pool and block until one is available and
returned.
|
protected static final int DEFAULT_POOL_SIZE
protected ConnectionPool(String host, int port, String username, String password, int poolSize)
host - port - username - password - poolSize - public static ConnectionPool newCachedConnectionPool()
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance described in the
concourse_client.prefs file located in the working directory or
the default connection info if no such file exists, but will try to use
previously created connections before establishing new ones for any
request.public static ConnectionPool newCachedConnectionPool(String prefs)
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance at host:
port on behalf of the user identified by username and
password, but will try to use previously created connections
before establishing new ones for any request.prefs - public static ConnectionPool newCachedConnectionPool(String host, int port, String username, String password)
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance defined in the client
prefs on behalf of the user defined in the client prefs,
but will try to use previously created connections before establishing
new ones for any request.host - port - username - password - public static ConnectionPool newCachedConnectionPool(String host, int port, String username, String password, String environment)
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance defined in the client
prefs on behalf of the user defined in the client prefs,
but will try to use previously created connections before establishing
new ones for any request.host - port - username - password - environment - @Deprecated public static ConnectionPool newConnectionPool(String prefs)
newFixedConnectionPool(String, int).ConnectionPool that provides connections to the
Concourse instance defined in the client prefs on behalf of the
user defined in the client prefs.prefs - @Deprecated public static ConnectionPool newConnectionPool(String prefs, int poolSize)
newFixedConnectionPool(String, int).ConnectionPool that provides poolSize
connections to the Concourse instance defined in the client prefs
on behalf of the user defined in the client prefs.prefs - poolSize - @Deprecated public static ConnectionPool newConnectionPool(String host, int port, String username, String password)
newFixedConnectionPool(String, int, String, String, int)
.ConnectionPool that provides connections to the
Concourse instance at host:port on behalf of the user
identified by username and password.host - port - username - password - @Deprecated public static ConnectionPool newConnectionPool(String host, int port, String username, String password, int poolSize)
newFixedConnectionPool(String, int, String, String, int)
.ConnectionPool that provides poolSize
connections to the Concourse instance at host:port on
behalf of the user identified by username and password.host - port - username - password - poolSize - public static ConnectionPool newFixedConnectionPool(int poolSize)
ConnectionPool with a fixed number of connections to
the Concourse instance defined in the concourse_client.prefs file
located in the working directory or using the default connection info if
no such file exists.
If all the connections from the pool are active, subsequent request attempts will block until a connection is returned.
poolSize - public static ConnectionPool newFixedConnectionPool(String prefs, int poolSize)
ConnectionPool with a fixed number of
connections to the Concourse instance defined in the client prefs
on behalf of the user defined in the client prefs.
If all the connections from the pool are active, subsequent request attempts will block until a connection is returned.
prefs - poolSize - public static ConnectionPool newFixedConnectionPool(String host, int port, String username, String password, int poolSize)
ConnectionPool with a fixed number of connections to
the Concourse instance at host:port on behalf of the user
identified by username and password.
If all the connections from the pool are active, subsequent request attempts will block until a connection is returned.
host - port - username - password - poolSize - public static ConnectionPool newFixedConnectionPool(String host, int port, String username, String password, String environment, int poolSize)
ConnectionPool with a fixed number of connections to
the Concourse instance at host:port on behalf of the user
identified by username and password.
If all the connections from the pool are active, subsequent request attempts will block until a connection is returned.
host - port - username - password - environment - poolSize - public void close()
throws Exception
close in interface AutoCloseableExceptionpublic boolean hasAvailableConnection()
true if the pool has any available connections.true if there are one or more available connectionspublic boolean isClosed()
true if this ConnectionPool has been closed.public void release(Concourse connection)
connection - public Concourse request()
protected abstract Queue<Concourse> buildQueue(int size)
Queue that will hold the connections.size - protected void forceClose()
closable state.protected abstract Concourse getConnection()
available ones. The subclass
should use the correct method depending upon whether this method should
block or not.