mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
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
This commit is contained in:
parent
5c4b698f86
commit
a1cbd93d6b
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* 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;
|
|||||||
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;
|
|||||||
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 {
|
|||||||
@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 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
|||||||
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) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* 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 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user