Search in sources :

Example 1 with AsyncSingleResultPublisher

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();
    });
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) URI(java.net.URI) SQLException(java.sql.SQLException) AsyncSingleResultPublisher(io.micronaut.core.async.publisher.AsyncSingleResultPublisher) HealthResult(io.micronaut.management.health.indicator.HealthResult)

Aggregations

AsyncSingleResultPublisher (io.micronaut.core.async.publisher.AsyncSingleResultPublisher)1 HealthResult (io.micronaut.management.health.indicator.HealthResult)1 URI (java.net.URI)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1