Felipe
Felipe

Reputation: 7563

How to execute action on Play framework startup?

I have this application using Play framework with Scala and I want to have a service that executes a method on startup of my application. I am doing everything that is said at How do I perform an action on server startup in the Scala Play Framework?. My version of Play is 2.6 and I am not using GlobalSettings for this.

package bootstrap
import com.google.inject.AbstractModule
class EagerLoaderModule extends AbstractModule {
  override def configure() = {
    println("EagerLoaderModule.configure")
    bind(classOf[InitSparkContext]).to(classOf[InitSparkContextImpl]).asEagerSingleton()
  }
}

On the application.conf I included the line play.modules.enabled += "bootstrap.EagerLoaderModule". Below is my Service that I want to start Spark context.

package bootstrap

import javax.inject.{Inject, Singleton}
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future

trait InitSparkContext {
  def init(): Unit
  def stop(): Unit
}

@Singleton
class InitSparkContextImpl @Inject()(appLifecycle: ApplicationLifecycle) extends InitSparkContext {

  override def init(): Unit = println("InitSparkContext.start")
  override def stop(): Unit = println("InitSparkContext.stop")
  appLifecycle.addStopHook { () =>
    stop()
    Future.successful(())
  }
  init()
}

Nothing is printed on the console, even println("EagerLoaderModule.configure") is not printed....

Upvotes: 0

Views: 1066

Answers (1)

F. Lins
F. Lins

Reputation: 636

You can't use println on play application, you'll need to set up a Logger. So:

val log = play.api.Logger(getClass)
log.info("This happened")

Then you can use the logback file to configure your log files: Here are some details on how to configure it: https://www.playframework.com/documentation/2.6.x/SettingsLogger

Upvotes: 1

Related Questions