playframework - How to add 'init' and 'destroy' like methods in scala play webapp? -


i new play, writing webapp using scala, in wish implement following functionality (play version - 2.3.9):

  1. when application runs first time, 5 separate variables read file (or db - yet decided).

  2. these variables can globally interfered , updated while app runs.

  3. when app shuts down, file (or db) saved latest values these variables.

i need define sort of java servlet-like init , destroy functionalities achieve task. guide me on how achieve this?

based on play 2.3.9 documentation should define hooks via globalsettings:

import play.api._  object global extends globalsettings {    override def onstart(app: application) {      logger.info("application has started")   }    override def onstop(app: application) {      logger.info("application shutdown...")   }  } 

but recommend move newer version >= 2.4, there globalsetting deprecated, , way add start , end hooks changed.

in order define "start hook" can add own guice module application configuration, there can write ever need heppen when application starts.

and in order add "stop hook" should use applicationlifecycle, see here more.

import scala.concurrent.future import javax.inject._ import play.api.inject.applicationlifecycle  @singleton class messagequeueconnection @inject() (lifecycle: applicationlifecycle) {    val connection = connecttomessagequeue()    lifecycle.addstophook { () =>       future.successful(connection.stop())    }     //... } 

Comments