阿卡:试图了解参与者之间的消息传递的顺序

时间:2019-01-31 18:39:19

标签: java scala kotlin concurrency akka

我想知道为什么消息的打印顺序与代码中的顺序完全相同。

import akka.actor.AbstractActor
import akka.actor.ActorRef
import akka.actor.ActorSystem
import akka.actor.Props
import akka.event.Logging
import akka.event.LoggingAdapter

data class Request(val name: String)

class Device : AbstractActor() {
    val log = Logging.getLogger(getContext().getSystem(), this);

    override fun createReceive(): Receive {
        return receiveBuilder().matchEquals("print") { x -> log.info("hello i'm a device") }
                .match(Request::class.java) { x -> log.info("A " + x.name) }
                .build()
    }

    companion object {
        fun props(): Props {
            return Props.create { Device() }
        }
    }
}


fun main(args: Array<String>) {
    val system = ActorSystem.create("container")
    val deviceA = system.actorOf(Device.props())
    val deviceC = system.actorOf(Device.props())
    val deviceD = system.actorOf(Device.props())


    val deviceB = system.actorOf(Device.props())

    deviceA.tell(Request("first "), deviceB)
    deviceA.tell(Request("second"), deviceC)
    deviceA.tell(Request("third"), deviceD)



}

它打印出:

/usr/lib/jvm/java-8-oracle/bin/java -Dvisualvm.id=32047598721041 -javaagent:/opt/intellij-idea-community/lib/idea_rt.jar=41737:/opt/intellij-idea-community/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/home/dell/akka-quickstart-java/target/classes:/home/dell/.m2/repository/com/typesafe/akka/akka-actor_2.12/2.5.19/akka-actor_2.12-2.5.19.jar:/home/dell/.m2/repository/org/scala-lang/scala-library/2.12.8/scala-library-2.12.8.jar:/home/dell/.m2/repository/com/typesafe/config/1.3.3/config-1.3.3.jar:/home/dell/.m2/repository/org/scala-lang/modules/scala-java8-compat_2.12/0.8.0/scala-java8-compat_2.12-0.8.0.jar:/home/dell/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.3.20/kotlin-stdlib-jdk8-1.3.20.jar:/home/dell/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib/1.3.20/kotlin-stdlib-1.3.20.jar:/home/dell/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-common/1.3.20/kotlin-stdlib-common-1.3.20.jar:/home/dell/.m2/repository/org/jetbrains/annotations/13.0/annotations-13.0.jar:/home/dell/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.3.20/kotlin-stdlib-jdk7-1.3.20.jar StuffKt
[INFO] [01/31/2019 18:43:16.058] [container-akka.actor.default-dispatcher-2] [akka://container/user/$a] A first 
[INFO] [01/31/2019 18:43:16.059] [container-akka.actor.default-dispatcher-2] [akka://container/user/$a] A second
[INFO] [01/31/2019 18:43:16.059] [container-akka.actor.default-dispatcher-2] [akka://container/user/$a] A third



我原本期望的顺序有时会与(“第一第二第三”)有所不同,但是每次运行时它都会打印出相同的顺序。

我的期望不正确吗?

1 个答案:

答案 0 :(得分:0)

documentation on message delivery order状态

  

更具体的规则是,对于给定的一对演员,   直接从第一个发送到第二个的消息将不会   收到故障。这个词直接强调   保证仅在与Tell操作员发送到   最终目的地,而不是雇用调解人或其他信息时   传播功能(除非另有说明)。

您正在从main方法(actor系统外部)发送到deviceA

不是是从deviceBdeviceCdeviceD演员发来的。这些只是用作发件人引用,因此deviceA具有someone to reply to