Scala 参与者无法识别消息
我正试图更熟悉akka。目前,我正在以一种非常简单的方式发送消息(下一步是发送Scala 参与者无法识别消息,scala,akka,Scala,Akka,我正试图更熟悉akka。目前,我正在以一种非常简单的方式发送消息(下一步是发送ActorRef)。但当我试图发送带有消息的数据时,我的目标参与者无法识别它 以下是我编写的代码: import akka.actor.Actor import akka.actor.Props import akka.pattern.gracefulStop import scala.concurrent.duration._ import akka.util._ import scala.concurrent._
ActorRef
)。但当我试图发送带有消息的数据时,我的目标参与者无法识别它
以下是我编写的代码:
import akka.actor.Actor
import akka.actor.Props
import akka.pattern.gracefulStop
import scala.concurrent.duration._
import akka.util._
import scala.concurrent._
import java.util.concurrent._
import scala.Array._
import akka.actor.ScalaActorRef//for ActorRef
import scala.concurrent.ExecutionContext.Implicits.global
sealed trait MSG
case class msgTest(
content: Int
) extends MSG
class a extends Actor {
def receive = {
case "start" =>
println("emit")
case _ =>
println("wut?")
case msgTest(content) =>
println("content: ?")
}
}
object main extends App {
val system = akka.actor.ActorSystem("mySystem")
val a = system.actorOf(Props[a], name = "a")
a ! "start"
a ! msgTest(80085)
a ! "start"
}
这是我的输出:
[success] Total time: 6 s, completed Aug 12, 2014 3:39:35 PM
> run
[info] Running main
emit
wut?
emit
顺序在参数匹配中很重要,默认匹配出现在您发送的参与者消息之前:
def receive = {
case "start" =>
println("emit")
case _ => // <-- here
println("wut?")
case msgTest(content) =>
println("content: ?")
}
def接收={
案例“开始”=>
println(“发射”)
案例=>//
println(“内容:?”)
}
应该是:
def receive = {
case "start" =>
println("emit")
case msgTest(content) =>
println("content: ?")
case _ => // <-- now execute only when every other match fails
println("wut?")
}
def接收={
案例“开始”=>
println(“发射”)
案例msgTest(内容)=>
println(“内容:?”)
case=>//简单回答…XD谢谢。遵循约定,以大写字母开始类。模式匹配要求开发人员遵循约定。否则,您可能会遇到奇怪的错误。