public class SimpleHostConnectionPool<CL> extends java.lang.Object implements HostConnectionPool<CL>
HostConnectionPool interface
Salient Features
The class provides a bunch of counters for visibility into the connection pool status e.g no of
that are available / active / pending / blocked etc.
This class also provides an async mechanism to create / prime and borrow Connection(s) using a LinkedBlockingQueue
Clients borrowing connections can wait at the end of the queue for a connection to be available. They send a tryOpenAsync() request
to create a new connection before waiting, but don't necessarily wait for the same connection to be opened, since they could be unblocked by
another client returning a previously used Connection
The class also provides a markAsDown(ConnectionException) method which helps purge all connections and then
attempts to init a new set of connections to the host.
| Modifier and Type | Class and Description |
|---|---|
static interface |
SimpleHostConnectionPool.Listener<CL>
Interface to notify the owning connection pool of up/down state changes.
|
| Modifier and Type | Field and Description |
|---|---|
protected ConnectionPoolConfiguration |
config |
| Constructor and Description |
|---|
SimpleHostConnectionPool(Host host,
ConnectionFactory<CL> factory,
ConnectionPoolMonitor monitor,
ConnectionPoolConfiguration config,
SimpleHostConnectionPool.Listener<CL> listener) |
| Modifier and Type | Method and Description |
|---|---|
void |
addLatencySample(long latency,
long now)
Add a single latency sample after an operation on a connection belonging
to this pool
|
Connection<CL> |
borrowConnection(int timeout)
Create a connection as long the max hasn't been reached
|
boolean |
closeConnection(Connection<CL> connection)
Close this connection and update internal state
|
int |
getActiveConnectionCount() |
int |
getBlockedThreadCount() |
int |
getBusyConnectionCount() |
int |
getClosedConnectionCount() |
int |
getConnectAttemptCount() |
int |
getErrorsSinceLastSuccess() |
int |
getFailedOpenConnectionCount() |
Host |
getHost() |
int |
getIdleConnectionCount() |
int |
getOpenedConnectionCount() |
int |
getPendingConnectionCount() |
double |
getScore() |
boolean |
isActive() |
boolean |
isReconnecting() |
boolean |
isShutdown() |
void |
markAsDown(ConnectionException reason)
Mark the host as down.
|
int |
primeConnections(int numConnections)
Create numConnections new connections and add them to the
|
boolean |
returnConnection(Connection<CL> connection)
Return a connection to this host
|
void |
shutdown()
Completely shut down this connection pool as part of a client shutdown
|
java.lang.String |
toString() |
protected final ConnectionPoolConfiguration config
public SimpleHostConnectionPool(Host host, ConnectionFactory<CL> factory, ConnectionPoolMonitor monitor, ConnectionPoolConfiguration config, SimpleHostConnectionPool.Listener<CL> listener)
public int primeConnections(int numConnections)
throws ConnectionException,
java.lang.InterruptedException
HostConnectionPoolprimeConnections in interface HostConnectionPool<CL>ConnectionExceptionjava.lang.InterruptedExceptionpublic Connection<CL> borrowConnection(int timeout) throws ConnectionException
borrowConnection in interface HostConnectionPool<CL>timeout - - Max wait timeout if max connections have been allocated and
pool is empty. 0 to throw a MaxConnsPerHostReachedException.TimeoutException - if timeout specified and no new connection is available
MaxConnsPerHostReachedException if max connections created
and no timeout was specifiedConnectionExceptionpublic boolean returnConnection(Connection<CL> connection)
returnConnection in interface HostConnectionPool<CL>connection - public boolean closeConnection(Connection<CL> connection)
HostConnectionPoolcloseConnection in interface HostConnectionPool<CL>public void markAsDown(ConnectionException reason)
markAsDown in interface HostConnectionPool<CL>public void shutdown()
HostConnectionPoolshutdown in interface HostConnectionPool<CL>public boolean isShutdown()
isShutdown in interface HostConnectionPool<CL>public boolean isReconnecting()
isReconnecting in interface HostConnectionPool<CL>public Host getHost()
getHost in interface HostConnectionPool<CL>public int getActiveConnectionCount()
getActiveConnectionCount in interface HostConnectionPool<CL>public int getIdleConnectionCount()
getIdleConnectionCount in interface HostConnectionPool<CL>public int getPendingConnectionCount()
getPendingConnectionCount in interface HostConnectionPool<CL>public int getBlockedThreadCount()
getBlockedThreadCount in interface HostConnectionPool<CL>public int getOpenedConnectionCount()
getOpenedConnectionCount in interface HostConnectionPool<CL>public int getFailedOpenConnectionCount()
getFailedOpenConnectionCount in interface HostConnectionPool<CL>public int getClosedConnectionCount()
getClosedConnectionCount in interface HostConnectionPool<CL>public int getConnectAttemptCount()
getConnectAttemptCount in interface HostConnectionPool<CL>public int getBusyConnectionCount()
getBusyConnectionCount in interface HostConnectionPool<CL>public double getScore()
getScore in interface HostConnectionPool<CL>public void addLatencySample(long latency,
long now)
HostConnectionPooladdLatencySample in interface HostConnectionPool<CL>public int getErrorsSinceLastSuccess()
getErrorsSinceLastSuccess in interface HostConnectionPool<CL>public java.lang.String toString()
toString in class java.lang.Objectpublic boolean isActive()
isActive in interface HostConnectionPool<CL>