}
@Override
public int primeConnections(int numConnections) throws ConnectionException, InterruptedException {
if (isReconnecting()) {
throw new HostDownException("Can't prime connections on downed host.");
}
// Don't try to create more than we're allowed
int remaining = Math.min(numConnections, config.getMaxConnsPerHost() - getActiveConnectionCount());
// Attempt to open 'count' connections and allow for MAX_PRIME_CONNECTIONS_RETRY_ATTEMPT
// retries before giving up if we can't open more.
int opened = 0;
Exception lastException = null;
for (int i = 0; opened < remaining && i < MAX_PRIME_CONNECTIONS_RETRY_ATTEMPT;) {
try {
reconnect();
opened++;
}
catch (Exception e) {
lastException = e;
Thread.sleep(PRIME_CONNECTION_DELAY);
i++;
}
}
// If no connection was opened then mark this host as down
if (remaining > 0 && opened == 0) {
this.markAsDown(null);
throw new HostDownException("Failed to prime connections", lastException);
}
return opened;
}