Skip to content

Commit a1cbd93

Browse files
committed
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
1 parent 5c4b698 commit a1cbd93

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfiguration.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,11 @@
1919
import org.apache.commons.logging.Log;
2020
import org.apache.commons.logging.LogFactory;
2121
import org.elasticsearch.client.Client;
22+
import org.elasticsearch.client.transport.TransportClient;
23+
import org.elasticsearch.common.lease.Releasable;
24+
import org.elasticsearch.common.settings.ImmutableSettings;
25+
import org.elasticsearch.node.Node;
26+
import org.elasticsearch.node.NodeBuilder;
2227
import org.springframework.beans.factory.DisposableBean;
2328
import org.springframework.beans.factory.annotation.Autowired;
2429
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -27,6 +32,7 @@
2732
import org.springframework.context.annotation.Configuration;
2833
import org.springframework.data.elasticsearch.client.NodeClientFactoryBean;
2934
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
35+
import org.springframework.util.ReflectionUtils;
3036
import org.springframework.util.StringUtils;
3137

3238
/**
@@ -49,13 +55,12 @@ public class ElasticsearchAutoConfiguration implements DisposableBean {
4955
@Autowired
5056
private ElasticsearchProperties properties;
5157

52-
private Client client;
58+
private Releasable releasable;
5359

5460
@Bean
5561
public Client elasticsearchClient() {
5662
try {
57-
this.client = createClient();
58-
return this.client;
63+
return createClient();
5964
}
6065
catch (Exception ex) {
6166
throw new IllegalStateException(ex);
@@ -70,29 +75,41 @@ private Client createClient() throws Exception {
7075
}
7176

7277
private Client createNodeClient() throws Exception {
73-
NodeClientFactoryBean factory = new NodeClientFactoryBean(true);
74-
factory.setClusterName(this.properties.getClusterName());
75-
factory.afterPropertiesSet();
76-
return factory.getObject();
78+
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put(
79+
"http.enabled", String.valueOf(false));
80+
Node node = new NodeBuilder().settings(settings)
81+
.clusterName(this.properties.getClusterName()).local(true).node();
82+
this.releasable = node;
83+
return node.client();
7784
}
7885

7986
private Client createTransportClient() throws Exception {
8087
TransportClientFactoryBean factory = new TransportClientFactoryBean();
8188
factory.setClusterName(this.properties.getClusterName());
8289
factory.setClusterNodes(this.properties.getClusterNodes());
8390
factory.afterPropertiesSet();
84-
return factory.getObject();
91+
TransportClient client = factory.getObject();
92+
this.releasable = client;
93+
return client;
8594
}
8695

8796
@Override
8897
public void destroy() throws Exception {
89-
if (this.client != null) {
98+
if (this.releasable != null) {
9099
try {
91100
if (logger.isInfoEnabled()) {
92101
logger.info("Closing Elasticsearch client");
93102
}
94-
if (this.client != null) {
95-
this.client.close();
103+
if (this.releasable != null) {
104+
try {
105+
this.releasable.close();
106+
}
107+
catch (NoSuchMethodError ex) {
108+
// Earlier versions of Elasticsearch had a different method name
109+
ReflectionUtils.invokeMethod(
110+
ReflectionUtils.findMethod(Releasable.class, "release"),
111+
this.releasable);
112+
}
96113
}
97114
}
98115
catch (final Exception ex) {

spring-boot-samples/spring-boot-sample-data-elasticsearch/src/main/java/sample/data/elasticsearch/SampleElasticsearchApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,6 +62,6 @@ private void fetchIndividualCustomers() {
6262
}
6363

6464
public static void main(String[] args) throws Exception {
65-
SpringApplication.run(SampleElasticsearchApplication.class, "--debug");
65+
SpringApplication.run(SampleElasticsearchApplication.class, "--debug").close();
6666
}
6767
}

0 commit comments

Comments
 (0)