Plugin definition
A Play plugin can be defined by extending play.api.plugin
, which is defined as follows:
trait Plugin { //Called when the application starts. def onStart() {} // Called when the application stops. def onStop() {} // Is the plugin enabled? def enabled: Boolean = true }
Now, we might be in a situation where we need to send an e-mail when an application is started or stopped so that the administrator can later use this time interval to monitor the application's performance and check why it stopped. We could define a plugin to do this for us:
class NotifierPlugin(app:Application) extends Plugin{ private def notify(adminId:String,status:String):Unit = { val time = new Date() val msg = s"The app has been $status at $time" //send email to admin with the msg log.info(msg) } override def onStart() { val emailId = app.configuration.getString("notify.admin.id").get notify(emailId,"started"...