Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.4 kB
1
Indexable
Never
package br.com.santander.yzcaml.clientintegration.healthcheck;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Setter
@Component
@Configuration
public class HealthCheckService implements HealthIndicator {

  @Value("${backend.healthchecks}")
  private String urlToHealthCheck;

  @Override
  public Health health() {
    try {
      RestTemplate dlbRestTemplate = new RestTemplate();
      ResponseEntity<String> result = dlbRestTemplate.getForEntity(this.urlToHealthCheck, String.class);
      if (result.getStatusCodeValue() != HttpStatus.SC_OK) {
        HealthCheckService.log.error("HealthCheck failed for URL: {}", this.urlToHealthCheck);
        return Health.down().build();
      }
      HealthCheckService.log.info("HealthCheck is running for URL: {}", this.urlToHealthCheck);

    } catch (Exception ex) {
      HealthCheckService.log.error("HealthCheck Error: " + ex);
      return Health.down(ex).build();
    }

    return Health.up().build();
  }
}
Leave a Comment