10
10
import org .elasticsearch .action .admin .cluster .health .ClusterHealthResponse ;
11
11
import org .elasticsearch .client .Client ;
12
12
import org .elasticsearch .client .transport .TransportClient ;
13
- import org .elasticsearch .common .logging .ESLogger ;
14
- import org .elasticsearch .common .logging .Loggers ;
15
13
import org .elasticsearch .common .settings .Settings ;
16
14
import org .elasticsearch .common .transport .InetSocketTransportAddress ;
17
15
import org .elasticsearch .common .unit .TimeValue ;
18
- import org .elasticsearch .node .Node ;
19
- import org .elasticsearch .node .NodeBuilder ;
16
+ import org .elasticsearch .transport .client .PreBuiltTransportClient ;
20
17
import org .elasticsearch .util .AddressParserUtil ;
21
18
import org .springframework .beans .factory .annotation .Value ;
22
19
import org .springframework .stereotype .Component ;
23
20
24
21
import com .floragunn .searchguard .ssl .SearchGuardSSLPlugin ;
25
22
import com .floragunn .searchguard .ssl .util .SSLConfigConstants ;
26
23
24
+ import lombok .extern .slf4j .Slf4j ;
25
+
27
26
/**
28
27
* Prepare the node to work with elastic search.
29
28
*
30
29
* @author luc boutier
31
30
*/
32
31
@ Component
32
+ @ Slf4j
33
33
public class ElasticSearchClient {
34
34
35
- private static final ESLogger LOGGER = Loggers .getLogger (MappingBuilder .class );
36
-
37
- private Node node ;
35
+ // private Node node;
38
36
private boolean isClient ;
39
37
private boolean isTransportClient ;
40
38
private List <InetSocketTransportAddress > adresses ;
@@ -52,7 +50,7 @@ public class ElasticSearchClient {
52
50
public void initialize () {
53
51
if (this .isClient && this .isTransportClient ) {
54
52
// when these both option are set, we use a transport client
55
- Settings .Builder settingsBuilder = Settings .settingsBuilder ()
53
+ Settings .Builder settingsBuilder = Settings .builder ()
56
54
.put ("cluster.name" , this .clusterName );
57
55
if (transportSSL ) {
58
56
settingsBuilder = settingsBuilder
@@ -62,36 +60,41 @@ public void initialize() {
62
60
.put (SSLConfigConstants .SEARCHGUARD_SSL_TRANSPORT_TRUSTSTORE_PASSWORD , this .truststorePassword );
63
61
}
64
62
Settings settings = settingsBuilder .build ();
65
- TransportClient .Builder transportClientBuilder = TransportClient .builder ().settings (settings );
66
- if (transportSSL ) {
67
- transportClientBuilder = transportClientBuilder .addPlugin (SearchGuardSSLPlugin .class );
63
+ TransportClient transportClient ;
64
+ if (!transportSSL ) {
65
+ transportClient = new PreBuiltTransportClient (settings );
66
+ } else {
67
+ transportClient = new PreBuiltTransportClient (settings , SearchGuardSSLPlugin .class );
68
68
}
69
- TransportClient transportClient = transportClientBuilder .build ();
70
69
for (InetSocketTransportAddress add : adresses ) {
71
70
transportClient .addTransportAddress (add );
72
71
}
73
72
this .client = transportClient ;
74
- } else {
73
+ } else {
74
+ /************************
75
75
// when only 'client' option is set, a node without data is initialized and joins the cluster
76
76
this.node = NodeBuilder.nodeBuilder().client(this.isClient).clusterName(this.clusterName).local(this.isLocal).node();
77
77
this.client = node.client();
78
- }
78
+ ***************************/
79
+ }
79
80
80
81
// if (this.resetData) { // removes all indices from elastic search. For Integration testing only.
81
82
// this.node.client().admin().indices().prepareDelete().execute().actionGet();
82
83
// }
83
- LOGGER .info ("Initialized ElasticSearch client for cluster <" + this .clusterName + ">" );
84
+ log .info ("Initialized ElasticSearch client for cluster <" + this .clusterName + ">" );
84
85
}
85
86
86
87
@ PreDestroy
87
88
public void close () {
88
89
if (client != null ) {
89
90
client .close ();
90
91
}
92
+ /***
91
93
if (node != null) {
92
94
node.close();
93
95
}
94
- LOGGER .info ("Closed ElasticSearch client for cluster <" + this .clusterName + ">" );
96
+ ****/
97
+ log .info ("Closed ElasticSearch client for cluster <" + this .clusterName + ">" );
95
98
}
96
99
97
100
/**
@@ -103,6 +106,15 @@ public Client getClient() {
103
106
return this .client ;
104
107
}
105
108
109
+ /**
110
+ * Set client (for tests)
111
+ *
112
+ * @param client ES client
113
+ */
114
+ public void setClient (Client client ) {
115
+ this .client = client ;
116
+ }
117
+
106
118
/**
107
119
* Wait for green status for the given indices.
108
120
*
@@ -115,14 +127,14 @@ public ClusterHealthResponse waitForGreenStatus(String... indices) {
115
127
builder .setWaitForGreenStatus ();
116
128
builder .setTimeout (TimeValue .timeValueSeconds (30 ));
117
129
ClusterHealthResponse response = builder .execute ().actionGet ();
118
- LOGGER .debug ("getStatus : {}" , response .getStatus ());
119
- LOGGER .debug ("getActivePrimaryShards : {}" , response .getActivePrimaryShards ());
120
- LOGGER .debug ("getActiveShards : {}" , response .getActiveShards ());
121
- LOGGER .debug ("getInitializingShards : {}" , response .getInitializingShards ());
122
- LOGGER .debug ("getNumberOfDataNodes : {}" , response .getNumberOfDataNodes ());
123
- LOGGER .debug ("getNumberOfNodes : {}" , response .getNumberOfNodes ());
124
- LOGGER .debug ("getRelocatingShards : {}" , response .getRelocatingShards ());
125
- LOGGER .debug ("getUnassignedShards : {}" , response .getUnassignedShards ());
130
+ log .debug ("getStatus : {}" , response .getStatus ());
131
+ log .debug ("getActivePrimaryShards : {}" , response .getActivePrimaryShards ());
132
+ log .debug ("getActiveShards : {}" , response .getActiveShards ());
133
+ log .debug ("getInitializingShards : {}" , response .getInitializingShards ());
134
+ log .debug ("getNumberOfDataNodes : {}" , response .getNumberOfDataNodes ());
135
+ log .debug ("getNumberOfNodes : {}" , response .getNumberOfNodes ());
136
+ log .debug ("getRelocatingShards : {}" , response .getRelocatingShards ());
137
+ log .debug ("getUnassignedShards : {}" , response .getUnassignedShards ());
126
138
//LOGGER.debug("getAllValidationFailures : {}", response.getAllValidationFailures());
127
139
return response ;
128
140
}
0 commit comments