@@ -70,7 +70,7 @@ class ElasticSearchWriter(esConfig: ElasticSearchConfiguration, indexConf: White
70
70
private val inflightRequestsSemaphore = new Semaphore (esConfig.maxInFlightBulkRequests, true )
71
71
72
72
// initialize the elastic search client
73
- private val esClient : JestClient = {
73
+ private lazy val esClient : JestClient = {
74
74
LOGGER .info(" Initializing the http elastic search client with endpoint={}" , esConfig.endpoint)
75
75
76
76
val factory = new JestClientFactory ()
@@ -85,13 +85,21 @@ class ElasticSearchWriter(esConfig: ElasticSearchConfiguration, indexConf: White
85
85
}
86
86
87
87
factory.setHttpClientConfig(builder.build())
88
- factory.getObject
88
+ val client = factory.getObject
89
+
90
+ if (esConfig.indexTemplateJson.isDefined) {
91
+ val putTemplateRequest = new PutTemplate .Builder (" spans-index-template" , esConfig.indexTemplateJson.get).build()
92
+ val result = client.execute(putTemplateRequest)
93
+ if (! result.isSucceeded) {
94
+ throw new RuntimeException (s " Fail to apply the following template to elastic search with reason= ${result.getErrorMessage}" )
95
+ }
96
+ }
97
+
98
+ client
89
99
}
90
100
91
101
private val bulkBuilder = new ThreadSafeBulkBuilder (esConfig.maxDocsInBulk, esConfig.maxBulkDocSizeInBytes)
92
102
93
- if (esConfig.indexTemplateJson.isDefined) applyTemplate(esConfig.indexTemplateJson.get)
94
-
95
103
override def close (): Unit = {
96
104
LOGGER .info(" Closing the elastic search client now." )
97
105
Try (esClient.shutdownClient())
@@ -152,12 +160,4 @@ class ElasticSearchWriter(esConfig: ElasticSearchConfiguration, indexConf: White
152
160
None
153
161
}
154
162
}
155
-
156
- private def applyTemplate (templateJson : String ) {
157
- val putTemplateRequest = new PutTemplate .Builder (" spans-index-template" , templateJson).build()
158
- val result = esClient.execute(putTemplateRequest)
159
- if (! result.isSucceeded) {
160
- throw new RuntimeException (s " Fail to apply the following template to elastic search with reason= ${result.getErrorMessage}" )
161
- }
162
- }
163
163
}
0 commit comments