通过一天的编码实验,我发现Akka的运行机制是这样的:
Akka运行时有一个默认的Dispatcher,该dispatcher定义了Akka 的actor所依靠的多线程服务。因为Akka的一个actor其实是在线程中运行的,如果按照默认的dispatcher配置,最多在线程池中只会有CPU Core * Factor个线程存在,如果你的程序中启动了超过这么多的Actor数量,那么, 由于同一个线程在同一时间只能运行一个actor,其他的actor就会被放入到队列中。好了,如果你的actor的处理消息是一个死循环,那么他就会一直占用该线程,其他Actor就不会得到执行机会。由于我们最近在做一个模拟多用户并行执行的分布式压力测试框架,虚拟用户的行为其实是重复的,如果采用Akka的默认配置,那么只有12个Actor会运行(CPU core = 4, Factor = 3),其他的就一直等待,这不是我们想要的。
其实Akka可以配置自定义的dispatcher。我们是Maven项目,在resource下面建立application.conf
my-thread-pool-dispatcher {
# Dispatcher is the name of the event-based dispatcher
type = Dispatcher
# What kind of ExecutionService to use
executor = "fork-join-executor"
# Configuration for the thread pool
fork-join-executor {
# Min number of threads to cap factor-based parallelism number to
parallelism-min = 2
# Parallelism (threads) ... ceil(available processors * factor)
parallelism-factor = 500
# Max number of threads to cap factor-based parallelism number to
parallelism-max = 1000
}
# Throughput defines the maximum number of messages to be
# processed per actor before the thread jumps to the next actor.
# Set to 1 for as fair as possible.
throughput = 1
}
上面定义了factor为500,这样我们最多就会有500 * 4 = 2000个线程,对于普通并发模拟已经足够了。
一个helloworld的示例。
HelloWorld.java
import akka.actor.*;
/**
* Created by niweimin on 2015/1/6.
*/
public class HelloWorld extends UntypedActor {
public int id;
public HelloWorld(final int id){
this.id = id;
}
public static Props props(int id){
return Props.create(HelloWorld.class, id);
}
@Override
public void preStart() throws InterruptedException {
System.out.println("Actor ID " + id + " says: I am created");
}
public void work(){
while(true) {
System.out.println("Actor ID " + id + " says: Hello world!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onReceive(Object msg) throws Exception {
if (((String)msg).equals(Message.DONE)) {
this.getContext().stop(this.getSelf());
}else if(((String)msg).equals(Message.WORK)){
this.work();
}
}
}
Latch.java
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
/**
* Created by niweimin on 2015/1/19.
*/
public class Latch extends UntypedActor {
ArrayList<ActorRef> actors;
long duration = 30;
Byte lock = 1;
public static Props props(){
return Props.create(Latch.class);
}
@Override
public void preStart() throws InterruptedException {
long startTime = System.currentTimeMillis();
ArrayList<ActorRef> list = new ArrayList<ActorRef>();
for(int i = 0; i < 1000; i++){
ActorRef greeter = getContext().actorOf(HelloWorld.props(i).withDispatcher("my-thread-pool-dispatcher") ,"Helloworld" + i);
list.add(greeter);
}
for (ActorRef ref : list) {
ref.tell(Message.WORK, this.getSelf());
}
while (true){
if (duration > 0 && (System.currentTimeMillis() - startTime) > (duration * 1000)) {
kill();
break;
}
}
}
private void kill() {
for(ActorRef ref : actors) {
ref.tell(Message.DONE, this.getSelf());
}
this.getContext().stop(this.getSelf());
System.exit(0);
}
@Override
public void onReceive(Object o) throws Exception {
}
}
Message.java
/**
* Created by niweimin on 2015/1/19.
*/
public class Message {
public static final String WORK = "work";
public static String DONE = "done";
}
Main.java
import akka.actor.*;
import akka.japi.Creator;
import java.util.ArrayList;
/**
* Created by niweimin on 2015/1/6.
*/
public class Main{
public static void main(String [] args){
new Main().run();
}
public void run(){
ActorSystem system = ActorSystem.apply();
ActorRef greeter = system.actorOf(Latch.props() ,"Latch");
}
}
在这个例子中,HelloWorld其实就是模拟的虚拟用户,在创建HelloWorld的Actor时,加入.withDispatcher("my-thread-pool-dispatcher")就可以了,这样每启动一个Actor,只要数目不超过配置里定义的2000,就会是一个actor一个线程。当然如果你不是想实现并行,只是想实现并发,那么可以自定义线程数目,实现线程复用,这时候Actor就有点像消息队列了。不得不说Akka还是相当灵活和强大的。