当前位置: 首页>>代码示例>>Scala>>正文


Scala AskTimeoutException类代码示例

本文整理汇总了Scala中akka.pattern.AskTimeoutException的典型用法代码示例。如果您正苦于以下问题:Scala AskTimeoutException类的具体用法?Scala AskTimeoutException怎么用?Scala AskTimeoutException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了AskTimeoutException类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: Application

//设置package包名称以及导入依赖的类
package controllers

import akka.actor._
import akka.pattern.AskTimeoutException
import com.google.inject.name.Named
import com.google.inject.{Inject, Singleton}
import play.api._
import play.api.data.Form
import play.api.data.Forms._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json.Json
import play.api.mvc._
import scala.concurrent.Future
import scalaz.\/.left
import scalaz.{-\/, \/-}
import actors.Library
import actors.Library.{RequestBooks, BookData}
import entities.Book
import forms.SearchForm

@Singleton
class Application @Inject()(system: ActorSystem,
                            @Named("ndl-client-actor") ndlClient: ActorRef)
  extends Controller {
  import akka.pattern.ask
  import scala.concurrent.duration._

  def index = Action(Ok(views.html.index("Simple OPAC")))

  val bookForm = Form(
    mapping(
      "title"  -> optional(text),
      "author" -> optional(text),
      "any"    -> optional(text),
      "count"  -> optional(number(min = 1, max = 200))
    )(SearchForm.apply)(SearchForm.unapply)
  )

  implicit val bookToJson = Json.writes[Book]
  implicit val timeout: akka.util.Timeout = 1 minute
  lazy val libraryActor = system.actorOf(Library.props)

  def books = Action.async { implicit req =>
    bookForm.bindFromRequest.fold(
      formWithError => {
        Future.successful(BadRequest("invalid request"))
      },
      {
        case validForm => (try {
          libraryActor.ask(RequestBooks(validForm, ndlClient)).mapTo[BookData].map(_.books)
        } catch {
          case e: AskTimeoutException => Future.successful(left(s"Server Error: \n$e"))
          case _ => Future.successful(left("Something wrong..."))
        }).map {
          case \/-(books) => Ok(Json.toJson(books))
          case -\/(msg) => InternalServerError(msg)
        }
      }
    )
  }
} 
开发者ID:cedretaber,项目名称:simple-opac,代码行数:62,代码来源:Application.scala

示例2:

//设置package包名称以及导入依赖的类
package com.packt.chapter9

import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.ExceptionHandler
import akka.pattern.AskTimeoutException

trait RouteExceptionHandler {

  val routeExceptionHandler = ExceptionHandler {
    case _: ArithmeticException =>
      complete {
        StatusCodes.BadRequest -> "You values are incorrect. Probably b needs to be different from 0"
      }
    case _: AskTimeoutException =>
      complete {
        StatusCodes.ServiceUnavailable -> "Internal actor is not responding within 500 millis"
      }
  }
} 
开发者ID:PacktPublishing,项目名称:Akka-Cookbook,代码行数:21,代码来源:RouteExceptionHandler.scala

示例3: funcToRunnable

//设置package包名称以及导入依赖的类
package com

import akka.actor.ActorRef
import com.carjump.http.{ReqParams, CacheResponseBody}
import scala.reflect.ClassTag
import scala.util.control.NonFatal
import akka.pattern.AskTimeoutException

import scala.concurrent.{ExecutionContext, Future}

package object carjump {

  implicit def funcToRunnable(f: () ? Unit) = new Runnable {
    override def run() = f()
  }

  implicit def asFiniteDuration(d: java.time.Duration) =
    scala.concurrent.duration.Duration.fromNanos(d.toNanos)

  case class ValidationException(message: String) extends Exception(message)


  trait AskSupport {
    import akka.pattern.ask

    implicit def askTimeout: akka.util.Timeout

    def queryCache[T <: CacheResponseBody](message: ReqParams, cache: ActorRef)(implicit ec: ExecutionContext, tag: ClassTag[T]): Future[cats.data.Xor[String, T]] =
      cache.ask(message).mapTo[T].map { response =>
        response.error.fold(cats.data.Xor.right(response)) { error =>
          throw ValidationException(error)
        }
      }.recoverWith {
        case ex: ClassCastException ? Future.successful(cats.data.Xor.left(s"Could'n cast type: ${ex.getMessage}"))
        case ex: AskTimeoutException ? Future.successful(cats.data.Xor.left(s"Request timeout: ${ex.getMessage}"))
        case ex: ValidationException => Future.successful(cats.data.Xor.left(s"Validation error: ${ex.getMessage}"))
        case NonFatal(e) => //Doesn't swallow stack overflow and out of memory errors
          Future.successful(cats.data.Xor.left(s"Unexpected error: ${e.getMessage}"))
      }
  }
} 
开发者ID:haghard,项目名称:cache-demon,代码行数:42,代码来源:package.scala


注:本文中的akka.pattern.AskTimeoutException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。