关于本文
- 版本:2.6之后的版本为收费版本,2.6版本的scala版使用文档见v2.6使用文档,flink在用的也是这个版本
- api类型:classic比较灵活,试用一下classic版本的api
构建应用Demo
配置如下的pom文件
pom.xml >folded1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <?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>tech.bravoqq</groupId> <artifactId>demo-akka</artifactId> <version>1.0-SNAPSHOT</version>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <scala.version>2.12.19</scala.version> <scala.base.version>2.12</scala.base.version> <akka.version>2.6.21</akka.version> </properties>
<dependencies> <dependency> <groupId>com.typesafe.akka</groupId> <artifactId>akka-actor_${scala.base.version}</artifactId> <version>${akka.version}</version> </dependency> </dependencies>
<build> <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>4.9.2</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>${scala.version}</scalaVersion> </configuration> </plugin> </plugins> </build> </project>
|
官方demo给到的是一个ping-pong的系统,如下,
Application.scala1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import akka.actor.{Actor, ActorRef, ActorSystem, PoisonPill, Props} import language.postfixOps import scala.concurrent.duration._
case object Ping
case object Pong
class Pinger extends Actor { var countDown = 100
def receive = { case Pong => println(s"${self.path} received pong, count down $countDown")
if (countDown > 0) { countDown -= 1 sender() ! Ping } else { sender() ! PoisonPill self ! PoisonPill } } }
class Ponger(pinger: ActorRef) extends Actor { def receive = { case Ping => println(s"${self.path} received ping") pinger ! Pong } }
object Application extends App { val system = ActorSystem("pingpong")
val pinger = system.actorOf(Props[Pinger](), "pinger")
val ponger = system.actorOf(Props(classOf[Ponger], pinger), "ponger")
import system.dispatcher
system.scheduler.scheduleOnce(500 millis) { ponger ! Ping } }
|