Commit a1cbd93d authored by Phillip Webb's avatar Phillip Webb

Ensure local Elasticsearch nodes are closed

Update ElasticsearchAutoConfiguration to ensure that local nodes are
closed when the context is closed. Prior to this commit the close()
method of the Client would be called which had no effect for local
Nodes.

Fixes gh-2480
parent 5c4b698f
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,6 +19,11 @@ package org.springframework.boot.autoconfigure.elasticsearch; ...@@ -19,6 +19,11 @@ package org.springframework.boot.autoconfigure.elasticsearch;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.lease.Releasable;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
...@@ -27,6 +32,7 @@ import org.springframework.context.annotation.Bean; ...@@ -27,6 +32,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.NodeClientFactoryBean; import org.springframework.data.elasticsearch.client.NodeClientFactoryBean;
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean; import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
...@@ -49,13 +55,12 @@ public class ElasticsearchAutoConfiguration implements DisposableBean { ...@@ -49,13 +55,12 @@ public class ElasticsearchAutoConfiguration implements DisposableBean {
@Autowired @Autowired
private ElasticsearchProperties properties; private ElasticsearchProperties properties;
private Client client; private Releasable releasable;
@Bean @Bean
public Client elasticsearchClient() { public Client elasticsearchClient() {
try { try {
this.client = createClient(); return createClient();
return this.client;
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
...@@ -70,10 +75,12 @@ public class ElasticsearchAutoConfiguration implements DisposableBean { ...@@ -70,10 +75,12 @@ public class ElasticsearchAutoConfiguration implements DisposableBean {
} }
private Client createNodeClient() throws Exception { private Client createNodeClient() throws Exception {
NodeClientFactoryBean factory = new NodeClientFactoryBean(true); ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put(
factory.setClusterName(this.properties.getClusterName()); "http.enabled", String.valueOf(false));
factory.afterPropertiesSet(); Node node = new NodeBuilder().settings(settings)
return factory.getObject(); .clusterName(this.properties.getClusterName()).local(true).node();
this.releasable = node;
return node.client();
} }
private Client createTransportClient() throws Exception { private Client createTransportClient() throws Exception {
...@@ -81,18 +88,28 @@ public class ElasticsearchAutoConfiguration implements DisposableBean { ...@@ -81,18 +88,28 @@ public class ElasticsearchAutoConfiguration implements DisposableBean {
factory.setClusterName(this.properties.getClusterName()); factory.setClusterName(this.properties.getClusterName());
factory.setClusterNodes(this.properties.getClusterNodes()); factory.setClusterNodes(this.properties.getClusterNodes());
factory.afterPropertiesSet(); factory.afterPropertiesSet();
return factory.getObject(); TransportClient client = factory.getObject();
this.releasable = client;
return client;
} }
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
if (this.client != null) { if (this.releasable != null) {
try { try {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("Closing Elasticsearch client"); logger.info("Closing Elasticsearch client");
} }
if (this.client != null) { if (this.releasable != null) {
this.client.close(); try {
this.releasable.close();
}
catch (NoSuchMethodError ex) {
// Earlier versions of Elasticsearch had a different method name
ReflectionUtils.invokeMethod(
ReflectionUtils.findMethod(Releasable.class, "release"),
this.releasable);
}
} }
} }
catch (final Exception ex) { catch (final Exception ex) {
......
/* /*
* Copyright 2012-2013 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -62,6 +62,6 @@ public class SampleElasticsearchApplication implements CommandLineRunner { ...@@ -62,6 +62,6 @@ public class SampleElasticsearchApplication implements CommandLineRunner {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
SpringApplication.run(SampleElasticsearchApplication.class, "--debug"); SpringApplication.run(SampleElasticsearchApplication.class, "--debug").close();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment