All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.reactivecommons.async.rabbit.health.RabbitReactiveHealthIndicator Maven / Gradle / Ivy

There is a newer version: 5.1.2
Show newest version
package org.reactivecommons.async.rabbit.health;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.reactivecommons.async.starter.config.health.RCHealth;
import org.reactivecommons.async.starter.config.health.RCHealthIndicator;
import reactor.core.publisher.Mono;

import java.net.SocketException;

import static org.reactivecommons.async.starter.config.health.ReactiveCommonsHealthIndicator.DOMAIN;
import static org.reactivecommons.async.starter.config.health.ReactiveCommonsHealthIndicator.VERSION;

@Log4j2
public class RabbitReactiveHealthIndicator extends RCHealthIndicator {
    private final String domain;
    private final ConnectionFactory connectionFactory;

    public RabbitReactiveHealthIndicator(String domain, ConnectionFactory connectionFactory) {
        this.domain = domain;
        this.connectionFactory = connectionFactory.clone();
        this.connectionFactory.useBlockingIo();
    }

    @Override
    public Mono doHealthCheck(RCHealth.RCHealthBuilder builder) {
        builder.withDetail(DOMAIN, domain);
        return Mono.fromCallable(() -> getRawVersion(connectionFactory))
                .map(status -> builder.up().withDetail(VERSION, status).build());
    }

    @SneakyThrows
    private String getRawVersion(ConnectionFactory factory) {
        Connection connection = null;
        try {
            connection = factory.newConnection();
            return connection.getServerProperties().get(VERSION).toString();
        } catch (SocketException e) {
            log.warn("Identified error", e);
            throw new RabbitMQHealthException(e);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                    log.error("Error closing health connection", e);
                }
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy