Search code examples
javascalajarakkasbt-assembly

System.exit working in "sbt run" but not in .jar


When shutting down my program I need to let my akka actors finish what they're doing before shutting down, so I have a shutdown hook:

sys.addShutdownHook(
    {
      log.info("\n shutting down startWorkScheduler\n")
      assignWorkScheduler.cancel()
      log.info("\ntelling manager to shutdown gracefully\n")
      manager ! Manager.ShutdownGracefully
      //spam shutdown messages until we exit
      val resendShutDown = system.scheduler.schedule(5 seconds, 2 seconds){
        manager ! Manager.ShutdownGracefully
      }
      system.awaitTermination()
    }

and elsewhere I wait for a "all ok, you can shutdown" message:

case Terminated(httpDlrSqlRouter) => {
      println("received terminated message from http router")
      if(shuttingDown) {
        println("httpDlrRouter done, shutting down......")
        log.info("httpDlrRouter done, shutting down......")
        context.system.shutdown()
        java.lang.System.exit(0)
      }else{
        log.warning("httpDlrRouter terminated, don't know why!")
      }
    }

When I run the code using sbt run it works as expected: kill or CTRL+c finishes the work, prints "httpDlrRouter done, shutting down......" and exits.

When I build a .jar using sbt assembly and run it using java -jar filename.jar it does the same except actually exiting, it just hangs until i kill it with kill -9

do I need any alternatives to System.exit or some configuration to make it work?


Solution

  • Apparently changing

    java.lang.System.exit(0)
    

    to

    Runtime.getRuntime().halt(0)
    

    solved the problem