Packages

t

play.api.inject

ApplicationLifecycle

trait ApplicationLifecycle extends AnyRef

Application lifecycle register.

This is used to hook into Play lifecycle events, specifically, when Play is stopped. The reason Play only provides lifecycle callbacks for stopping is that constructors are considered the application start callback. This has several advantages:

- It simplifies implementation, if you want to start something, just do it in the constructor. - It simplifies state, there's no transitional state where an object has been created but not started yet. Hence, as long as you have a reference to something, it's safe to use it. - It solves startup dependencies in a type safe manner - the order that components must be started is enforced by the order that they must be instantiated due to the component graph.

Stop hooks are executed when the application is shutdown, in reverse from when they were registered. Due to this reverse ordering, a component can know that it is safe to use the components it depends on as long as it hasn't received a shutdown event.

To use this, declare a dependency on ApplicationLifecycle, and then register the stop hook when the component is started. For example:

import play.api.inject.ApplicationLifecycle
import javax.inject.Inject

class SomeDatabase @Inject() (applicationLifecycle: ApplicationLifecycle) {

  private val connectionPool = new SomeConnectionPool()
  applicationLifecycle.addStopHook { () =>
    Future.successful(connectionPool.shutdown())
  }

  ...
}
Source
ApplicationLifecycle.scala
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ApplicationLifecycle
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Abstract Value Members

  1. abstract def addStopHook(hook: () => Future[_]): Unit

    Add a stop hook to be called when the application stops.

    Add a stop hook to be called when the application stops.

    The stop hook should redeem the returned future when it is finished shutting down. It is acceptable to stop immediately and return a successful future.

  2. abstract def stop(): Future[_]

    Call to shutdown the application and execute the registered hooks.

    Call to shutdown the application and execute the registered hooks.

    Since 2.7.0, implementations of stop are expected to be idempotent so invoking the method several times only runs the process once.

    returns

    A future that will be redeemed once all hooks have executed.

    Annotations
    @deprecated
    Deprecated

    (Since version 2.7.0) Do not invoke stop() directly. Instead, use CoordinatedShutdown.run to stop and release your resources.

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. def addStopHook(hook: Callable[_ <: CompletionStage[_]]): Unit

    Add a stop hook to be called when the application stops.

    Add a stop hook to be called when the application stops.

    The stop hook should redeem the returned future when it is finished shutting down. It is acceptable to stop immediately and return a successful future.

  5. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  6. def asJava: inject.ApplicationLifecycle

    returns

    the Java version for this Application Lifecycle.

  7. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @HotSpotIntrinsicCandidate() @native()
  8. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  9. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  10. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  11. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  12. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  13. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  14. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  15. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  16. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  17. def toString(): String
    Definition Classes
    AnyRef → Any
  18. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  19. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  20. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

    (Since version 9)

Inherited from AnyRef

Inherited from Any

Ungrouped