public AsyncFuture<Void> configure() { final IndicesAdminClient indices = client.admin().indices(); log.info("[{}] updating template for {}", templateName, index.template()); final PutIndexTemplateRequestBuilder put = indices.preparePutTemplate(templateName); put.setSettings(settings); put.setTemplate(index.template()); for (final Map.Entry<String, Map<String, Object>> mapping : mappings.entrySet()) { put.addMapping(mapping.getKey(), mapping.getValue()); } final ResolvableFuture<Void> future = async.future(); final ListenableActionFuture<PutIndexTemplateResponse> target = put.execute(); target.addListener( new ActionListener<PutIndexTemplateResponse>() { @Override public void onResponse(final PutIndexTemplateResponse response) { if (!response.isAcknowledged()) { future.fail(new Exception("request not acknowledged")); return; } future.resolve(null); } @Override public void onFailure(Throwable e) { future.fail(e); } }); future.onCancelled(() -> target.cancel(false)); return future; }
/** Helper method to convert a {@link ListenableFuture} to an {@link AsyncFuture}. */ public static <T> AsyncFuture<T> bind(AsyncFramework async, ListenableFuture<T> source) { final ResolvableFuture<T> target = async.future(); Futures.addCallback( source, new FutureCallback<T>() { @Override public void onSuccess(T result) { target.resolve(result); } @Override public void onFailure(Throwable t) { target.fail(t); } }); target.onCancelled( () -> { source.cancel(false); }); return target; }