EsClientFactory.java
1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.cjs.cms.util.elasticsearch;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class EsClientFactory extends BasePooledObjectFactory<TransportClient> {
private Settings settings;
private TransportAddress[] transportAddress;
public EsClientFactory(Settings settings, TransportAddress... transportAddress) {
this.settings = settings;
this.transportAddress = transportAddress;
}
@SuppressWarnings("unchecked")
@Override
public TransportClient create() throws Exception {
Settings.Builder builder = Settings.builder();
//builder.put("cluster.name", "myClusterName");
//builder.put("client.transport.sniff", true);
//ping节点的超时时间,默认5s
//builder.put("client.transport.ping_timeout", 5);
//How often to sample / ping the nodes listed and connected. Defaults to 5s.
//builder.put("client.transport.nodes_sampler_interval", 5);
TransportClient client = new PreBuiltTransportClient(builder.build());
try {
client.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("192.168.10.186"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
return client;
}
@Override
public PooledObject<TransportClient> wrap(TransportClient obj) {
return new DefaultPooledObject<TransportClient>(obj);
}
}