2019独角兽企业重金招聘Python工程师标准>>>
Akka 配置Dispatcher(一)
akka默认加载classpath下application.conf配置文件,在application.conf配置文件中配置Dispatcher
如下定义一个application.conf配置文件,
akka {loglevel = INFO
}my-dispatcher {# Dispatcher is the name of the event-based dispatchertype = Dispatcher# What kind of ExecutionService to useexecutor = "fork-join-executor"# Configuration for the fork join poolfork-join-executor {# Min number of threads to cap factor-based parallelism number toparallelism-min = 2# Parallelism (threads) . . . ceil(available processors * factor)parallelism-factor = 2.0# Max number of threads to cap factor-based parallelism number toparallelism-max = 10}# 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 = 100
}
如何使用这个Dispatcher的配置?
首先定义一个Actor,如下,
package com.usoft6;import akka.actor.UntypedActor;/*** Created by liyanxin on 2015/1/12.*/
public class MyActor extends UntypedActor {private int x;private int y;public MyActor(int x, int y) {this.x = x;this.y = y;}@Overridepublic void onReceive(Object message) throws Exception {System.out.println("接收到的消息=" + message);int result = x + y;this.getSender().tell(result, this.getSelf());this.getContext().stop(this.getSelf());}
}
DispatcherDemo1.java
package com.usoft6;import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.pattern.Patterns;
import akka.util.Timeout;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;/*** Created by liyanxin on 2015/1/13.*/
public class DispatcherDemo1 {public static void main(String args[]) throws Exception {ActorSystem system = ActorSystem.create("myActorSystem");ActorRef myActor = system.actorOf(Props.create(MyActor.class, 54, 65).withDispatcher("my-dispatcher"), "myactor");Timeout timeout = new Timeout(Duration.create(5, "seconds"));Future<Object> future = Patterns.ask(myActor, "are you ready?", timeout);// This will cause the current thread to block and wait for the UntypedActor to ‘complete’// the Future with it’s reply.// 在这里会阻塞到 Await.result 方法上,但这会导致性能的损失。Integer result = (Integer) Await.result(future, timeout.duration());System.out.println(result);}
}
这块代码指定Dispatcher的配置,
ActorRef myActor = system.actorOf(Props.create(MyActor.class, 54, 65).withDispatcher("my-dispatcher"), "myactor");
=============END=============