Scala,Akka:特征匹配中对象的模式匹配

编程入门 行业动态 更新时间:2024-10-15 14:14:54
本文介绍了Scala,Akka:特征匹配中对象的模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

美好的一天。我正在制作一个简单的程序,该程序检查服务器的某些状态,并面临模式匹配的问题。 这是代码:入口点:

Good day. I'm making a simple program which check's some server state and faced the issue with pattern matching. Here is the code: Entry point:

object Run extends App with StateActor.Api{ private implicit val system = ActorSystem() implicit val blockingDispatcher: MessageDispatcher = system.dispatchers.lookup("blocking-dispatcher") protected val log: LoggingAdapter = Logging(system, getClass) protected implicit val materializer: ActorMaterializer = ActorMaterializer() import scala.concurrent.duration._ implicit val timeout = Timeout(17 seconds) val listener = system.actorOf(StateActor.props) system.scheduler.schedule( 0 milliseconds, 5 minutes, listener, Ping ) }

演员:

class StateActor(implicit val blockingDispatcher: MessageDispatcher) extends Actor with StateActor.Api with ActorLogging { import akka.pattern.pipe private val formatter = JSONFormat.defaultFormatter private val mHookUrl = ... var mState: State = UNDEFINED override def receive: Receive = { case Ping => log.debug("Ping") Future(Http("...").timeout(15000, 15000).asString) .map { case HttpResponse(_, 200, _) => UpResponse case HttpResponse(body, code, _) => DownResponse(s"Code: $code, body:\n $body") case rest => DownResponse(s"Undefined object: ${rest.toString}") } recover { case e => DownResponse(e.getMessage) } pipeTo self case UpResponse => if (mState == DOWN || mState == UNDEFINED) { mState == UP reportToSlack("Client Up") } case DownResponse(reason) => if (mState == UP || mState == UNDEFINED) { mState == DOWN reportToSlack(s"Client DOWN!\n Reason: $reason") } case other => println(other) println(other.getClass) } def reportToSlack(message: String): Unit = { ... } } object StateActor { trait Api { case object Ping sealed trait State case object UP extends State case object DOWN extends State case object UNDEFINED extends State sealed trait StateMessage case object UpResponse extends StateMessage case class DownResponse(reason: String) extends StateMessage } def props(implicit blockingDispatcher: MessageDispatcher) = Props(new StateActor()) }

如您所见,我将所有消息和其他内容放入 StateActor伴随对象内的特征 API中。但是,当调度程序将 Ping发送给演员时,它会匹配其他情况,而不是 case Ping。只需将案例对象Ping从特征和伴随对象中移出并使其成为独立对象,就可以解决问题。像这样:

As you can see, I put all messages and other stuff intoto trait "API" inside "StateActor" companion object. But when scheduler sends "Ping" to actor, it matches 'case other', not 'case Ping'. Problem can be solved just by moving 'case object Ping' out from trait and companion object and making it 'stand alone' object. Like this:

case object Ping object StateActor { trait Api { ... } ... }

但是为什么呢内在特质时不起作用?

But why it doesn't work when it's inside trait? All other case classes and objects in trait pattern match just fine.

推荐答案

运行和 StateActor 都分别扩展了特征,因此它们各自具有自己的 Ping 对象,并且不应匹配。其他消息匹配的唯一原因是因为 StateActor 正在将它们发送给自己!甚至不能与两个不同的 StateActor 实例一起使用。

Run and StateActor both extend the trait separately, so each has its own Ping object and they shouldn't match. The only reason other messages match is because the StateActor is sending them to itself! It wouldn't even work with two different StateActor instances.

而不是

将案例对象Ping从特征和伴侣对象中移出

moving 'case object Ping' out from trait and companion object

您应该将 Api 设置为对象,并通过导入来使消息可访问: import StateActor.Api ._ 而不是扩展StateActor.Api (或直接将它们放入 Object StateActor )

you should make Api an object and make the messages accessible by importing them: import StateActor.Api._ instead of extends StateActor.Api (or put them directly into object StateActor).

更多推荐

Scala,Akka:特征匹配中对象的模式匹配

本文发布于:2023-11-25 04:57:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:特征   对象   模式   Scala   Akka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!