How to implement playback platform lifecycle callbacks (2.5.x)

I am trying to learn the game interface. I want to implement playback platform lifecycle callbacks in my application. Now I saw that this can be easily done using GlobalSettings below:

object Global extends GlobalSettings { override def onStart(app: Application) { Logger.info("Application has started") } override def onStop(app: Application) { Logger.info("Application shutdown...") } } 

But it is deprecated in the play framework (2.5.x) . And they provide reliable binding for onStart , and other mechanisms exist for onStop and onError . I looked at the documentation for release 2.5.x and saw the code there, as shown below:

 import com.google.inject.AbstractModule import com.google.inject.name.Names class Module extends AbstractModule { def configure() = { bind(classOf[Hello]) .annotatedWith(Names.named("en")) .to(classOf[EnglishHello]).asEagerSingleton bind(classOf[Hello]) .annotatedWith(Names.named("de")) .to(classOf[GermanHello]).asEagerSingleton } } 

But, unfortunately, I could not understand this. Like using GlobalSettings, it was easy enough to implement lifecycle callbacks. Suppose I just embed Logger information in lifecycle callbacks. No complicated codes.
How can I implement this for start, stop and error callbacks in 2.5.x ??

+5
source share
1 answer

In general, moving these mechanisms away from the GlobalSettings object also means that you no longer register such β€œcallbacks” globally, but bind them to a component / class. This gives the advantage that the initialization and shutdown of a particular component can occur directly inside the corresponding class. However, if you have code that you want to run on startup (or shutdown) that is not tied to a specific component (for example, logging, startup checks, etc.), you will have to create new classes for them and bind them in your module.

Note that in the latter case, you usually bind the corresponding classes as strong single ones (to make sure they are created), while in the first case the classes are created as part of the dependency tree.

Launch . Run the code in the constructor of any class that is controlled by the dependency injection container.

  • Link class in module
 bind(classOf[Hello]).to(classOf[EnglishHello]).asEagerSingleton 
  1. Put code in constructor
 class EnglishHello extends Hello { println("hello") } 

Please note that asEagerSingleton is not required on its own. Since I assume that you are using the manual for the DI provider, you can learn more about it here: https://github.com/google/guice/wiki/Scopes

Shutdown . In any class that needs to run some shutdown code, register a lifecycle callback. 1. Link the class in the module

 bind(classOf[Hello]).to(classOf[EnglishHello]).asEagerSingleton 
  1. Register a lifecycle callback (and enter ApplicationLifecycle)
 class EnglishHello @Inject() (lifecycle: ApplicationLifecycle) extends Hello { lifecycle.addStopHook { () => Future.successful(connection.stop()) } } 

Note that you might want to cover these classes as single classes, because otherwise you end up registering the stop hooks for each instance - depending on what you do with the stop, this might be what you want. More on this here: https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection#Stopping/cleaning-up

Errors : run your own HttpErrorHandler. The basic idea is that you implement a class with a number of methods that will be called by Play! to the corresponding errors. This is described here: https://www.playframework.com/documentation/2.5.x/ScalaErrorHandling

+5
source

All Articles