|
1 |
| -/* |
2 |
| - * Copyright 2013 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 java.net.InetAddress; |
21 |
| -import java.util.Properties; |
22 |
| - |
23 |
| -import org.elasticsearch.client.transport.TransportClient; |
24 |
| -import org.elasticsearch.common.settings.Settings; |
25 |
| -import org.elasticsearch.common.transport.InetSocketTransportAddress; |
26 |
| -import org.elasticsearch.transport.client.PreBuiltTransportClient; |
27 |
| -import org.slf4j.Logger; |
28 |
| -import org.slf4j.LoggerFactory; |
29 |
| -import org.springframework.beans.factory.DisposableBean; |
30 |
| -import org.springframework.beans.factory.FactoryBean; |
31 |
| -import org.springframework.beans.factory.InitializingBean; |
32 |
| -import org.springframework.util.Assert; |
33 |
| - |
34 |
| -/** |
35 |
| - * TransportClientFactoryBean |
36 |
| - * |
37 |
| - * @author Rizwan Idrees |
38 |
| - * @author Mohsin Husen |
39 |
| - * @author Jakub Vavrik |
40 |
| - * @author Piotr Betkier |
41 |
| - */ |
42 |
| - |
43 |
| -public class TransportClientFactoryBean implements FactoryBean<TransportClient>, InitializingBean, DisposableBean { |
44 |
| - |
45 |
| - private static final Logger logger = LoggerFactory.getLogger(TransportClientFactoryBean.class); |
46 |
| - private String clusterNodes = "127.0.0.1:9300"; |
47 |
| - private String clusterName = "elasticsearch"; |
48 |
| - private Boolean clientTransportSniff = true; |
49 |
| - private Boolean clientIgnoreClusterName = Boolean.FALSE; |
50 |
| - private String clientPingTimeout = "5s"; |
51 |
| - private String clientNodesSamplerInterval = "5s"; |
52 |
| - private TransportClient client; |
53 |
| - private Properties properties; |
54 |
| - static final String COLON = ":"; |
55 |
| - static final String COMMA = ","; |
56 |
| - |
57 |
| - @Override |
58 |
| - public void destroy() throws Exception { |
59 |
| - try { |
60 |
| - logger.info("Closing elasticSearch client"); |
61 |
| - if (client != null) { |
62 |
| - client.close(); |
63 |
| - } |
64 |
| - } catch (final Exception e) { |
65 |
| - logger.error("Error closing ElasticSearch client: ", e); |
66 |
| - } |
67 |
| - } |
68 |
| - |
69 |
| - @Override |
70 |
| - public TransportClient getObject() throws Exception { |
71 |
| - return client; |
72 |
| - } |
73 |
| - |
74 |
| - @Override |
75 |
| - public Class<TransportClient> getObjectType() { |
76 |
| - return TransportClient.class; |
77 |
| - } |
78 |
| - |
79 |
| - @Override |
80 |
| - public boolean isSingleton() { |
81 |
| - return false; |
82 |
| - } |
83 |
| - |
84 |
| - @Override |
85 |
| - public void afterPropertiesSet() throws Exception { |
86 |
| - buildClient(); |
87 |
| - } |
88 |
| - |
89 |
| - protected void buildClient() throws Exception { |
90 |
| - |
91 |
| - client = new PreBuiltTransportClient(settings()); |
92 |
| - Assert.hasText(clusterNodes, "[Assertion failed] clusterNodes settings missing."); |
93 |
| - for (String clusterNode : split(clusterNodes, COMMA)) { |
94 |
| - String hostName = substringBeforeLast(clusterNode, COLON); |
95 |
| - String port = substringAfterLast(clusterNode, COLON); |
96 |
| - Assert.hasText(hostName, "[Assertion failed] missing host name in 'clusterNodes'"); |
97 |
| - Assert.hasText(port, "[Assertion failed] missing port in 'clusterNodes'"); |
98 |
| - logger.info("adding transport node : " + clusterNode); |
99 |
| - client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port))); |
100 |
| - } |
101 |
| - client.connectedNodes(); |
102 |
| - } |
103 |
| - |
104 |
| - private Settings settings() { |
105 |
| - if (properties != null) { |
106 |
| - return Settings.builder().put(properties).build(); |
107 |
| - } |
108 |
| - return Settings.builder() |
109 |
| - .put("cluster.name", clusterName) |
110 |
| - .put("client.transport.sniff", clientTransportSniff) |
111 |
| - .put("client.transport.ignore_cluster_name", clientIgnoreClusterName) |
112 |
| - .put("client.transport.ping_timeout", clientPingTimeout) |
113 |
| - .put("client.transport.nodes_sampler_interval", clientNodesSamplerInterval) |
114 |
| - .build(); |
115 |
| - } |
116 |
| - |
117 |
| - public void setClusterNodes(String clusterNodes) { |
118 |
| - this.clusterNodes = clusterNodes; |
119 |
| - } |
120 |
| - |
121 |
| - public void setClusterName(String clusterName) { |
122 |
| - this.clusterName = clusterName; |
123 |
| - } |
124 |
| - |
125 |
| - public void setClientTransportSniff(Boolean clientTransportSniff) { |
126 |
| - this.clientTransportSniff = clientTransportSniff; |
127 |
| - } |
128 |
| - |
129 |
| - public String getClientNodesSamplerInterval() { |
130 |
| - return clientNodesSamplerInterval; |
131 |
| - } |
132 |
| - |
133 |
| - public void setClientNodesSamplerInterval(String clientNodesSamplerInterval) { |
134 |
| - this.clientNodesSamplerInterval = clientNodesSamplerInterval; |
135 |
| - } |
136 |
| - |
137 |
| - public String getClientPingTimeout() { |
138 |
| - return clientPingTimeout; |
139 |
| - } |
140 |
| - |
141 |
| - public void setClientPingTimeout(String clientPingTimeout) { |
142 |
| - this.clientPingTimeout = clientPingTimeout; |
143 |
| - } |
144 |
| - |
145 |
| - public Boolean getClientIgnoreClusterName() { |
146 |
| - return clientIgnoreClusterName; |
147 |
| - } |
148 |
| - |
149 |
| - public void setClientIgnoreClusterName(Boolean clientIgnoreClusterName) { |
150 |
| - this.clientIgnoreClusterName = clientIgnoreClusterName; |
151 |
| - } |
152 |
| - |
153 |
| - public void setProperties(Properties properties) { |
154 |
| - this.properties = properties; |
155 |
| - } |
156 |
| -} |
| 1 | +/* |
| 2 | + * Copyright 2013 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 java.net.InetAddress; |
| 21 | +import java.util.Properties; |
| 22 | + |
| 23 | +import org.elasticsearch.client.transport.TransportClient; |
| 24 | +import org.elasticsearch.common.settings.Settings; |
| 25 | +import org.elasticsearch.common.transport.TransportAddress; |
| 26 | +import org.elasticsearch.transport.client.PreBuiltTransportClient; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | +import org.springframework.beans.factory.DisposableBean; |
| 30 | +import org.springframework.beans.factory.FactoryBean; |
| 31 | +import org.springframework.beans.factory.InitializingBean; |
| 32 | +import org.springframework.util.Assert; |
| 33 | + |
| 34 | +/** |
| 35 | + * TransportClientFactoryBean |
| 36 | + * |
| 37 | + * @author Rizwan Idrees |
| 38 | + * @author Mohsin Husen |
| 39 | + * @author Jakub Vavrik |
| 40 | + * @author Piotr Betkier |
| 41 | + * @author Ilkang Na |
| 42 | + */ |
| 43 | + |
| 44 | +public class TransportClientFactoryBean implements FactoryBean<TransportClient>, InitializingBean, DisposableBean { |
| 45 | + |
| 46 | + private static final Logger logger = LoggerFactory.getLogger(TransportClientFactoryBean.class); |
| 47 | + private String clusterNodes = "127.0.0.1:9300"; |
| 48 | + private String clusterName = "elasticsearch"; |
| 49 | + private Boolean clientTransportSniff = true; |
| 50 | + private Boolean clientIgnoreClusterName = Boolean.FALSE; |
| 51 | + private String clientPingTimeout = "5s"; |
| 52 | + private String clientNodesSamplerInterval = "5s"; |
| 53 | + private TransportClient client; |
| 54 | + private Properties properties; |
| 55 | + static final String COLON = ":"; |
| 56 | + static final String COMMA = ","; |
| 57 | + |
| 58 | + @Override |
| 59 | + public void destroy() throws Exception { |
| 60 | + try { |
| 61 | + logger.info("Closing elasticSearch client"); |
| 62 | + if (client != null) { |
| 63 | + client.close(); |
| 64 | + } |
| 65 | + } catch (final Exception e) { |
| 66 | + logger.error("Error closing ElasticSearch client: ", e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public TransportClient getObject() throws Exception { |
| 72 | + return client; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Class<TransportClient> getObjectType() { |
| 77 | + return TransportClient.class; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public boolean isSingleton() { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void afterPropertiesSet() throws Exception { |
| 87 | + buildClient(); |
| 88 | + } |
| 89 | + |
| 90 | + protected void buildClient() throws Exception { |
| 91 | + |
| 92 | + client = new PreBuiltTransportClient(settings()); |
| 93 | + Assert.hasText(clusterNodes, "[Assertion failed] clusterNodes settings missing."); |
| 94 | + for (String clusterNode : split(clusterNodes, COMMA)) { |
| 95 | + String hostName = substringBeforeLast(clusterNode, COLON); |
| 96 | + String port = substringAfterLast(clusterNode, COLON); |
| 97 | + Assert.hasText(hostName, "[Assertion failed] missing host name in 'clusterNodes'"); |
| 98 | + Assert.hasText(port, "[Assertion failed] missing port in 'clusterNodes'"); |
| 99 | + logger.info("adding transport node : " + clusterNode); |
| 100 | + client.addTransportAddress(new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port))); |
| 101 | + } |
| 102 | + client.connectedNodes(); |
| 103 | + } |
| 104 | + |
| 105 | + private Settings settings() { |
| 106 | + if (properties != null) { |
| 107 | + Settings.Builder builder = Settings.builder(); |
| 108 | + |
| 109 | + properties.forEach((key, value) -> { |
| 110 | + builder.put(key.toString(), value.toString()); |
| 111 | + }); |
| 112 | + |
| 113 | + return builder.build(); |
| 114 | + } |
| 115 | + return Settings.builder() |
| 116 | + .put("cluster.name", clusterName) |
| 117 | + .put("client.transport.sniff", clientTransportSniff) |
| 118 | + .put("client.transport.ignore_cluster_name", clientIgnoreClusterName) |
| 119 | + .put("client.transport.ping_timeout", clientPingTimeout) |
| 120 | + .put("client.transport.nodes_sampler_interval", clientNodesSamplerInterval) |
| 121 | + .build(); |
| 122 | + } |
| 123 | + |
| 124 | + public void setClusterNodes(String clusterNodes) { |
| 125 | + this.clusterNodes = clusterNodes; |
| 126 | + } |
| 127 | + |
| 128 | + public void setClusterName(String clusterName) { |
| 129 | + this.clusterName = clusterName; |
| 130 | + } |
| 131 | + |
| 132 | + public void setClientTransportSniff(Boolean clientTransportSniff) { |
| 133 | + this.clientTransportSniff = clientTransportSniff; |
| 134 | + } |
| 135 | + |
| 136 | + public String getClientNodesSamplerInterval() { |
| 137 | + return clientNodesSamplerInterval; |
| 138 | + } |
| 139 | + |
| 140 | + public void setClientNodesSamplerInterval(String clientNodesSamplerInterval) { |
| 141 | + this.clientNodesSamplerInterval = clientNodesSamplerInterval; |
| 142 | + } |
| 143 | + |
| 144 | + public String getClientPingTimeout() { |
| 145 | + return clientPingTimeout; |
| 146 | + } |
| 147 | + |
| 148 | + public void setClientPingTimeout(String clientPingTimeout) { |
| 149 | + this.clientPingTimeout = clientPingTimeout; |
| 150 | + } |
| 151 | + |
| 152 | + public Boolean getClientIgnoreClusterName() { |
| 153 | + return clientIgnoreClusterName; |
| 154 | + } |
| 155 | + |
| 156 | + public void setClientIgnoreClusterName(Boolean clientIgnoreClusterName) { |
| 157 | + this.clientIgnoreClusterName = clientIgnoreClusterName; |
| 158 | + } |
| 159 | + |
| 160 | + public void setProperties(Properties properties) { |
| 161 | + this.properties = properties; |
| 162 | + } |
| 163 | +} |
0 commit comments