How to run code on startup in Play! framework 2.4
Asked Answered
S

1

14

I im trying to print "Hello" to console on application start. Can You explain how to do it?

What i tried myself:

app/modules/HelloModule.scala:

package modules

import com.google.inject.AbstractModule

trait Hello {}

class MyHelloClass extends Hello {
  initialize() // running initialization in constructor
  def initialize() = {
    println("Hello")
  }
}

class HelloModule extends AbstractModule {
  def configure() = {
    bind(classOf[Hello])
      .to(classOf[MyHelloClass]).asEagerSingleton
  }
}

in conf/application.conf i added:

play.modules.enabled += "modules.HelloModule"

and "Hello" is not printed when i run activator run

September answered 15/9, 2015 at 10:2 Comment(3)
Is "Hello" displayed when the first request is received? In dev mode, play lazily start the app until the first request comes. Btw you can just write bind(classOf[MyHelloClass]).asEagerSingleton.Chetnik
after activator run i request localhost:9000/books (it's simple-rest-scala activator template) and "Hello" is not displayedSeptember
See this #36454455Louettalough
D
7

You need to use Global object, and override "onStart" method:

Defining a Global object in your project allows you to handle global settings for your application. This object must be defined in the default (empty) package and must extend 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...")
  }

}

You can also specify a custom GlobalSettings implementation class name using the application.global configuration key.

Update:

The correct way would be to use Dependency Injection, exactly like it described in the question. GlobalSettings could be removed later

There is no problem with the code in the question. I verified it on my local setup. The code write "Hello" after first request in the development mode "activator run" and after application start in the production mode "activator start".

Btw, try to use some more easy to find string in the log, like

"--------APP DZIABLO HAS BEEN STARTED--------"

It could be so you just missed "Hello" in the log (I did not recognise it from the start)

enter image description here

Disintegration answered 16/9, 2015 at 9:10 Comment(6)
It works, thank You, but should We use GlobalSettings when there is guide how to use dependency injection instead of Global object? link I mean, won't it be deprecated or removed from framework in the future?September
for sure you need to use DI, GlobalSettings is just simplest way to "print "Hello" to console on application start"Disintegration
@September - look on my update please. I do not see any problems with your code. I did verification on my own local projectDisintegration
I created fresh scala-play template, added code and it works. Maybe there is something wrong with simple-rest-scala template.. Anyway, thank You very much!September
I don't see how adding a deprecated code snippet is marked up a solution to this problem. The reason that you did not see "Hello" is that activator run starts the server it does not start your application.Kleeman
@Haurus: exactly this explained in the "update" section, is not it?Disintegration

© 2022 - 2024 — McMap. All rights reserved.