11package com .sksamuel .elastic4s .http
22
3- import java .io .InputStream
3+ import java .io .{ Closeable , InputStream }
44import java .nio .charset .StandardCharsets
55import java .util .zip .GZIPInputStream
66import com .sksamuel .elastic4s .{
@@ -24,7 +24,8 @@ case class JavaClientExceptionWrapper(t: Throwable) extends RuntimeException(t)
2424
2525/** An implementation of HttpClient that wraps the Elasticsearch Java Rest Client
2626 */
27- class JavaClient (client : RestClient )(implicit ec : ExecutionContext ) extends HttpClient [Future ] {
27+ class JavaClient (client : RestClient , onClose : Option [Closeable ] = None )(implicit ec : ExecutionContext )
28+ extends HttpClient [Future ] {
2829
2930 def apacheEntity (entity : HttpEntity ): AbstractHttpEntity = entity match {
3031 case e : HttpEntity .StringEntity =>
@@ -87,7 +88,10 @@ class JavaClient(client: RestClient)(implicit ec: ExecutionContext) extends Http
8788 promise.future
8889 }
8990
90- override def close (): Future [Unit ] = Future (client.close())
91+ override def close (): Future [Unit ] = Future {
92+ onClose.foreach(_.close()) // close sniffer first, it references the client
93+ client.close()
94+ }
9195
9296 private def isEntityGziped (entity : org.apache.http.HttpEntity ): Boolean = {
9397 Option (entity.getContentEncoding).flatMap(x => Option (x.getValue)).contains(" gzip" )
@@ -105,7 +109,8 @@ object JavaClient {
105109 * @return
106110 * newly created Scala client
107111 */
108- def fromRestClient (client : RestClient )(implicit ec : ExecutionContext ): JavaClient = new JavaClient (client)
112+ def fromRestClient (client : RestClient , onClose : Option [Closeable ] = None )(implicit ec : ExecutionContext ): JavaClient =
113+ new JavaClient (client, onClose)
109114
110115 /** Creates a new [[ElasticClient ]] using the elasticsearch Java API rest client as the underlying client. Optional
111116 * callbacks can be passed in to configure the client.
0 commit comments