【完整代码】Scala akka入门示例

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>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学亮编程手记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值