Make ElasticSearchAutoConfiguration conditional on SD Elasticsearch

ElasticSearchAutoConfiguration depends on two Spring Data Elasticsearch
classes (TransportClientFactoryBean and NodeClientFactoryBean), however
it’s only conditional on Elasticsearch itself being on the classpath.
This lead to start up failures due to a ClassNotFoundException. Its
@ConditionalOnClass configuration has been updated so that the
auto-configuration will only be enabled if both Elasticsearch and Spring
Data Elasticsearch are on the classpath.

The dependencies on TransportClientFactoryBean and NodeClientFactoryBean
were ‘hidden’ in ElasticsearchProperties. The logic that uses these
types has been moved into ElasticSearchAutoConfiguration so that the
usage of the types and the related @ConditionalOnClass configuration
is in the same file.

Fixes #1023
pull/1031/head
Andy Wilkinson 11 years ago
parent 9b44c322d4
commit f7d1aab9f3

@ -25,6 +25,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; 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.TransportClientFactoryBean;
import org.springframework.util.StringUtils;
/** /**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
@ -32,10 +35,12 @@ import org.springframework.context.annotation.Configuration;
* *
* @author Artur Konczak * @author Artur Konczak
* @author Mohsin Husen * @author Mohsin Husen
* @author Andy Wilkinson
* @since 1.1.0 * @since 1.1.0
*/ */
@Configuration @Configuration
@ConditionalOnClass(Client.class) @ConditionalOnClass({ Client.class, TransportClientFactoryBean.class,
NodeClientFactoryBean.class })
@EnableConfigurationProperties(ElasticsearchProperties.class) @EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchAutoConfiguration implements DisposableBean { public class ElasticsearchAutoConfiguration implements DisposableBean {
@ -48,10 +53,35 @@ public class ElasticsearchAutoConfiguration implements DisposableBean {
@Bean @Bean
public Client elasticsearchClient() { public Client elasticsearchClient() {
this.client = this.properties.createClient(); try {
if (StringUtils.hasLength(this.properties.getClusterNodes())) {
this.client = createTransportClient();
}
else {
this.client = createNodeClient();
}
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
return this.client; return this.client;
} }
private Client createNodeClient() throws Exception {
NodeClientFactoryBean factory = new NodeClientFactoryBean(true);
factory.setClusterName(this.properties.getClusterName());
factory.afterPropertiesSet();
return factory.getObject();
}
private Client createTransportClient() throws Exception {
TransportClientFactoryBean factory = new TransportClientFactoryBean();
factory.setClusterName(this.properties.getClusterName());
factory.setClusterNodes(this.properties.getClusterNodes());
factory.afterPropertiesSet();
return factory.getObject();
}
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
if (this.client != null) { if (this.client != null) {

@ -16,11 +16,7 @@
package org.springframework.boot.autoconfigure.elasticsearch; package org.springframework.boot.autoconfigure.elasticsearch;
import org.elasticsearch.client.Client;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.elasticsearch.client.NodeClientFactoryBean;
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
import org.springframework.util.StringUtils;
/** /**
* Configuration properties for Elasticsearch. * Configuration properties for Elasticsearch.
@ -36,8 +32,6 @@ public class ElasticsearchProperties {
private String clusterNodes; private String clusterNodes;
private boolean local = true;
public String getClusterName() { public String getClusterName() {
return this.clusterName; return this.clusterName;
} }
@ -53,30 +47,4 @@ public class ElasticsearchProperties {
public void setClusterNodes(String clusterNodes) { public void setClusterNodes(String clusterNodes) {
this.clusterNodes = clusterNodes; this.clusterNodes = clusterNodes;
} }
public Client createClient() {
try {
return (StringUtils.hasLength(this.clusterNodes) ? createTransportClient()
: createNodeClient());
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
private Client createNodeClient() throws Exception {
NodeClientFactoryBean factory = new NodeClientFactoryBean(this.local);
factory.setClusterName(this.clusterName);
factory.afterPropertiesSet();
return factory.getObject();
}
private Client createTransportClient() throws Exception {
TransportClientFactoryBean factory = new TransportClientFactoryBean();
factory.setClusterName(this.clusterName);
factory.setClusterNodes(this.clusterNodes);
factory.afterPropertiesSet();
return factory.getObject();
}
} }

Loading…
Cancel
Save