How to initialize a single singleton without requiring a controller request in Play?

In a Scala-based playback application, I am trying to initiate a single-user service without requiring a request to the controller. I followed the directions in the 2.4 API documentation to create a singleton class, and then use the Guice dependency injection library to bind this class as an impatient singleton.

Even with vigorous binding, singleton is still not called until I receive a request through the controller route. Any ideas on what I'm doing wrong?

Module

package models import com.google.inject.AbstractModule import com.google.inject.name.Names class MessageLogModule extends AbstractModule { def configure() = { bind(classOf[MessageLogService]).asEagerSingleton } } 

Configuration

 play.modules.enabled += "models.MessageLogModule" 

Singleton

 package models import javax.inject._ @Singleton class MessageLogService { // Create a file to test println("IN SINGLETON - CREATING NEW FILE") val file = new java.io.File("howdy.txt") file.createNewFile } 

Launch command

 sbt compile run 

The above singleton is not called until I issue ...

 curl http://localhost:9000/ 

I want MessageLogService to start from the moment the service starts and not wait for the request to hit the controller route.

+5
source share
1 answer

What you need: sbt compile start

run delays compilation and initialization until the first request allows you to speed up the change-update-view-change development cycle.

+5
source

Source: https://habr.com/ru/post/1241740/


All Articles