diff --git a/pom.xml b/pom.xml
index cadbd81..724943b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -164,6 +164,12 @@
2.0.1.Final
+
+
+ org.springframework.boot
+ spring-boot-starter-actuator
+
+
eu.dnetlib.dhp
diff --git a/src/main/java/eu/openaire/api/config/health/HealthCheck.java b/src/main/java/eu/openaire/api/config/health/HealthCheck.java
new file mode 100644
index 0000000..e790c7e
--- /dev/null
+++ b/src/main/java/eu/openaire/api/config/health/HealthCheck.java
@@ -0,0 +1,45 @@
+package eu.openaire.api.config;
+
+import eu.openaire.api.repositories.SolrRepository;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.actuate.health.Health;
+import org.springframework.boot.actuate.health.HealthIndicator;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+@Component
+public class HealthCheck implements HealthIndicator {
+
+ @Autowired
+ private SolrRepository solrRepository;
+
+ @Override
+ public Health health() {
+
+ System.out.println("HEALTH CHECK");
+
+ // Custom health check logic
+ boolean isHealthy = checkHealth();
+ if (isHealthy) {
+ return Health.up().withDetail("message", "Successfully pinged Solr instances").build();
+ } else {
+ return Health.down().withDetail("message", "Could not ping Solr instances; please check the logs for details").build();
+ }
+ }
+
+ private boolean checkHealth() {
+
+ // Ping Solr cluster to check health
+ try {
+ if (solrRepository.ping().getStatus() != 0) {
+ return false;
+ }
+ } catch (SolrServerException | IOException e) {
+ return false;
+ }
+
+ return true;
+ }
+}
diff --git a/src/main/java/eu/openaire/api/config/health/SolrStartupHealthCheck.java b/src/main/java/eu/openaire/api/config/health/SolrStartupHealthCheck.java
new file mode 100644
index 0000000..6f5c7d5
--- /dev/null
+++ b/src/main/java/eu/openaire/api/config/health/SolrStartupHealthCheck.java
@@ -0,0 +1,2 @@
+package eu.openaire.api.config.health;public class SolrStartupHealthCheck {
+}
diff --git a/src/main/java/eu/openaire/api/repositories/SolrRepository.java b/src/main/java/eu/openaire/api/repositories/SolrRepository.java
index df1bc31..10ad7a2 100644
--- a/src/main/java/eu/openaire/api/repositories/SolrRepository.java
+++ b/src/main/java/eu/openaire/api/repositories/SolrRepository.java
@@ -10,6 +10,7 @@ import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrException;
import org.springframework.stereotype.Repository;
import java.io.IOException;
@@ -62,4 +63,8 @@ public class SolrRepository {
return solrConnectionManager.getSolrClient().query(query);
}
+
+ public void ping() throws SolrServerException, SolrException, IOException {
+ solrConnectionManager.ping();
+ }
}
diff --git a/src/main/java/eu/openaire/api/solr/SolrConnectionManager.java b/src/main/java/eu/openaire/api/solr/SolrConnectionManager.java
index d2db084..92efa3d 100644
--- a/src/main/java/eu/openaire/api/solr/SolrConnectionManager.java
+++ b/src/main/java/eu/openaire/api/solr/SolrConnectionManager.java
@@ -8,6 +8,7 @@ import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
+import org.apache.solr.common.SolrException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@@ -46,10 +47,15 @@ public class SolrConnectionManager {
// set the appropriate Solr collection
((CloudSolrClient) solrClient).setDefaultCollection(solrProperties.getCollection());
- log.info("Creating Solr client - Ping response: " + solrClient.ping().toString());
+ // uncomment ping below, to crash app on startup if Solr is not available
+// this.ping();
}
+ public void ping() throws IOException, SolrException, SolrServerException {
+ log.info("Solr ping response: " + solrClient.ping().toString());
+ }
+
@PreDestroy
private void closeClient() throws IOException {
@@ -58,6 +64,5 @@ public class SolrConnectionManager {
if (solrClient != null) {
solrClient.close();
}
-
}
}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index f898942..228d23e 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -12,3 +12,13 @@ logging.level.org.springframework.web=DEBUG
#removes 'trace' field from error responses; alternatively remove 'spring-boot-devtools' dependency
server.error.include-stacktrace=never
+# Enable the health endpoint
+management.endpoint.health.enabled=true
+
+# Expose the health endpoint
+management.endpoints.web.exposure.include=health,info
+
+# Customize health endpoint settings
+management.endpoint.health.show-details=always
+management.endpoint.health.show-components=always
+