/** * Pings the Consul Agent. */ public void ping() { try { retrofit2.Response<Void> response = api.ping().execute(); if (!response.isSuccessful()) { throw new ConsulException(String.format("Error pinging Consul: %s", response.message())); } } catch (Exception ex) { throw new ConsulException("Error connecting to Consul", ex); } }
strategy.markRequestFailed(nextRequest.get()); throw new ConsulException("Unable to successfully determine a viable host for communication."); throw new ConsulException("Consul failover strategy has determined that there are no viable hosts remaining.");
/** * Performs a Consul transaction. * * PUT /v1/tx * * @deprecated Replaced by {@link #performTransaction(TransactionOptions, Operation...)} * * @param consistency The consistency to use for the transaction. * @param operations A list of KV operations. * @return A {@link ConsulResponse} containing results and potential errors. */ @Deprecated public ConsulResponse<TxResponse> performTransaction(ConsistencyMode consistency, Operation... operations) { Map<String, Object> query = consistency == ConsistencyMode.DEFAULT ? ImmutableMap.of() : ImmutableMap.of(consistency.toParam().get(), "true"); try { return http.extractConsulResponse(api.performTransaction(RequestBody.create(MediaType.parse("application/json"), Jackson.MAPPER.writeValueAsString(kv(operations))), query)); } catch (JsonProcessingException e) { throw new ConsulException(e); } }
/** * Performs a Consul transaction. * * PUT /v1/tx * * @param transactionOptions transaction options (e.g. dc, consistency). * @param operations A list of KV operations. * @return A {@link ConsulResponse} containing results and potential errors. */ public ConsulResponse<TxResponse> performTransaction(TransactionOptions transactionOptions, Operation... operations) { Map<String, Object> query = transactionOptions.toQuery(); try { return http.extractConsulResponse(api.performTransaction(RequestBody.create(MediaType.parse("application/json"), Jackson.MAPPER.writeValueAsString(kv(operations))), query)); } catch (JsonProcessingException e) { throw new ConsulException(e); } }
private <T> Response<T> executeCall(Call<T> call) { try { return call.execute(); } catch (IOException e) { eventHandler.httpRequestFailure(call.request(), e); throw new ConsulException(e); } }
onFailure(new ConsulException("Consul cluster has no elected leader"));
private <T> void ensureResponseSuccessful(Call<T> call, Response<T> response, Integer... okCodes) { if(isSuccessful(response, okCodes)) { eventHandler.httpRequestSuccess(call.request()); } else { ConsulException exception = new ConsulException(response.code(), response); eventHandler.httpRequestInvalid(call.request(), exception); throw exception; } }