use of io.micronaut.core.async.publisher.AsyncSingleResultPublisher in project micronaut-core by micronaut-projects.
the class JdbcIndicator method getResult.
private Publisher<HealthResult> getResult(DataSource dataSource) {
if (executorService == null) {
throw new IllegalStateException("I/O ExecutorService is null");
}
return new AsyncSingleResultPublisher<>(executorService, () -> {
Optional<Throwable> throwable = Optional.empty();
Map<String, Object> details = null;
String key;
try (Connection connection = dataSource.getConnection()) {
if (connection.isValid(CONNECTION_TIMEOUT)) {
DatabaseMetaData metaData = connection.getMetaData();
key = metaData.getURL();
details = new LinkedHashMap<>(1);
details.put("database", metaData.getDatabaseProductName());
details.put("version", metaData.getDatabaseProductVersion());
} else {
throw new SQLException("Connection was not valid");
}
} catch (SQLException e) {
throwable = Optional.of(e);
try {
String url = dataSource.getClass().getMethod("getUrl").invoke(dataSource).toString();
if (url.startsWith("jdbc:")) {
url = url.substring(5);
}
url = url.replaceFirst(";", "?");
url = url.replaceAll(";", "&");
URI uri = new URI(url);
key = uri.getHost() + ":" + uri.getPort() + uri.getPath();
} catch (Exception n) {
key = dataSource.getClass().getName() + "@" + Integer.toHexString(dataSource.hashCode());
}
}
HealthResult.Builder builder = HealthResult.builder(key);
if (throwable.isPresent()) {
builder.exception(throwable.get());
builder.status(HealthStatus.DOWN);
} else {
builder.status(HealthStatus.UP);
builder.details(details);
}
return builder.build();
});
}
Aggregations