문제

I'm writing a server that runs in a loop and terminates in case of SIGINT(ctrl-c) or terminate button pressed in the console window of Eclipse IDE. I want it to shutdown gracefully printing out termination logs. But the problem is that println doesn't seem to work while shutdown sequence triggered by pressing the terminate button in Eclipse IDE. Look at the simple code below:

object Test extends App {
  println("start")
  Runtime.getRuntime().addShutdownHook(new Thread {
    override def run = println("shutdown")
  })
  synchronized { wait }
}

It works well with the command line scala tool. Both messages "start" and "shutdown" are printed when I hit ctrl-c. But the "shutdown" message isn't printed when I run it in Eclipse IDE and hit the terminate button of the console window. It just terminates silently. I've verified that everything else in the shutdown hook runs correctly while termination. It's only println that doesn't work.

Any idea about this? I need to print out termination messages for logging.
Thanks for your help in advance!

도움이 되었습니까?

해결책

Try logging the shutdown messages to a file instead. Eclipse probably closes its end of the stdout pipe before the program is really terminated.

다른 팁

Shutdown hooks don't get run by Eclipse when shutting down a process: https://bugs.eclipse.org/bugs/show_bug.cgi?id=38016

That bug report is resolved as Won't fix

I realise that its an old bug and so maybe things have changed since then.

You can try the shutDownHook which scala provides

sys addShutdownHook(println("shutdown")) 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top