Skip to content

Commit 21111b6

Browse files
committed
DATAES-6 , Add ability to let NodeClient clean up working directory
1 parent a8aca3e commit 21111b6

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/main/java/org/springframework/data/elasticsearch/client/NodeClientFactoryBean.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,6 +17,9 @@
1717

1818
import org.elasticsearch.client.Client;
1919
import org.elasticsearch.client.node.NodeClient;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.springframework.beans.factory.DisposableBean;
2023
import org.springframework.beans.factory.FactoryBean;
2124
import org.springframework.beans.factory.InitializingBean;
2225

@@ -29,9 +32,11 @@
2932
* @author Mohsin Husen
3033
*/
3134

32-
public class NodeClientFactoryBean implements FactoryBean<NodeClient>, InitializingBean{
35+
public class NodeClientFactoryBean implements FactoryBean<NodeClient>, InitializingBean, DisposableBean {
3336

37+
private static final Logger logger = LoggerFactory.getLogger(NodeClientFactoryBean.class);
3438
private boolean local;
39+
private boolean data;
3540
private NodeClient nodeClient;
3641

3742
NodeClientFactoryBean() {
@@ -58,10 +63,26 @@ public boolean isSingleton() {
5863

5964
@Override
6065
public void afterPropertiesSet() throws Exception {
61-
nodeClient = (NodeClient) nodeBuilder().local(this.local).node().client();
66+
nodeClient = (NodeClient) nodeBuilder().local(this.local).data(this.data).node().client();
6267
}
6368

6469
public void setLocal(boolean local) {
6570
this.local = local;
6671
}
72+
73+
public void setData(boolean data) {
74+
this.data = data;
75+
}
76+
77+
@Override
78+
public void destroy() throws Exception {
79+
try {
80+
logger.info("Closing elasticSearch client");
81+
if (nodeClient != null) {
82+
nodeClient.close();
83+
}
84+
} catch (final Exception e) {
85+
logger.error("Error closing ElasticSearch client: ", e);
86+
}
87+
}
6788
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.data.elasticsearch.config;
1718

1819
import org.springframework.beans.factory.support.AbstractBeanDefinition;
@@ -40,6 +41,7 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
4041

4142
private void setLocalSettings(Element element, BeanDefinitionBuilder builder) {
4243
builder.addPropertyValue("local", Boolean.valueOf(element.getAttribute("local")));
44+
builder.addPropertyValue("data", Boolean.valueOf(element.getAttribute("data")));
4345
}
4446

4547

src/main/resources/org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<xsd:complexContent>
4646
<xsd:extension base="beans:identifiedType">
4747
<xsd:attribute name="local" type="xsd:boolean" default="false"/>
48+
<xsd:attribute name="data" type="xsd:boolean" default="false"/>
4849
</xsd:extension>
4950
</xsd:complexContent>
5051
</xsd:complexType>

0 commit comments

Comments
 (0)