Spring Boot provides the following HealthIndicator implementations out-of-the-box. They are auto-configured by default.

  • CassandraHealthIndicator
  • DiskSpaceHealthIndicator
  • DataSourceHealthIndicator
  • ElasticsearchHealthIndicator
  • JmsHealthIndicator
  • MailHealthIndicator
  • MongoHealthIndicator
  • RabbitHealthIndicator
  • RedisHealthIndicator
  • SolrHealthIndicator
package com.example.samplewebapp;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonParserFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.Map;

@Component
public class RestHealthIndicator implements HealthIndicator {
	@Override
	public Health health() {
		RestTemplate restTemplate = new RestTemplate();
		String url = "https://jsonplaceholder.typicode.com/todos/1";
		try {
			String resp = restTemplate.getForObject(url, String.class);
			System.out.println("-------------->" + resp);
			JsonParser springParser = JsonParserFactory.getJsonParser();
			Map<String, Object> map = springParser.parseMap(resp);

			if ("delectus aut autem".equalsIgnoreCase((String) map.get("title"))) {
				return Health.up().
						build();
			} else {
				return Health.down().withDetail("ping_url", url).withDetail("ping_time", new Date()).build();
			}
		} catch (RestClientException e) {
			return Health.down(e).withDetail("ping_url", url).withDetail("ping_time", new Date()).build();
		}
	}
}

If we try to reach the Internet URL https://jsonplaceholder.typicode.com/todos/1 the following code JSON is returned:

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

We have implemented the same logic in our Health indicator so that if you try to reach the "/actuator/health" URL, the health() callback is performed. This is a simple way to check if you are connected to Internet.

$ curl http://localhost:8080/actuator/health
{"status":"UP"}
FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials