本文整理汇总了Scala中play.api.libs.concurrent.Akka类的典型用法代码示例。如果您正苦于以下问题:Scala Akka类的具体用法?Scala Akka怎么用?Scala Akka使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Akka类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ExecutionContexts
//设置package包名称以及导入依赖的类
package context
import play.api.libs.concurrent.Akka
import play.api.Play.current
object ExecutionContexts {
// 0-500 msecs
implicit val genericOps = Akka.system.dispatchers.lookup("contexts.generic-ops")
// 0-500 msecs
implicit val criticalOps = Akka.system.dispatchers.lookup("contexts.critical-ops")
// 1000 msecs-1 min
implicit val slowIoOps = Akka.system.dispatchers.lookup("contexts.slow-io-ops")
// More than 1 min
implicit val slowCpuOps = Akka.system.dispatchers.lookup("contexts.slow-cpu-ops")
}
示例2: sendEmailAsync
//设置package包名称以及导入依赖的类
package utils
import javax.inject.Inject
import play.api.Play.current
import play.api.libs.concurrent.Akka
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.mailer._
import scala.language.postfixOps
import scala.concurrent.duration._
trait MailService {
def sendEmailAsync(recipients: String*)(subject: String, bodyHtml: String, bodyText: String = ""): Unit
def sendEmail(recipients: String*)(subject: String, bodyHtml: String, bodyText: String = ""): Unit
}
class MailServiceImpl @Inject() (mailerClient: MailerClient) extends MailService {
def from: String = current.configuration.getString("play.mailer.from").getOrElse("UNKNOWN")
def sendEmailAsync(recipients: String*)(subject: String, bodyHtml: String, bodyText: String = ""): Unit = {
Akka.system.scheduler.scheduleOnce(100 milliseconds) {
sendEmail(recipients: _*)(subject, bodyHtml, bodyText)
}
}
def sendEmail(recipients: String*)(subject: String, bodyHtml: String, bodyText: String = ""): Unit = {
mailerClient.send(Email(
subject,
from,
recipients,
// sends text, HTML or both...
Some(bodyText),
Some(bodyHtml)
))
}
}
示例3: Global
//设置package包名称以及导入依赖的类
import play.api._
import models._
import akka.actor._
import play.api.libs.iteratee._
import play.api.libs.concurrent.Akka
import play.api.Play.current
object Global extends GlobalSettings {
override def onStart(app: Application) {
class Listener extends Actor {
var out: Option[Concurrent.Channel[String]] = None
def receive = {
case Start(out) => this.out = Some(out)
case UserRegistration => this.out.map(_.push("user"))
case NewMessage => this.out.map(_.push("message"))
}
}
val listener = Akka.system.actorOf(Props[Listener], "listener")
}
}
示例4: Application
//设置package包名称以及导入依赖的类
package controllers
import play.api._
import play.api.mvc._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import play.api.libs.concurrent.Akka
import play.api.Play.current
import models._
import play.api.libs.EventSource
import play.api.libs.iteratee._
class Application extends Controller {
val (out, channel) = Concurrent.broadcast[String]
val listener = Akka.system.actorSelection("akka://application/user/listener")
listener ! Start(channel)
def dashboard = Action {
Ok(views.html.dashboard())
}
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
def createNewUser = Action.async {
listener ! UserRegistration
Future.successful(Ok(""))
}
def sendMessage = Action.async {
listener ! NewMessage
Future.successful(Ok(""))
}
def stream = Action { implicit req =>
Ok.feed(out &> EventSource()).as("text/event-stream")
}
}
object Application extends Application
示例5: ChatController
//设置package包名称以及导入依赖的类
package controllers
import play.api.libs.json._
import play.api.mvc.WebSocket.FrameFormatter
import play.api.mvc._
import play.api.Play.current
import play.api.libs.concurrent.Akka
import akka.actor._
import java.util.Date
import java.text.SimpleDateFormat
object ChatController extends Controller {
// ?????????????????????????
case class ChatMessage(name: String, message: String, date: Option[String])
// ChatMessage?JSON????????????????
implicit val chatMessageFormat = Json.format[ChatMessage]
implicit val chatMessageFormatter = FrameFormatter.jsonFrame[ChatMessage]
// ?????????
def chat = Action {
implicit request =>
Ok(views.html.chat())
}
// ??????WebSocket????????
def socket = WebSocket.acceptWithActor[ChatMessage, ChatMessage] {
request => out =>
Akka.system.eventStream.subscribe(out, classOf[ChatMessage])
Props(new WebSocketActor())
}
// ????????????Actor
class WebSocketActor() extends Actor{
def receive = {
case msg: ChatMessage => {
val date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())
Akka.system.eventStream.publish(msg.copy(date = Some(date)))
}
}
}
}
示例6: ExecutionContexts
//设置package包名称以及导入依赖的类
package context
import play.api.libs.concurrent.Akka
import play.api.Play.current
object ExecutionContexts {
// 0-500 msecs
implicit val genericOps = Akka.system.dispatchers.lookup("contexts.generic-ops")
// 0-500 msecs
implicit val criticalOps = Akka.system.dispatchers.lookup("contexts.critical-ops")
// 1000 msecs-1 min
implicit val slowIoOps = Akka.system.dispatchers.lookup("contexts.slow-io-ops")
// More than 1 min
implicit val slowCpuOps = Akka.system.dispatchers.lookup("contexts.slow-cpu-ops")
}
示例7: sendEmail
//设置package包名称以及导入依赖的类
package services
import play.api.libs.concurrent.Akka
import play.api.Play.current
import play.api.mvc.RequestHeader
import play.api.i18n.Lang
import play.twirl.api.{ Txt, Html }
import com.typesafe.plugin._
import scala.concurrent.duration._
import play.api.libs.concurrent.Execution.Implicits._
import akka.actor._
import com.mohiva.play.silhouette.api._
def sendEmail(subject: String, recipient: String, body: (Option[Txt], Option[Html])) = {
play.Logger.debug(s"[securesocial] sending email to $recipient")
play.Logger.debug(s"[securesocial] mail = [$body]")
Akka.system.scheduler.scheduleOnce(1 seconds) {
val mail = use[MailerPlugin].email
mail.setSubject(subject)
mail.setRecipient(recipient)
mail.setFrom(fromAddress)
// the mailer plugin handles null / empty string gracefully
mail.send(body._1.map(_.body).getOrElse(""), body._2.map(_.body).getOrElse(""))
}
}
}
示例8: sendEmailAsync
//设置package包名称以及导入依赖的类
package utils
import javax.inject.Inject
import akka.actor.{ActorSystem, Props}
import com.google.inject.ImplementedBy
import play.api.Configuration
import play.api.Play.current
import play.api.libs.concurrent.Akka
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.mailer._
import scala.language.postfixOps
import scala.concurrent.duration._
@ImplementedBy(classOf[MailServiceImpl])
trait MailService {
def sendEmailAsync(recipients: String*)(subject: String, bodyHtml: String, bodyText: String): Unit
def sendEmail(recipients: String*)(subject: String, bodyHtml: String, bodyText: String): Unit
}
class MailServiceImpl @Inject() (mailerClient: MailerClient, val conf: Configuration) extends MailService with ConfigSupport {
lazy val from = confRequiredString("play.mailer.from")
def sendEmailAsync(recipients: String*)(subject: String, bodyHtml: String, bodyText: String) = {
Akka.system.scheduler.scheduleOnce(100 milliseconds) {
sendEmail(recipients: _*)(subject, bodyHtml, bodyText)
}
}
def sendEmail(recipients: String*)(subject: String, bodyHtml: String, bodyText: String) =
mailerClient.send(Email(subject, from, recipients, Some(bodyText), Some(bodyHtml)))
}
示例9: ServiceLocatorModule
//设置package包名称以及导入依赖的类
package com.lightbend.lagom.dns
import com.lightbend.lagom.javadsl.api.ServiceLocator
import play.api.{ Configuration, Environment, Mode }
import play.api.inject.{ Binding, Module }
import javax.inject.Singleton
import play.api.libs.concurrent.Akka
import com.lightbend.dns.locator.{ ServiceLocator => ServiceLocatorService }
class ServiceLocatorModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] =
if (environment.mode == Mode.Prod)
Seq(
bind[ServiceLocator].to[DnsServiceLocator].in[Singleton],
Akka.bindingOf[ServiceLocatorService]("ServiceLocatorService")
)
else
Seq.empty
}
示例10: Worker
//设置package包名称以及导入依赖的类
import play.api._
import play.api.libs.concurrent.Akka
import play.api.mvc._
import play.api.Logger
import scala.concurrent.Future
import akka.actor._
import scala.concurrent.duration._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.Play.current
object Worker extends GlobalSettings {
override def onStart(app: Application): Unit = {
val testActor = Akka.system.actorOf(MyActor.props)
Akka.system.scheduler.schedule(0 seconds, 5 seconds, testActor, "tick")
}
}
object MyActor{
def props = Props[MyActor]
}
class MyActor extends Actor{
import MyActor._
override def receive: Receive = {
case "tick" => println("execute!")
case _ => println("ups?")
}
}
示例11: EmailUtil
//设置package包名称以及导入依赖的类
package utils
import akka.actor.Props
import akka.routing.RoundRobinPool
import org.apache.commons.mail.{DefaultAuthenticator, Email}
import play.api.Play
import play.api.libs.concurrent.Akka
import play.api.Play.current
package object support {
object EmailUtil {
val smtpHost = Play.application.configuration.getString("smtp.host").getOrElse("mail.m8chat.com")
val smtpPort = Play.application.configuration.getInt("smtp.port").getOrElse(587)
val smtpUser = Play.application.configuration.getString("smtp.user").getOrElse("[email protected]")
val smtpPassword = Play.application.configuration.getString("smtp.password").getOrElse("64PscSUvTHRV8wB3")
val smtpStartTls = Play.application.configuration.getBoolean("smtp.startTls").getOrElse(true)
val smtpDebug = Play.application.configuration.getBoolean("smtp.debug").getOrElse(false)
val noReplySender = "[email protected]"
def send(mail: Email) = {
mail.setHostName(EmailUtil.smtpHost)
mail.setSmtpPort(EmailUtil.smtpPort)
mail.setAuthenticator(new DefaultAuthenticator(EmailUtil.smtpUser, EmailUtil.smtpPassword))
mail.setSSLCheckServerIdentity(false)
mail.setStartTLSRequired(EmailUtil.smtpStartTls)
mail.setDebug(EmailUtil.smtpDebug)
mail.send()
}
}
val ContactUsEmailRouter = Akka.system.actorOf(RoundRobinPool(3).props(Props[ContactUsEmailSender]), "support.ContactUsEmailRouter")
val AdminEmailSender = Akka.system.actorOf(Props[AdminEmailSender], "support.AdminEmailSender")
}
示例12: MazeGlobal
//设置package包名称以及导入依赖的类
package global
import actors.MazeActor
import akka.actor.Props
import play.api.libs.concurrent.Akka
import play.api.{Application, GlobalSettings, Logger}
import play.api.Play.current
object MazeGlobal extends GlobalSettings {
lazy val actor = Akka.system.actorOf(Props[MazeActor], "MazeActor")
override def onStart(app: Application): Unit = {
super.onStart(app)
Logger.info("App started")
}
override def onStop(app: Application): Unit = {
super.onStop(app)
Logger.info("App Stopped")
Akka.system.shutdown()
}
}
示例13: AppModule
//设置package包名称以及导入依赖的类
package conf
import java.time.{Clock, ZoneId}
import akka.actor.{Actor, ActorRef, Props}
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier
import com.google.api.client.http.javanet.NetHttpTransport
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.client.util.store.AbstractDataStoreFactory
import com.google.inject.AbstractModule
import com.google.inject.name.Names
import com.google.inject.util.Providers
import controllers.AppController
import play.api.libs.concurrent.{Akka, AkkaGuiceSupport}
import services._
import services.support.SystemClock
import scala.reflect.ClassTag
class AppModule extends AbstractModule {
def configure = {
bind(classOf[AppController]).asEagerSingleton // in prod mode all dependencies of AppController get built at startup time
bind(classOf[AbstractDataStoreFactory]).to(classOf[RepositoryDataStoreFactory])
bind(classOf[GoogleIdTokenVerifier]).toInstance(new GoogleIdTokenVerifier.Builder(new NetHttpTransport, new JacksonFactory).build)
bind(classOf[Clock]).toInstance(new SystemClock(ZoneId.systemDefault()))
}
}
class AkkaModule extends AbstractModule with AkkaGuiceSupport {
def configure = {
bindActorNoEager[SuperSupervisorActor](SuperSupervisorActor.actorName)
bindActorFactory[UserSupervisorActor, UserSupervisorActor.Factory]
}
private def bindActorNoEager[T <: Actor : ClassTag](name: String, props: Props => Props = identity): Unit = {
binder.bind(classOf[ActorRef])
.annotatedWith(Names.named(name))
.toProvider(Providers.guicify(Akka.providerOf[T](name, props)))
}
}
示例14: TFLInsertPolyLineDefinition
//设置package包名称以及导入依赖的类
package database.tfl
import akka.actor.{Actor, ActorRef, Props}
import com.mongodb.casbah.commons.MongoDBObject
import database.{POLYLINE_INDEX_DOCUMENT, POLYLINE_INDEX_COLLECTION, DatabaseCollections, DatabaseInsert}
import com.mongodb.casbah.Imports._
import play.api.Logger
import play.api.libs.concurrent.Akka
import play.api.Play.current
object TFLInsertPolyLineDefinition extends DatabaseInsert{
override protected val collection: DatabaseCollections = POLYLINE_INDEX_COLLECTION
override val supervisor: ActorRef = Akka.system.actorOf(Props[TFLInsertPolyLineDefinitionSupervisor], "TFLUpdatePolyLineSupervisor")
}
class TFLInsertPolyLineDefinitionSupervisor extends Actor {
val dbTransactionActor: ActorRef = context.actorOf(Props[TFLInsertPolyLineDefinition], name = "TFLUpdatePolyLineActor")
override def receive: Actor.Receive = {
case doc1: POLYLINE_INDEX_DOCUMENT => dbTransactionActor ! doc1
}
}
class TFLInsertPolyLineDefinition extends Actor {
val collection = POLYLINE_INDEX_COLLECTION
override def receive: Receive = {
case doc1: POLYLINE_INDEX_DOCUMENT => insertToDB(doc1)
case _ =>
Logger.error("TFL PolyLine Definition Actor received unknown message")
throw new IllegalStateException("TFL PolyLine Definition Actor received unknown message")
}
private def insertToDB(doc: POLYLINE_INDEX_DOCUMENT) = {
TFLInsertPolyLineDefinition.numberDBTransactionsRequested += 1
val query = MongoDBObject(
collection.FROM_STOP_CODE -> doc.fromStopCode,
collection.TO_STOP_CODE -> doc.toStopCode)
val update = $set(collection.POLYLINE -> doc.polyLine)
TFLInsertPolyLineDefinition.dBCollection.update(query, update, upsert = true)
TFLInsertPolyLineDefinition.numberDBTransactionsExecuted += 1
}
}
开发者ID:chrischivers,项目名称:London-Bus-Tracker-Play-Framework,代码行数:51,代码来源:TFLInsertPolyLineDefinition.scala
示例15: BMotticus
//设置package包名称以及导入依赖的类
package plugins
import com.typesafe.scalalogging.StrictLogging
import play.api.libs.concurrent.{Akka, Execution}
import play.api.Application
import modules._
import play.api.libs.ws._
import javax.inject.{Inject,Singleton}
class BMotticus @Inject() (implicit val app: Application) extends BMPlugin with Context with StrictLogging{
lazy val config = app.configuration
implicit def ctx: modules.Context = this
implicit def actorSystem = Akka.system
lazy val oAuthConfigs = new OAuthConfiguration(config.getObject("google.client_id.json.web").get)
lazy val usersM = new modules.UsersModule(ctx)
lazy val accountsM = new modules.AccountsModule(ctx)
lazy val storeM = new modules.StoreModule(ctx)
lazy val userM = new modules.UserModule(ctx)
lazy val googleAuth = new modules.GoogleOAuthClient(
WS.client(app),
oAuthConfigs,
config.getString("google.api_key").get
)
println(config.getObject("google.client_id.json"))
override def onStart() = {
logger.info("BMotticus application started using " + Thread.currentThread.getName)
super.onStart()
}
override def onStop () = {
logger.info("Application Shutting down")
super.onStop()
}
}
import play.api.Play.current
trait BMotticusContext {
def bm: plugins.BMotticus = current.plugin[BMotticus].getOrElse(throw new RuntimeException("Plugin Failed to load"))
}
object BMotticusContext extends BMotticusContext