美好的一天。我正在制作一个简单的程序,该程序检查服务器的某些状态,并面临模式匹配的问题。 这是代码:入口点:
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:特征匹配中对象的模式匹配
发布评论