@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.
|
protected Supplier<Concourse> |
supplier
|
| Modifier | Constructor and Description |
|---|---|
protected |
ConnectionPool(Concourse concourse,
int poolSize)
Construct a new instance that provides
Concourse connections that
copy the connection information from the provided concourse
handler. |
protected |
ConnectionPool(String host,
int port,
String username,
String password,
int poolSize)
Deprecated.
|
protected |
ConnectionPool(String host,
int port,
String username,
String password,
String environment,
int poolSize)
Deprecated.
|
protected |
ConnectionPool(Supplier<Concourse> supplier,
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(Concourse concourse)
Returns a
ConnectionPool populated with handlers that
copy the connection
information of the provided concourse instance. |
static ConnectionPool |
newCachedConnectionPool(Path... configFiles)
Return a
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance described in the
configFiles on behalf of the user identified in the
configFiles, but will try to use previously created connections
before establishing new ones for any request. |
static ConnectionPool |
newCachedConnectionPool(String config)
Return a
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance described in the
config on behalf of the user identified in the config,
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(Concourse concourse,
int poolSize)
Returns a
ConnectionPool populated with handlers that
copy the connection
information of the provided concourse instance. |
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(int poolSize,
Path... configFiles)
Return a new
ConnectionPool with a fixed number of connections to
the Concourse instance defined in the configFiles on behalf of
the user defined in the configFiles. |
static ConnectionPool |
newFixedConnectionPool(String config,
int poolSize)
Return a new
ConnectionPool with a fixed number of connections to
the Concourse instance defined in the configFiles on behalf of
the user defined in the configFiles. |
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 final Queue<Concourse> available
protected ConnectionPool(Concourse concourse, int poolSize)
Concourse connections that
copy the connection information from the provided concourse
handler.
NOTE:This constructor is provided for subclasses to
conveniently implement connection copying while abstracting away the
details of how to construct an appropriate Supplier.
concourse - poolSize - @Deprecated protected ConnectionPool(String host, int port, String username, String password, int poolSize)
host - port - username - password - poolSize - @Deprecated protected ConnectionPool(String host, int port, String username, String password, String environment, int poolSize)
host - port - username - password - environment - 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(Concourse concourse)
ConnectionPool populated with handlers that
copy the connection
information of the provided concourse instance. The pool has no
limit on the number of connections it can manage, but will attempt to use
previously created connections before establishing new ones on request.
NOTE: The provided concourse connection will not
be a member of the returned ConnectionPool and its status will
not affect the status of any connections managed by the pool.
concourse - the Concourse connection to copy when populating
the ConnectionPoolConnectionPoolpublic static ConnectionPool newCachedConnectionPool(Path... configFiles)
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance described in the
configFiles on behalf of the user identified in the
configFiles, but will try to use previously created connections
before establishing new ones for any request.config - public static ConnectionPool newCachedConnectionPool(String config)
ConnectionPool that has no limit on the number of
connections it can manage to the Concourse instance described in the
config on behalf of the user identified in the config,
but will try to use previously created connections before establishing
new ones for any request.config - 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(Concourse concourse, int poolSize)
ConnectionPool populated with handlers that
copy the connection
information of the provided concourse instance. The pool will
contain poolSize connections. If all connections are active,
subsequent requests will block until a connection is returned.
NOTE: The provided concourse connection will not
be a member of the returned ConnectionPool and its status will
not affect the status of any connections managed by the pool.
concourse - the Concourse connection to copy when populating
the ConnectionPoolpoolSize - the number of connections in the poolConnectionPoolpublic 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(int poolSize, Path... configFiles)
ConnectionPool with a fixed number of connections to
the Concourse instance defined in the configFiles on behalf of
the user defined in the configFiles.
If all the connections from the pool are active, subsequent request attempts will block until a connection is returned.
poolSize - config - public static ConnectionPool newFixedConnectionPool(String config, int poolSize)
ConnectionPool with a fixed number of connections to
the Concourse instance defined in the configFiles on behalf of
the user defined in the configFiles.
If all the connections from the pool are active, subsequent request attempts will block until a connection is returned.
config - 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.