Akka 配置Dispatcher(一)

 2023-09-05 阅读 20 评论 0

摘要:2019独角兽企业重金招聘Python工程师标准>>> Akka 配置Dispatcher(一) akka默认加载classpath下application.conf配置文件,在application.conf配置文件中配置Dispatcher 如下定义一个application.conf配置文件, akka{loglevel=INFO

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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=============

转载于:https://my.oschina.net/xinxingegeya/blog/367343

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/1591.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息