|
18 | 18 | import io.netty.util.ThreadDeathWatcher;
|
19 | 19 | import io.netty.util.concurrent.GlobalEventExecutor;
|
20 | 20 |
|
21 |
| -import java.net.InetAddress; |
22 | 21 | import java.util.ArrayList;
|
23 | 22 | import java.util.Arrays;
|
24 | 23 | import java.util.Collection;
|
|
31 | 30 | import org.elasticsearch.common.SuppressForbidden;
|
32 | 31 | import org.elasticsearch.common.network.NetworkModule;
|
33 | 32 | import org.elasticsearch.common.settings.Settings;
|
34 |
| -import org.elasticsearch.common.transport.InetSocketTransportAddress; |
35 | 33 | import org.elasticsearch.index.reindex.ReindexPlugin;
|
36 | 34 | import org.elasticsearch.join.ParentJoinPlugin;
|
37 | 35 | import org.elasticsearch.percolator.PercolatorPlugin;
|
|
46 | 44 | import org.springframework.beans.factory.InitializingBean;
|
47 | 45 | import org.springframework.util.Assert;
|
48 | 46 | import org.springframework.util.ClassUtils;
|
49 |
| -import org.springframework.util.StringUtils; |
50 | 47 |
|
51 | 48 | /**
|
52 | 49 | * TransportClientFactoryBean
|
|
55 | 52 | * @author Mohsin Husen
|
56 | 53 | * @author Jakub Vavrik
|
57 | 54 | * @author Piotr Betkier
|
| 55 | + * @author Oliver Gierke |
58 | 56 | */
|
59 |
| - |
60 | 57 | public class TransportClientFactoryBean implements FactoryBean<TransportClient>, InitializingBean, DisposableBean {
|
61 | 58 |
|
62 | 59 | private static final Logger logger = LoggerFactory.getLogger(TransportClientFactoryBean.class);
|
63 |
| - private String clusterNodes = "127.0.0.1:9300"; |
| 60 | + private ClusterNodes clusterNodes = ClusterNodes.of("127.0.0.1:9300"); |
64 | 61 | private String clusterName = "elasticsearch";
|
65 | 62 | private Boolean clientTransportSniff = true;
|
66 | 63 | private Boolean clientIgnoreClusterName = Boolean.FALSE;
|
67 | 64 | private String clientPingTimeout = "5s";
|
68 | 65 | private String clientNodesSamplerInterval = "5s";
|
69 | 66 | private TransportClient client;
|
70 | 67 | private Properties properties;
|
71 |
| - static final String COLON = ":"; |
72 |
| - static final String COMMA = ","; |
73 | 68 |
|
74 | 69 | @Override
|
75 | 70 | public void destroy() throws Exception {
|
@@ -106,39 +101,26 @@ public void afterPropertiesSet() throws Exception {
|
106 | 101 | protected void buildClient() throws Exception {
|
107 | 102 |
|
108 | 103 | client = new SpringDataTransportClient(settings());
|
109 |
| - Assert.hasText(clusterNodes, "[Assertion failed] clusterNodes settings missing."); |
110 |
| - String[] clusterNodesArray = StringUtils.split(clusterNodes, COMMA); |
111 |
| - if (clusterNodesArray != null) { |
112 |
| - for (String clusterNode : clusterNodesArray) { |
113 |
| - if (clusterNode != null) { |
114 |
| - int colonPosition = clusterName.lastIndexOf(COLON); |
115 |
| - String hostName = colonPosition != -1 ? clusterNode.substring(0, colonPosition) : clusterNode; |
116 |
| - String port = colonPosition != -1 ? clusterNode.substring(colonPosition, clusterNode.length()) : ""; |
117 |
| - Assert.hasText(hostName, "[Assertion failed] missing host name in 'clusterNodes'"); |
118 |
| - Assert.hasText(port, "[Assertion failed] missing port in 'clusterNodes'"); |
119 |
| - logger.info("adding transport node : " + clusterNode); |
120 |
| - client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port))); |
121 |
| - } |
122 |
| - } |
123 |
| - } |
| 104 | + |
| 105 | + clusterNodes.stream() // |
| 106 | + .peek(it -> logger.info("Adding transport node : " + it.toString())) // |
| 107 | + .forEach(client::addTransportAddress); |
| 108 | + |
124 | 109 | client.connectedNodes();
|
125 | 110 | }
|
126 | 111 |
|
127 | 112 | private Settings settings() {
|
128 | 113 | if (properties != null) {
|
129 | 114 | return Settings.builder().put(properties).build();
|
130 | 115 | }
|
131 |
| - return Settings.builder() |
132 |
| - .put("cluster.name", clusterName) |
133 |
| - .put("client.transport.sniff", clientTransportSniff) |
| 116 | + return Settings.builder().put("cluster.name", clusterName).put("client.transport.sniff", clientTransportSniff) |
134 | 117 | .put("client.transport.ignore_cluster_name", clientIgnoreClusterName)
|
135 | 118 | .put("client.transport.ping_timeout", clientPingTimeout)
|
136 |
| - .put("client.transport.nodes_sampler_interval", clientNodesSamplerInterval) |
137 |
| - .build(); |
| 119 | + .put("client.transport.nodes_sampler_interval", clientNodesSamplerInterval).build(); |
138 | 120 | }
|
139 | 121 |
|
140 | 122 | public void setClusterNodes(String clusterNodes) {
|
141 |
| - this.clusterNodes = clusterNodes; |
| 123 | + this.clusterNodes = ClusterNodes.of(clusterNodes); |
142 | 124 | }
|
143 | 125 |
|
144 | 126 | public void setClusterName(String clusterName) {
|
@@ -176,7 +158,7 @@ public void setClientIgnoreClusterName(Boolean clientIgnoreClusterName) {
|
176 | 158 | public void setProperties(Properties properties) {
|
177 | 159 | this.properties = properties;
|
178 | 160 | }
|
179 |
| - |
| 161 | + |
180 | 162 | /**
|
181 | 163 | * Pretty exact copy of {@link PreBuiltTransportClient} except that we're inspecting the classpath for Netty
|
182 | 164 | * dependencies to only include the ones available. {@link PreBuiltTransportClient} expects both Netty 3 and Netty 4
|
@@ -236,8 +218,9 @@ private static void setSystemPropertyIfUnset(final String key, final String valu
|
236 | 218 | found = true;
|
237 | 219 | } catch (ClassNotFoundException | LinkageError e) {}
|
238 | 220 | }
|
239 |
| - |
240 |
| - Assert.state(found, "Neither Netty 3 or Netty 4 plugin found on the classpath. One of them is required to run the transport client!"); |
| 221 | + |
| 222 | + Assert.state(found, |
| 223 | + "Neither Netty 3 or Netty 4 plugin found on the classpath. One of them is required to run the transport client!"); |
241 | 224 |
|
242 | 225 | plugins.add(ReindexPlugin.class);
|
243 | 226 | plugins.add(PercolatorPlugin.class);
|
|
0 commit comments