两个 Actor 的通讯机制原理图
目录结构:
代码:
AActor.scala
package com.akka.actors
import akka.actor.{Actor, ActorRef}
class AActor(actorRef: ActorRef) extends Actor {
val bActorRef: ActorRef = actorRef
override def receive: Receive = {
case "start" => {
println("AActor 出招了 , start ok")
self ! "我打" //发给自己
}
case "我打" => {
//给 BActor 发出消息
//这里需要持有 BActor 的引用(BActorRef)
println("AActor(黄飞鸿) 厉害 看我佛山无影脚")
Thread.sleep(1000)
bActorRef ! "我打" //给 BActor 发出消息
}
}
}
BActor.scala
package com.akka.actors
import akka.actor.Actor
class BActor extends Actor {
override def receive: Receive = {
case "我打" => {
println("BActor(乔峰) 挺猛 看我降龙十八掌")
Thread.sleep(1000)
//通过 sender() 可以获取到发现消息的 actor 的 ref
sender() ! "我打"
}
}
}
ActorGame.scala
package com.akka.actors
import akka.actor.{ActorRef, ActorSystem, Props}
/**
* 启动类
*/
//100 招后,就退出
object ActorGame extends App {
//创建 ActorSystem
val actorfactory = ActorSystem("actorfactory")
//先创建 BActor 引用/代理
val bActorRef: ActorRef = actorfactory.actorOf(Props[BActor], "bActor")
//创建 AActor 的引用
val aActorRef: ActorRef = actorfactory.actorOf(Props(new AActor(bActorRef)), "aActor")
//aActor 出招
aActorRef ! "start"
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>akkademo1</groupId>
<artifactId>akkademo1</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 定义一下常量 -->
<properties>
<encoding>UTF-8</encoding>
<scala.version>2.11.8</scala.version>
<scala.compat.version>2.11</scala.compat.version>
<akka.version>2.4.17</akka.version>
</properties>
<dependencies>
<!-- 添加scala的依赖 -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- 添加akka的actor依赖 -->
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_${scala.compat.version}</artifactId>
<version>${akka.version}</version>
</dependency>
<!-- 多进程之间的Actor通信 -->
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-remote_${scala.compat.version}</artifactId>
<version>${akka.version}</version>
</dependency>
</dependencies>
<!-- 指定插件-->
<build>
<!-- 指定源码包和测试包的位置 -->
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<!-- 指定编译scala的插件 -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<!--maven打包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>reference.conf</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!--<mainClass>test.MondoDBtoES</mainClass>-->
<mainClass>com.akka.actors.ActorGame</mainClass>
<!--<mainClass>AkkaServerApplication</mainClass>-->
<!--<mainClass>AkkaClientApplication</mainClass>-->
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
来源:oschina
链接:https://my.oschina.net/u/3730149/blog/4335945