Johnczek
Johnczek

Reputation: 657

SolrHealthIndicator without deprecated CompositeHealthIndicator

I've tried to upgrade Spring Boot to 2.2.4.RELEASE version. Everzthing if fine exept problem with CompositeHealthIndicator which is deprecated.

I have this bean method

@Autowired
private HealthAggregator healthAggregator;

    @Bean
public HealthIndicator solrHealthIndicator() {
    CompositeHealthIndicator composite = new CompositeHealthIndicator(
            this.healthAggregator);
    composite.addHealthIndicator("solr1", createHealthIndicator(firstHttpSolrClient()));
    composite.addHealthIndicator("solr2", createHealthIndicator(secondHttpSolrClient()));
    composite.addHealthIndicator("querySolr", createHealthIndicator(queryHttpSolrClient()));
    return composite;
}

private CustomSolrHealthIndicator createHealthIndicator(SolrClient source) {

    try {
        return new CustomSolrHealthIndicator(source);
    } catch (Exception ex) {
        throw new IllegalStateException("Unable to create helthCheckIndicator for solr client instance.", ex);
    }
}

That registers HealthIndicator for 3 instances of SOLR (2 indexing, 1 for query). Everything worked fine until Spring Boot update. After update the method CompositeHealthIndicator.addHealthIndicator is not present, the whole class is marked as Deprecated.

The class which is created in createHealthIndicator is like this:

    public class CustomSolrHealthIndicator extends SolrHealthIndicator {

    private final SolrClient solrClient;

    public CustomSolrHealthIndicator(SolrClient solrClient) {
        super(solrClient);
        this.solrClient = solrClient;
    }

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {

        if (!this.solrClient.getClass().isAssignableFrom(HttpSolrClient.class)) {

            super.doHealthCheck(builder);
        }

        HttpSolrClient httpSolrClient = (HttpSolrClient) this.solrClient;
        if (StringUtils.isBlank(httpSolrClient.getBaseURL())) {
            return;
        }

        super.doHealthCheck(builder);
    }
}

Is there any easy way to transform the old way how to register the instances of SOLR i want to check if they are up or down at Spring Boot version 2.2.X?

EDIT: I have tried this:

@Bean
public CompositeHealthContributor solrHealthIndicator() {

    Map<String, HealthIndicator> solrIndicators = Maps.newLinkedHashMap();
    solrIndicators.put("solr1", createHealthIndicator(firstHttpSolrClient()));
    solrIndicators.put("solr2", createHealthIndicator(secondHttpSolrClient()));
    solrIndicators.put("querySolr", createHealthIndicator(queryHttpSolrClient()));

    return CompositeHealthContributor.fromMap(solrIndicators);
}

private CustomSolrHealthIndicator createHealthIndicator(SolrClient source) {

    try {
        return new CustomSolrHealthIndicator(source);
    } catch (Exception ex) {
        throw new IllegalStateException("Unable to create healthCheckIndicator for solr client instance.", ex);
    }
}

The CustomSolrHealthIndicator has no changes against start state.

But I cannot create that bean. When calling createHealthIndicator I am getting NoClassDefFoundError

Does anyone know where the problem is?

Upvotes: 0

Views: 1004

Answers (2)

Tomek Wermiński
Tomek Wermiński

Reputation: 61

Instead of deprecated CompositeHealthIndicator#addHealthIndicator use constructor with map:

@Bean
public HealthIndicator solrHealthIndicator() {

    Map<String, HealthIndicator> healthIndicators = new HashMap<>();
    healthIndicators.put("solr1", createHealthIndicator(firstHttpSolrClient()));
    healthIndicators.put("solr2", createHealthIndicator(secondHttpSolrClient()));
    healthIndicators.put("querySolr", createHealthIndicator(queryHttpSolrClient()));

    return new CompositeHealthIndicator(this.healthAggregator, healthIndicators);
}

Upvotes: 1

Christopher Schneider
Christopher Schneider

Reputation: 3915

Looks like you can just use CompositeHealthContributor. It's not much different from what you have already. It appears something like this would work. You could override the functionality to add them one at a time if you'd like, also, which might be preferable if you have a large amount of configuration. Shouldn't be any harm with either approach.

@Bean
public HealthIndicator solrHealthIndicator() {
    Map<String, HealthIndicator> solrIndicators;
    solrIndicators.put("solr1", createHealthIndicator(firstHttpSolrClient()));
    solrIndicators.put("solr2", createHealthIndicator(secondHttpSolrClient()));
    solrIndicators.put("querySolr", createHealthIndicator(queryHttpSolrClient()));
    return CompositeHealthContributor.fromMap(solrIndicators);
}

Upvotes: 1

Related Questions