shutdown hook within Tomcat

430 views
Skip to first unread message

Maatary Okouya

unread,
Jun 8, 2015, 6:08:52 PM6/8/15
to akka...@googlegroups.com
Hi, 

I am using Spray client within tomcat. I am wondering if any one has ever faced this situation. I need to shutdown my spray client when tomcat is shutdown. However the classic shutdown hook as in 


Runtime.getRuntime().addShutdownHook(new Thread() {

override def run() {
println("shutting down spray")
IO(Http) ! Http.CloseAll
IO(Http) ! PoisonPill
system.shutdown();
}
})


Does not work. 

I introduce it in my service that actually to rest call to other service that are external from within a tomcat webapp


case class PClassificationService(name: String) extends ClassificationService {


implicit val requestTimeout = Timeout(800 seconds)
implicit val system = ActorSystem("simple-spray-client")


/*Runtime.getRuntime().addShutdownHook(new Thread() {

override def run() {
println("shutting down spray")
IO(Http) ! Http.CloseAll
IO(Http) ! PoisonPill
system.shutdown();
}
})*/

......

Guido Medina

unread,
Jun 8, 2015, 6:17:09 PM6/8/15
to akka...@googlegroups.com
For Tomcat 7 and up it is very easy with Servlet 3.0 specification, basically you create create a listener and annotate it, then just add your shutdown code in the context destroyed method:

Maatary Okouya

unread,
Jun 8, 2015, 9:41:47 PM6/8/15
to akka...@googlegroups.com
Thank you looking into it.

But one question into it: how would you reference your actor system from here, if you actually create it, from within a another class.

Indeed I do not inject the actor system in my class but create it inside it.

Basically it is a class that provide a client to communicate with external services.

Any hints on that ?

What I mean how do you call system.shutdown

Guido Medina

unread,
Jun 9, 2015, 4:28:50 AM6/9/15
to akka...@googlegroups.com
Like you would reference any other singleton instance, as a static variable for example, ActorSystem is a heavy object so it is recommended to have just one per JVM anyway.

Maatary Okouya

unread,
Jun 9, 2015, 12:59:13 PM6/9/15
to akka...@googlegroups.com
Got it thanks
Reply all
Reply to author
Forward
0 new messages