Akka实现了Actor Model,前面的文章中具体介绍了Actor Model是什么以及它的好处,从这篇文章开始介绍Akka的使用以及实现。
下面以HelloWorld示例作为开端。
首先建立Maven工程,在pom文件中加入akka的依赖
<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>com.myakka</groupId>
<artifactId>akka-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>akka-helloworld</name>
<!-- akka依赖 -->
<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.10</artifactId>
<version>2.3.4</version>
</dependency>
</dependencies>
<!-- 打包脚本 -->
<build>
<plugins>
<!-- 依赖输出到allpackage -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/allpackage</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<!-- 工程输出到allpackage -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/allpackage</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
然后编写SayHello类,这个Actor主要在启动的时候向另一个Actor发送消息,之后接收那个Actor返回过来的消息。这里面所有的Actor均继承自UntypedActor,这个在之后会降到。
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.actor.ActorRef;
public class SayHello extends UntypedActor {
@Override
public void preStart() {
final ActorRef receive =
getContext().actorOf(Props.create(ReceiveHello.class), "ReceiveHello");
// 发送消息到receive
receive.tell(ReceiveHello.Msg.HELLO, getSelf());
}
@Override
public void onReceive(Object msg) {
if (msg == ReceiveHello.Msg.DONE) {
getContext().stop(getSelf());
} else {
unhandled(msg);
}
}
}
消息的发送是在preStart中调用的,而preStart表示Actor启动前所需要进行的操作。
接下来看一下和SayHello交互的Actor ReceiveHello,
import akka.actor.UntypedActor;
public class ReceiveHello extends UntypedActor {
public static enum Msg {
HELLO, DONE
}
@Override
public void onReceive(Object msg) {
if (msg == Msg.HELLO) {
System.out.println("Hello World!");
// 返回消息给sender,也就是SayHello
getSender().tell(Msg.DONE, getSelf());
} else {
unhandled(msg);
}
}
}
可以看到这个Actor在接收到消息的时候打印了Hello World!,之后发送消息DONE给SayHello。
进入工程目录,使用mvn package,会调用我们之前在pom中配置的几个插件,将打包好的工程jar和它所依赖的三个jar放置到同一个目录中,在目录target/allpackage中有四个jar包。
将下面的内容拷贝到某个bat文件中,放置到allpackage目录里,
@echo off
set patha=./akka-actor_2.10-2.3.4.jar
set pathb=./akka-helloworld-0.0.1-SNAPSHOT.jar
set pathc=./config-1.2.1.jar
set pathd=./scala-library-2.10.4.jar
echo %patha%;%pathb%;%pathc%;%pathd%
java -classpath %patha%;%pathb%;%pathc%;%pathd% akka.Main SayHello
这里的akka.Main就是Actor System的入口。这里暂时不做介绍。
运行就可以看到效果。