本文整理汇总了Scala中play.api.inject.ApplicationLifecycle类的典型用法代码示例。如果您正苦于以下问题:Scala ApplicationLifecycle类的具体用法?Scala ApplicationLifecycle怎么用?Scala ApplicationLifecycle使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ApplicationLifecycle类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ApplicationTimer
//设置package包名称以及导入依赖的类
package services
import java.time.{Clock, Instant}
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future
@Singleton
class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle) {
// This code is called when the application starts.
private val start: Instant = clock.instant
Logger.info(s"ApplicationTimer demo: Starting application at $start.")
// When the application starts, register a stop hook with the
// ApplicationLifecycle object. The code inside the stop hook will
// be run when the application stops.
appLifecycle.addStopHook { () =>
val stop: Instant = clock.instant
val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
Future.successful(())
}
}
示例2: ApplicationTimer
//设置package包名称以及导入依赖的类
package services
import java.time.{Clock, Instant}
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future
@Singleton
class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle) {
// This code is called when the application starts.
private val start: Instant = clock.instant
Logger.info(s"ApplicationTimer demo: Starting application at ${start}.")
// When the application starts, register a stop hook with the
// ApplicationLifecyle object. The code inside the stop hook wil
// be run when the application stops.
appLifecycle.addStopHook { () =>
val stop: Instant = clock.instant
val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
Future.successful(())
}
}
示例3: ApplicationTimer
//设置package包名称以及导入依赖的类
package services
import java.time.{Clock, Instant}
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future
@Singleton
class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle) {
// This code is called when the application starts.
private val start: Instant = clock.instant
Logger.info(s"ApplicationTimer demo: Starting application at $start.")
// When the application starts, register a stop hook with the
// ApplicationLifecycle object. The code inside the stop hook will
// be run when the application stops.
appLifecycle.addStopHook { () =>
val stop: Instant = clock.instant
val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
Future.successful(())
}
}
示例4: ApplicationTimer
//设置package包名称以及导入依赖的类
package services
import java.time.{Clock, Instant}
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future
@Singleton
class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle) {
// This code is called when the application starts.
private val start: Instant = clock.instant
Logger.info(s"ApplicationTimer demo: Starting application at $start.")
// When the application starts, register a stop hook with the
// ApplicationLifecycle object. The code inside the stop hook wil
// be run when the application stops.
appLifecycle.addStopHook { () =>
val stop: Instant = clock.instant
val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
Future.successful(())
}
}
示例5: ApplicationTimer
//设置package包名称以及导入依赖的类
package services
import java.time.{Clock, Instant}
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future
@Singleton
class ApplicationTimer @Inject()(clock: Clock, appLifecycle: ApplicationLifecycle) {
// This code is called when the application starts.
private val start: Instant = clock.instant
Logger.info(s"ApplicationTimer demo: Starting application at $start.")
// When the application starts, register a stop hook with the
// ApplicationLifecycle object. The code inside the stop hook will
// be run when the application stops.
appLifecycle.addStopHook { () =>
val stop: Instant = clock.instant
val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
Future.successful(())
}
}
示例6: ApplicationTimer
//设置package包名称以及导入依赖的类
package modules.samples
import java.time.{Clock, Instant}
import java.util.concurrent.TimeUnit
import javax.inject.{Inject, Singleton}
import akka.actor.ActorSystem
import com.github.sample.utils.Loggable
import play.api.inject.ApplicationLifecycle
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._
@Singleton
class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle, actorSystem: ActorSystem) extends Loggable {
implicit val myExecutionContext: ExecutionContext = actorSystem.dispatcher
private val start: Instant = clock.instant
logger.info(s"ApplicationTimer demo: Starting application at $start.")
actorSystem.scheduler.schedule(Duration.Zero, Duration.create(10, TimeUnit.SECONDS)) {
logger.info(clock.instant.toString)
}
// When the application starts, register a stop hook with the
// ApplicationLifecycle object. The code inside the stop hook will
// be run when the application stops.
appLifecycle.addStopHook { () =>
val stop: Instant = clock.instant
val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
Future.successful(())
}
}
示例7: CassandraAsyncContextImpl
//设置package包名称以及导入依赖的类
package data
import javax.inject.{Inject, Singleton}
import com.datastax.driver.core.{Cluster, Session}
import com.typesafe.config.Config
import io.getquill.{CassandraAsyncContext, SnakeCase}
import play.api.inject.ApplicationLifecycle
import play.api.{Configuration, Environment, Logger}
import util.FutureUtils.toFutureUnit
import scala.concurrent.ExecutionContext
@Singleton
class CassandraAsyncContextImpl @Inject()(cassandra: CassandraCluster, conf: CassandraConfig)
extends CassandraAsyncContext[SnakeCase](cassandra.cluster, conf.keySpace, 100L)
@Singleton
class CassandraCluster @Inject()(conf: CassandraConfig, appLifecycle: ApplicationLifecycle)(implicit executor: ExecutionContext) {
import conf._
private[data] val cluster =
Cluster.builder()
.addContactPoints(hosts: _*)
.withPort(port)
.build()
private[data] def noKeySpaceSession: Session = cluster.connect()
private[data] def stop() = toFutureUnit(cluster.closeAsync())
Logger.info(s"Cassandra host to be used: '${hosts.mkString(",")}' with port:$port")
appLifecycle.addStopHook(() => stop())
}
@Singleton
class CassandraConfig @Inject()(configuration: Configuration, environment: Environment) {
val config: Config = configuration.underlying
val keySpace = config.getString("devgym.db.cassandra.keyspace")
val port = config.getInt("devgym.db.cassandra.port")
val hosts: Seq[String] =
configuration.getStringSeq("devgym.db.cassandra.hosts").get
}
示例8: ApplicationTimer
//设置package包名称以及导入依赖的类
package com.ddn.wechat.service
import java.time.{Clock, Instant}
import javax.inject._
import play.api.Logger
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future
@Singleton
class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle) {
// This code is called when the application starts.
private val start: Instant = clock.instant
Logger.info(s"ApplicationTimer demo: Starting application at ${start}.")
// When the application starts, register a stop hook with the
// ApplicationLifecyle object. The code inside the stop hook wil
// be run when the application stops.
appLifecycle.addStopHook { () =>
val stop: Instant = clock.instant
val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
Future.successful(())
}
}
示例9: DatabaseProductService
//设置package包名称以及导入依赖的类
package service
import javax.inject.{Inject, Singleton}
import com.outworkers.phantom.dsl.context
import com.typesafe.scalalogging.StrictLogging
import model.db.ProdDb
import model.domain.{Category, CategoryDistribution, StoreProduct}
import play.api.Environment
import play.api.inject.ApplicationLifecycle
import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NoStackTrace
@Singleton
class DatabaseProductService @Inject()(
environment: Environment,
applicationLifecycle: ApplicationLifecycle
)(
implicit exec: ExecutionContext
) extends StoreFront with StrictLogging with ProdDb {
override def getProduct(title: String): Future[Option[StoreProduct]] = {
database.products.findByTitle(title)
}
override def addProductToCategory(title: String, category: Category): Future[Unit] = {
getProduct(title) flatMap {
case Some(product) =>
for {
cat <- database.productsByCategory.store(category, product)
prod <- database.products.addCategory(title, category)
} yield ()
case None => Future.failed(new Exception(s"Could not find product with title $title") with NoStackTrace)
}
}
override def addProduct(product: StoreProduct): Future[Unit] = {
for {
_ <- database.products.store(product)
cats <- Future.sequence(product.categories.values.map(c => database.productsByCategory.store(c, product)))
} yield ()
}
}