Skip to content

Commit 2f0b9b7

Browse files
donrwakonczak
authored andcommitted
DATAES-407 - Support for HighLevelRestClient via ElasticsearchRestTemplate
Original pull request: spring-projects#216
1 parent 9c2f876 commit 2f0b9b7

23 files changed

+6028
-1895
lines changed

pom.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<properties>
2020
<commonscollections>3.2.1</commonscollections>
2121
<commonslang>2.6</commonslang>
22-
<elasticsearch>6.2.2</elasticsearch>
22+
<elasticsearch>6.3.0</elasticsearch>
2323
<log4j>2.9.1</log4j>
2424
<springdata.commons>2.2.0.BUILD-SNAPSHOT</springdata.commons>
2525
<java-module-name>spring.data.elasticsearch</java-module-name>
@@ -79,6 +79,18 @@
7979
</exclusions>
8080
</dependency>
8181

82+
<dependency>
83+
<groupId>org.elasticsearch.client</groupId>
84+
<artifactId>elasticsearch-rest-high-level-client</artifactId>
85+
<version>${elasticsearch}</version>
86+
<exclusions>
87+
<exclusion>
88+
<groupId>commons-logging</groupId>
89+
<artifactId>commons-logging</artifactId>
90+
</exclusion>
91+
</exclusions>
92+
</dependency>
93+
8294
<dependency>
8395
<groupId>org.slf4j</groupId>
8496
<artifactId>log4j-over-slf4j</artifactId>
@@ -169,7 +181,7 @@
169181
<groupId>org.projectlombok</groupId>
170182
<artifactId>lombok</artifactId>
171183
<version>${lombok}</version>
172-
<scope>test</scope>
184+
<scope>provided</scope>
173185
</dependency>
174186

175187
</dependencies>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.client;
17+
18+
import static org.apache.commons.lang.StringUtils.*;
19+
20+
import lombok.extern.slf4j.Slf4j;
21+
22+
import java.net.URL;
23+
import java.util.ArrayList;
24+
25+
import org.apache.http.HttpHost;
26+
import org.elasticsearch.client.RestClient;
27+
import org.elasticsearch.client.RestHighLevelClient;
28+
import org.springframework.beans.factory.DisposableBean;
29+
import org.springframework.beans.factory.FactoryBean;
30+
import org.springframework.beans.factory.InitializingBean;
31+
import org.springframework.util.Assert;
32+
33+
/**
34+
* RestClientFactoryBean
35+
*
36+
* @author Don Wellington
37+
*/
38+
@Slf4j
39+
public class RestClientFactoryBean implements FactoryBean<RestHighLevelClient>, InitializingBean, DisposableBean {
40+
41+
private RestHighLevelClient client;
42+
private String hosts = "http://localhost:9200";
43+
static final String COMMA = ",";
44+
45+
@Override
46+
public void destroy() throws Exception {
47+
try {
48+
log.info("Closing elasticSearch client");
49+
if (client != null) {
50+
client.close();
51+
}
52+
} catch (final Exception e) {
53+
log.error("Error closing ElasticSearch client: ", e);
54+
}
55+
}
56+
57+
@Override
58+
public void afterPropertiesSet() throws Exception {
59+
buildClient();
60+
}
61+
62+
@Override
63+
public RestHighLevelClient getObject() throws Exception {
64+
return client;
65+
}
66+
67+
@Override
68+
public Class<?> getObjectType() {
69+
return RestHighLevelClient.class;
70+
}
71+
72+
@Override
73+
public boolean isSingleton() {
74+
return false;
75+
}
76+
77+
protected void buildClient() throws Exception {
78+
79+
Assert.hasText(hosts, "[Assertion Failed] At least one host must be set.");
80+
ArrayList<HttpHost> httpHosts = new ArrayList<HttpHost>();
81+
for (String host : split(hosts, COMMA)) {
82+
URL hostUrl = new URL(host);
83+
httpHosts.add(new HttpHost(hostUrl.getHost(), hostUrl.getPort(), hostUrl.getProtocol()));
84+
}
85+
client = new RestHighLevelClient(RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()])));
86+
}
87+
88+
public void setHosts(String hosts) {
89+
this.hosts = hosts;
90+
}
91+
92+
public String getHosts() {
93+
return this.hosts;
94+
}
95+
}

src/main/java/org/springframework/data/elasticsearch/config/ElasticsearchNamespaceHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2018 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.
@@ -25,8 +25,8 @@
2525
*
2626
* @author Rizwan Idrees
2727
* @author Mohsin Husen
28+
* @author Don Wellington
2829
*/
29-
3030
public class ElasticsearchNamespaceHandler extends NamespaceHandlerSupport {
3131

3232
@Override
@@ -37,5 +37,6 @@ public void init() {
3737
registerBeanDefinitionParser("repositories", parser);
3838
registerBeanDefinitionParser("node-client", new NodeClientBeanDefinitionParser());
3939
registerBeanDefinitionParser("transport-client", new TransportClientBeanDefinitionParser());
40+
registerBeanDefinitionParser("rest-client", new RestClientBeanDefinitionParser());
4041
}
4142
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.config;
17+
18+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
19+
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
20+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
21+
import org.springframework.beans.factory.xml.ParserContext;
22+
import org.springframework.data.elasticsearch.client.RestClientFactoryBean;
23+
import org.w3c.dom.Element;
24+
25+
/**
26+
* @author Don Wellington
27+
*/
28+
public class RestClientBeanDefinitionParser extends AbstractBeanDefinitionParser {
29+
30+
@Override
31+
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
32+
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(RestClientFactoryBean.class);
33+
setConfigurations(element, builder);
34+
return getSourcedBeanDefinition(builder, element, parserContext);
35+
}
36+
37+
private void setConfigurations(Element element, BeanDefinitionBuilder builder) {
38+
builder.addPropertyValue("hosts", element.getAttribute("hosts"));
39+
}
40+
41+
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,
42+
ParserContext context) {
43+
AbstractBeanDefinition definition = builder.getBeanDefinition();
44+
definition.setSource(context.extractSource(source));
45+
return definition;
46+
}
47+
}

0 commit comments

Comments
 (0)