0

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....

Felipe
  • 7,013
  • 8
  • 44
  • 102
  • Did you register the new `EagerLoaderModule`? See https://www.playframework.com/documentation/2.6.x/ScalaDependencyInjection#Programmatic-bindings – Tzach Zohar Jan 12 '18 at 16:10
  • do you mean the `play.modules.enabled += "bootstrap.EagerLoaderModule"`? I did as I explained in the question ....... – Felipe Jan 12 '18 at 17:41
  • I assume you are using `sbt run` and then sending a request to the application. Is that right? – marcospereira Jan 13 '18 at 02:47
  • yes, I am sending a request after `sbt run`. – Felipe Jan 16 '18 at 01:22
  • I would try to inject this class into one of your controllers to make sure it works like that. If that works, try to check whether the module is loaded (not sure if there's logging for that, I'd probably just attach a debugger). – rethab Jan 16 '18 at 12:36

1 Answers1

1

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

F. Lins
  • 636
  • 5
  • 14