@VisibleForTesting
ServicePool<S> buildInternal() {
checkNotNull(_serviceFactory);
checkNotNull(_metrics);
HostDiscovery hostDiscovery = findHostDiscovery(_serviceName);
boolean shutdownHealthCheckExecutorOnClose = (_healthCheckExecutor == null);
try {
if (_cachingPolicy == null) {
_cachingPolicy = ServiceCachingPolicyBuilder.NO_CACHING;
}
if (_healthCheckExecutor == null) {
_healthCheckExecutor = Executors.newScheduledThreadPool(DEFAULT_NUM_HEALTH_CHECK_THREADS,
new ThreadFactoryBuilder()
.setNameFormat(_serviceName + "-HealthCheckThread-%d")
.setDaemon(true)
.build());
}
ServicePool<S> servicePool = new ServicePool<>(Ticker.systemTicker(), hostDiscovery, _closeHostDiscovery,
_serviceFactory, _cachingPolicy, _partitionFilter, _loadBalanceAlgorithm, _healthCheckExecutor,
shutdownHealthCheckExecutorOnClose, _healthCheckRetryDelay, _metrics);
_closeHostDiscovery = false;
return servicePool;
} catch (Throwable t) {
if (shutdownHealthCheckExecutorOnClose && _healthCheckExecutor != null) {
_healthCheckExecutor.shutdownNow();
_healthCheckExecutor = null;
}
try {
if (_closeHostDiscovery) {
hostDiscovery.close();
}
} catch (IOException e) {
// NOP
} finally {
_closeHostDiscovery = false;