Skip to content

Commit 68782fb

Browse files
committed
Changed jestHealthCheck to perform cluster health request
1 parent 7d98189 commit 68782fb

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchJestHealthIndicator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.gson.JsonParser;
2121
import io.searchbox.client.JestClient;
2222
import io.searchbox.client.JestResult;
23+
2324
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
2425
import org.springframework.boot.actuate.health.Health;
2526
import org.springframework.boot.actuate.health.HealthIndicator;
@@ -28,6 +29,7 @@
2829
* {@link HealthIndicator} for Elasticsearch using a {@link JestClient}.
2930
*
3031
* @author Stephane Nicoll
32+
* @author Julian David Devia Serna (amoto)
3133
* @since 2.0.0
3234
*/
3335
public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
@@ -43,13 +45,15 @@ public ElasticsearchJestHealthIndicator(JestClient jestClient) {
4345

4446
@Override
4547
protected void doHealthCheck(Health.Builder builder) throws Exception {
46-
JestResult healthResult = this.jestClient.execute(new io.searchbox.cluster.Health.Builder().build());
48+
JestResult healthResult = this.jestClient
49+
.execute(new io.searchbox.cluster.Health.Builder().build());
4750
JsonElement root = this.jsonParser.parse(healthResult.getJsonString());
4851
JsonElement status = root.getAsJsonObject().get("status");
49-
if (!healthResult.isSucceeded() || healthResult.getResponseCode() != 200
50-
|| status.getAsString().equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
52+
if (!healthResult.isSucceeded() || healthResult.getResponseCode() != 200 || status
53+
.getAsString().equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
5154
builder.outOfService();
52-
} else {
55+
}
56+
else {
5357
builder.up();
5458
}
5559
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchJestHealthIndicatorTests.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.elasticsearch;
1818

19+
import java.io.IOException;
20+
1921
import com.google.gson.Gson;
2022
import com.google.gson.JsonParser;
2123
import io.searchbox.action.Action;
@@ -24,11 +26,10 @@
2426
import io.searchbox.client.config.exception.CouldNotConnectException;
2527
import io.searchbox.core.SearchResult;
2628
import org.junit.Test;
29+
2730
import org.springframework.boot.actuate.health.Health;
2831
import org.springframework.boot.actuate.health.Status;
2932

30-
import java.io.IOException;
31-
3233
import static org.assertj.core.api.Assertions.assertThat;
3334
import static org.mockito.ArgumentMatchers.any;
3435
import static org.mockito.BDDMockito.given;
@@ -91,14 +92,16 @@ public void elasticsearchIsOutOfServiceBySucceeded() throws IOException {
9192
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
9293
}
9394

94-
private static JestResult createJestResult(String status, int responseCode, boolean succeeded) {
95-
String json = String.format("{\"cluster_name\":\"docker-cluster\"," +
96-
"\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1," +
97-
"\"number_of_data_nodes\":1,\"active_primary_shards\":0," +
98-
"\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0," +
99-
"\"unassigned_shards\":0,\"delayed_unassigned_shards\":0," +
100-
"\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0," +
101-
"\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}", status);
95+
private static JestResult createJestResult(String status, int responseCode,
96+
boolean succeeded) {
97+
String json = String.format("{\"cluster_name\":\"docker-cluster\","
98+
+ "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
99+
+ "\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
100+
+ "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
101+
+ "\"unassigned_shards\":0,\"delayed_unassigned_shards\":0,"
102+
+ "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0,"
103+
+ "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}",
104+
status);
102105
SearchResult searchResult = new SearchResult(new Gson());
103106
searchResult.setJsonString(json);
104107
searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject());

0 commit comments

Comments
 (0)