3

I tried to run an example code of elastic4s, as follows,

import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._

object hw extends App {
  val client = ElasticClient.local
  client.execute(create index "bands")
  client.execute { index into "bands/artists" fields "name"->"coldplay" }.await
  val resp = client.execute { search in "bands/artists" query "coldplay" }.await
  println(resp)
  client.close
}

The program correctly prints the results, but it does not exit itself. I don't know if there are problems with my code or environment.

Fei Jiang
  • 330
  • 1
  • 6
  • The JVM doesn't exit until all threads have exited. I'm not familiar with elastic4s, but you might want to do `client.shutdown` before `client.close`? Or run it in a debugger and check which threads are still running. – lmm Dec 22 '14 at 16:58
  • I can confirm the same behaviour. In fact it happens even if you just open the client, and immediately close it. The close call is just a wrapper around the Java client, so I'm not sure why it not terminating immediately. – sksamuel Dec 22 '14 at 17:18
  • @Imm that does not work... – Fei Jiang Dec 23 '14 at 01:57

1 Answers1

2

Try using shutdown. shutdown will actually delegate to prepareNodesShutdown, which is a method of ClusterAdminClient and shutdowns a node. shutdown without any argument will shutdown the local node.

EDIT: added code and javadoc link

The following did work for me and worked as expected with elastic4s 1.4.0 (i.e. main is terminated)

import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._

object Main extends App {
  val client = ElasticClient.local
  client.execute(create index "bands")
  client.execute { index into "bands/artists" fields "name"->"coldplay" }.await
  val resp = client.execute { search in "bands/artists" query "coldplay" }.await
  println(resp)
  client.close()
  client.shutdown
}
edi
  • 3,112
  • 21
  • 16
  • Is there a shutdown method? There is the javadoc for the client class, I cannot see shutdown, only close. http://javadoc.kyubu.de/elasticsearch/v1.4.2/org/elasticsearch/client/Client.html – sksamuel Dec 23 '14 at 15:08
  • This does not work. Even calling sys.exit(0) at the last line does not work either. I don't know why. – Fei Jiang Dec 23 '14 at 15:15
  • Well, I've only checked the [source](https://github.com/sksamuel/elastic4s/blob/master/src/main/scala/com/sksamuel/elastic4s/Client.scala) for elastic4s and shutdown seems to a wrapper around `java.admin.cluster.prepareNodesShutdown` – edi Dec 23 '14 at 15:16
  • The example posted up by @user3567830 works for me. I think it might be slightly more proper to call shutdown before closing the client, but either way it seems to work. – sksamuel Dec 23 '14 at 16:48