package com.zxl.akka
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
//说明
//1. 当我们继承 Actor 后,就是一个 Actor,核心方法 receive 方法重写
class SayHelloActor extends Actor{
//说明
//1. receive 方法,会被该 Actor 的 MailBox(实现了 Runnable 接口)调用
//2. 当该 Actor 的 MailBox 接收到消息,就会调用 receive
//3. type Receive = PartialFunction[Any, Unit]
override def receive:Receive = {
case "hello" => println("收到 hello, 回应 hello too:)")
case "ok" => println("收到 ok, 回应 ok too:)")
case "exit" => {
println("接收到 exit 指令,退出系统")
context.stop(self) //停止 actoref
context.system.terminate()//退出 actorsystem
}
case _ => println("匹配不到")
}
}
object SayHelloActorDemo {
//1. 先创建一个 ActorSystem, 专门用于创建 Actor
private val actoryFactory = ActorSystem("actoryFactory")
//2. 创建一个 Actor 的同时,返回 Actor 的 ActorRef
// 说明
//(1) Props[SayHelloActor] 创建了一个 SayHelloActor 实例,使用反射
//(2) "sayHelloActor" 给 actor 取名
//(3) sayHelloActorRef: ActorRef 就是 Props[SayHelloActor] 的 ActorRef
//(4) 创建的 SayHelloActor 实例被 ActorSystme 接管
private val sayHelloActorRef: ActorRef = actoryFactory.actorOf(Props[SayHelloActor],"sayHelloActor")
def main(args: Array[String]): Unit = {
//给 SayHelloActor 发消息(邮箱)
sayHelloActorRef ! "hello"
sayHelloActorRef ! "ok"
sayHelloActorRef ! "ok~"
//研究异步如何退出 ActorSystem
sayHelloActorRef ! "exit"
}
}
pom文件
<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>
<!-- 公司或者组织的唯一标志,并且配置时生成的路径也是由此生成, 如com.companyname.project-group,maven会将该项目打成的jar包放本地路径:/com/zxl/akka -->
<groupId>com.zxl.akka</groupId>
<!-- 项目的唯一ID,一个groupId下面可能多个项目,就是靠artifactId来区分的 -->
<artifactId>akka-hello</artifactId>
<!-- 版本号 -->
<version>1.0</version>
<properties>
<scala.version>2.11.8</scala.version>
<scala.compat.version>2.11</scala.compat.version>
<akka.version>2.4.17</akka.version>
<encoding>UTF-8</encoding>
</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>
<!-- 多进程之间akka通信 -->
<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>
<plugins>
<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-assembly-plugin打包成可执行的jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.zxl.akka.Hello
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>