简体   繁体   English

Scala Akka演员,消息确认

[英]Scala Akka actor, validation of messages

This is an Architecture/design form question as I have a nominally workable solution. 这是一个体系结构/设计形式的问题,因为我有一个名义上可行的解决方案。 I want to see if there is a better way... 我想看看是否有更好的方法...

I have an actor which handles a number of messages. 我有一个处理许多消息的演员。 Each message may have its own content structure validation. 每个消息可以具有其自己的内容结构验证。 I wish to know if there is a suitable pattern for (near seemless) message validation. 我想知道是否有一种合适的模式(几乎没有出现)验证消息。 For example, if I did 例如,如果我做了

val f:Future[Any] = actorRef ask MyMessage(invalidContent)

I could expect back: 我可以期待回来:

f = Future.failure(ValidationException(someMessage))

Currently I have achieved this by creating a wrapper which inherits 目前,我已经通过创建一个继承了

trait ValidatingActorRef[-T] {

  def actorRef:ActorRef

  def validate:PartialFunction[T, Option[ValidationException]]

  def ask(message:T)(implicit timeout:Timeout):Future[Any] =
    validate
      .lift(message)
      .flatten
      .map(t => Future.failed(t))
      .getOrElse(akka.pattern.ask(actorRef, message))
}

Giving me 给我

val myActorRefWrapper = new ValidatingActorRef[MyMessages] {  

  val actorRef = system.system.actorOf(Props[MyActor])

  val log = {

    case MyMessage(content) if content == badContent =>
      BadContentValidationException("you have bad content")
  }

} }

What I have got here is a validation response without having to waste time on the actor mailbox, or dealing with a future. 我在这里得到的是一个验证响应,而不必浪费时间在actor邮箱上或处理未来。 However the wrapper approach isnt very seemless, I have explicitly wire in a ValidatingActorRef, not just an ActorRef 但是包装器方法并不是非常好看,我明确地加入了ValidatingActorRef,而不仅仅是ActorRef

Is there a better pattern? 有没有更好的模式?

In my opinion it is better to validate message not on the sender side, but on actors side. 在我看来,最好不是在发送方,而是在参与者方来验证消息。

Just wrap your receive method with general validation logic and mixin some validation implementation. 只需使用常规验证逻辑包装您的receive方法,然后混合一些验证实现即可。

Also check presentation from ooyala guys - http://slideshare.net/EvanChan2/akka-inproductionpnw-scala2013 . 同样来自Ooyala的家伙支票颁赠- http://slideshare.net/EvanChan2/akka-inproductionpnw-scala2013 Look from 14th slide and find how they build stackable traits on actors 从第14张幻灯片中查看,并发现它们如何在演员上建立可堆叠的特征

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM