In a Scala-based Play application, I'm trying to initiate a singleton service without requiring a request to a controller. I've followed the directions in the 2.4 API documentation to create a singleton class and then use Guice's dependency injection library to bind the class as an eager singleton.
Even with eager binding, the singleton still doesn't get called until after I've received a request through a controller route. Any ideas as to 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
}
Run Command
sbt compile run
The above singleton does not get called until I issue a...
curl http://localhost:9000/
What I want is for MessageLogService to start at the point of running the service and not wait for a request to hit a controller route.
run
andstart
: #18213927 – Methylal