private Optional<JsonNode> clusterHealth(Collection<? extends String> indices) { final Health request = new Health.Builder() .addIndex(indices) .timeout(Ints.saturatedCast(requestTimeout.toSeconds())) .build(); try { final JestResult jestResult = JestUtils.execute(jestClient, request, () -> "Couldn't read cluster health for indices " + indices); return Optional.of(jestResult.getJsonObject()); } catch(ElasticsearchException e) { if (LOG.isDebugEnabled()) { LOG.error("{} ({})", e.getMessage(), Optional.ofNullable(e.getCause()).map(Throwable::getMessage).orElse("n/a"), e); } else { LOG.error("{} ({})", e.getMessage(), Optional.ofNullable(e.getCause()).map(Throwable::getMessage).orElse("n/a")); } return Optional.empty(); } }
/** * Check if Elasticsearch is available and that there are data nodes in the cluster. * * @return {@code true} if the Elasticsearch client is up and the cluster contains data nodes, {@code false} otherwise */ public boolean isConnected() { final Health request = new Health.Builder() .local() .timeout(Ints.saturatedCast(requestTimeout.toSeconds())) .build(); try { final JestResult result = JestUtils.execute(jestClient, request, () -> "Couldn't check connection status of Elasticsearch"); final int numberOfDataNodes = result.getJsonObject().path("number_of_data_nodes").asInt(); return numberOfDataNodes > 0; } catch (ElasticsearchException e) { if (LOG.isDebugEnabled()) { LOG.error(e.getMessage(), e); } return false; } }
private Optional<JsonNode> clusterHealth(Collection<? extends String> indices) { final Health request = new Health.Builder() .addIndex(indices) .timeout(Ints.saturatedCast(requestTimeout.toSeconds())) .build(); try { final JestResult jestResult = JestUtils.execute(jestClient, request, () -> "Couldn't read cluster health for indices " + indices); return Optional.of(jestResult.getJsonObject()); } catch(ElasticsearchException e) { if (LOG.isDebugEnabled()) { LOG.error("{} ({})", e.getMessage(), Optional.ofNullable(e.getCause()).map(Throwable::getMessage).orElse("n/a"), e); } else { LOG.error("{} ({})", e.getMessage(), Optional.ofNullable(e.getCause()).map(Throwable::getMessage).orElse("n/a")); } return Optional.empty(); } }
/** * Check if Elasticsearch is available and that there are data nodes in the cluster. * * @return {@code true} if the Elasticsearch client is up and the cluster contains data nodes, {@code false} otherwise */ public boolean isConnected() { final Health request = new Health.Builder() .local() .timeout(Ints.saturatedCast(requestTimeout.toSeconds())) .build(); try { final JestResult result = JestUtils.execute(jestClient, request, () -> "Couldn't check connection status of Elasticsearch"); final int numberOfDataNodes = result.getJsonObject().path("number_of_data_nodes").asInt(); return numberOfDataNodes > 0; } catch (ElasticsearchException e) { if (LOG.isDebugEnabled()) { LOG.error(e.getMessage(), e); } return false; } }