1 | package org.springframework.data.elasticsearch.client; |
2 | |
3 | import org.elasticsearch.client.transport.TransportClient; |
4 | import org.elasticsearch.common.settings.Settings; |
5 | import org.elasticsearch.common.transport.InetSocketTransportAddress; |
6 | import org.slf4j.Logger; |
7 | import org.slf4j.LoggerFactory; |
8 | import org.springframework.beans.factory.DisposableBean; |
9 | import org.springframework.beans.factory.FactoryBean; |
10 | import org.springframework.beans.factory.InitializingBean; |
11 | import org.springframework.util.Assert; |
12 | |
13 | import java.util.Properties; |
14 | |
15 | import static org.apache.commons.lang.StringUtils.substringAfter; |
16 | import static org.apache.commons.lang.StringUtils.substringBefore; |
17 | import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; |
18 | |
19 | |
20 | public class TransportClientFactoryBean implements FactoryBean<TransportClient>, InitializingBean, DisposableBean { |
21 | |
22 | private static final Logger logger = LoggerFactory.getLogger(TransportClientFactoryBean.class); |
23 | private String[] clusterNodes; |
24 | private TransportClient client; |
25 | private Properties properties; |
26 | static final String COLON = ":"; |
27 | |
28 | @Override |
29 | public void destroy() throws Exception { |
30 | try { |
31 | logger.info("Closing elasticSearch client"); |
32 | if (client != null) { |
33 | client.close(); |
34 | } |
35 | } catch (final Exception e) { |
36 | logger.error("Error closing ElasticSearch client: ", e); |
37 | } |
38 | } |
39 | |
40 | @Override |
41 | public TransportClient getObject() throws Exception { |
42 | return client; |
43 | } |
44 | |
45 | @Override |
46 | public Class<TransportClient> getObjectType() { |
47 | return TransportClient.class; |
48 | } |
49 | |
50 | @Override |
51 | public boolean isSingleton() { |
52 | return false; |
53 | } |
54 | |
55 | @Override |
56 | public void afterPropertiesSet() throws Exception { |
57 | buildClient(); |
58 | } |
59 | |
60 | protected void buildClient() throws Exception { |
61 | client = new TransportClient(settings()); |
62 | Assert.notEmpty(clusterNodes,"[Assertion failed] clusterNodes settings missing."); |
63 | for (String clusterNode : clusterNodes) { |
64 | String hostName = substringBefore(clusterNode, COLON); |
65 | String port = substringAfter(clusterNode, COLON); |
66 | Assert.hasText(hostName,"[Assertion failed] missing host name in 'clusterNodes'"); |
67 | Assert.hasText(port,"[Assertion failed] missing port in 'clusterNodes'"); |
68 | logger.info("adding transport node : " + clusterNode); |
69 | client.addTransportAddress(new InetSocketTransportAddress(hostName, Integer.valueOf(port))); |
70 | } |
71 | client.connectedNodes(); |
72 | } |
73 | |
74 | private Settings settings(){ |
75 | if(properties != null){ |
76 | return settingsBuilder().put(properties).build(); |
77 | } |
78 | return settingsBuilder() |
79 | .put("client.transport.sniff",true).build(); |
80 | } |
81 | |
82 | public void setClusterNodes(String[] clusterNodes) { |
83 | this.clusterNodes = clusterNodes; |
84 | } |
85 | |
86 | public void setProperties(Properties properties) { |
87 | this.properties = properties; |
88 | } |
89 | } |