原文地址 http://rerun.me/2014/10/06/akka-notes-actorsystem-in-progress/

2. SCHEDULE

可以看到在ActorSystem的API有一个很有用的小方法叫scheduler,回返回一个Scheduler。Scheduler有很多 schedule方法让我们可以在Actor环境中做很多有趣的事情。

A. SCHEDULE SOMETHING TO EXECUTE ONCE


拿我们的学生-老师的例子来说,假设我们的学生StudentActor要在收到InitSignal后5秒钟才发消息给老师,代码需要这样写:

class StudentDelayedActor (teacherActorRef:ActorRef) extends Actor with ActorLogging {def receive = {case InitSignal=> {import context.dispatchercontext.system.scheduler.scheduleOnce(5 seconds, teacherActorRef, QuoteRequest)//teacherActorRef!QuoteRequest}......}
}

测试用例
让我们写个测试用例来校验一下:

"A delayed student" must {"fire the QuoteRequest after 5 seconds when an InitSignal is sent to it" in {import me.rerun.akkanotes.messaging.protocols.StudentProtocol._val teacherRef = system.actorOf(Props[TeacherActor], "teacherActorDelayed")val studentRef = system.actorOf(Props(new StudentDelayedActor(teacherRef)), "studentDelayedActor")EventFilter.info (start="Printing from Student Actor", occurrences=1).intercept{studentRef!InitSignal}}}

增加EventFilter的超时时间
EventFilter等消息在EventStream中出现的时间默认为3秒钟。现在让我们把他增加到7秒来验证我们的测试用例。filter-leeway的配置项能帮我们做到这个。

class RequestResponseTest extends TestKit(ActorSystem("TestUniversityMessageSystem", ConfigFactory.parseString("""  akka{loggers = ["akka.testkit.TestEventListener"]test{filter-leeway = 7s}}""")))  with WordSpecLikewith MustMatcherswith BeforeAndAfterAll with ImplicitSender {......

B. SCHEDULE SOMETHING TO EXECUTE REPEATEDLY

要想让东西能重复执行,你可以使用Scheduler的schedule方法。

一个经常被用来重载的schedule方法是用来发消息给Actor的方法。它接受4个参数:

第一次执行后要延迟多久开始初始化
执行子流程的频率
我们要发消息给哪个目标ActorRef
消息本身

case InitSignal=> {  import context.dispatchercontext.system.scheduler.schedule(0 seconds, 5 seconds, teacherActorRef, QuoteRequest)      //teacherActorRef!QuoteRequest}

琐事

import context.dispatcher在这里非常重要。

schedule方法需要一个非常重要的隐藏参数 - ExecutionContext,在我们看了 schedule方法的实现后就会明白为什么了:

final def schedule(  initialDelay: FiniteDuration,interval: FiniteDuration,receiver: ActorRef,message: Any)(implicit executor: ExecutionContext,sender: ActorRef = Actor.noSender): Cancellable =schedule(initialDelay, interval, new Runnable {      def run = {receiver ! message        if (receiver.isTerminated)          throw new SchedulerException("timer active for terminated actor")}})

schedule方法只是将tell包进了Runnable线程中,并且最终被我们传进的ExecutionContext执行。

为了让ExecutionContext在作用域中作为implicit(隐式类型),我们将上下文中的dispatcher声明为implicit。

在ActorCell(上下文)中

/*** Returns the dispatcher (MessageDispatcher) that is used for this Actor.* Importing this member will place an implicit ExecutionContext in scope.*/implicit def dispatcher: ExecutionContextExecutor

代码

跟往常一样,我们的代码可以在github 中下载到。
https://github.com/arunma/AkkaMessagingRequestResponse


文章来自微信平台「麦芽面包」
微信公众号「darkjune_think」
转载请注明。
如果觉得有趣,微信扫一扫关注公众号。

[翻译] AKKA笔记- ACTORSYSTEM (配置CONFIGURATION 与调度SCHEDULING) - 4(二)相关推荐

  1. [翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6

    原文:http://rerun.me/2014/10/21/akka-notes-child-actors-and-path/ Actor是完全的继承结构.你创建的任何Actor肯定都是一个其他Act ...

  2. [翻译]AKKA笔记 - DEATHWATCH -7

    当我们说Actor生命周期的时候,我们能看到Actor能被很多种方式停掉(用ActorSystem.stop或ActorContext.stop或发送一个PoisonPill - 也有一个kill和g ...

  3. 干货分享:高效办公工具【视频转文字、视频播放器、B站视频下载软件、贴图、截图提取文字并翻译、笔记记录软件、任务管理网站】

    高效办公工具分享--视频转文字.视频播放器.B站视频下载软件.贴图.截图提取文字并翻译.笔记记录软件.任务管理网站 一 前言 二.下载链接及效果 1.视频自动转文字-飞书妙记(目前免费使用!!) 2. ...

  4. 联邦学习笔记-《Federated Machine Learning: Concept and Applications》论文翻译个人笔记

    联邦学习笔记-<Federated Machine Learning: Concept and Applications>论文翻译个人笔记 摘要 今天的人工智能仍然面临着两大挑战.一是在大 ...

  5. A Survey of Deep Learning-based Object Detection论文翻译 + 阅读笔记

    A Survey of Deep Learning-based Object Detection论文翻译 + 阅读笔记 //2022.1.7 日下午16:00开始阅读 双阶段检测器示意图 单阶段检测器 ...

  6. 区块链分片:《Monoxide: Scale Out Blockchain with Asynchronous Consensus Zones》论文翻译个人笔记

    区块链分片:<Monoxide: Scale Out Blockchain with Asynchronous Consensus Zones>论文翻译个人笔记 日期:2019年2月26- ...

  7. 配置Configuration Manager站点和层次架构(1)

    一. 在 Configuration Manager 中配置发现 使用 Configuration Manager 管理的计算机和用户资源,还可发现环境中的网络基础结构.使用下列步骤在 System ...

  8. 022_配置configuration

    1. 配置(configuration)就是freemarker.template.Configuration对象, 它存储了常用(全局, 应用程序级)的设置, 定义了想要在所有模板中可用的变量(称为 ...

  9. oracle11g中用asmlib配置磁盘组,ASM学习笔记_配置ASMLIB磁盘组

    ASM学习笔记_配置ASMLIB磁盘组 目录 1 ASMLIB Introduction 2 虚拟机添加一个共享磁盘(块设备) 3 下载,安装ASMLIB 4 配置,使用ASMLib 磁盘组 #### ...

最新文章

  1. 创建型模式-工厂模式
  2. Keil μVision 5版新建工程详细步骤(版本2)
  3. 为什么构造函数不能声明为虚函数,析构函数可以,构造函数中为什么不能调用虚函数?
  4. Ceres Solver 非线性优化库
  5. 游戏筑基开发之初识指针
  6. Hi3518ev200使用HiTool下载程序
  7. 适配器模式(Adapter模式)
  8. 金蝶服务器换了无线网怎么办,搬家后wifi怎么重新设置?
  9. The Apache Way - 开源项目
  10. [笔记]Class.forName 时static代码的运行
  11. f49.in index.php,国家语言,语言代码,locale id对应表
  12. A. IQ test
  13. 【量化课堂】kd 树算法之详细篇 【1002 消化第一次ojbk】
  14. Java 判断一个点是否在一个三角形内
  15. Vivado驱动安装
  16. 从0开始学习微服务(二)
  17. 基于Python的DELMIA二次开发(一):创建零件
  18. [渝粤教育] 沈阳理工大学 复变函数与积分变换 参考 资料
  19. M1 Mac使用PD虚拟机配合VSC代替Keil烧录MM32单片机
  20. Electron 适配 Windows 7 32位操作系统

热门文章

  1. 小学生python入门-写给中小学老师们的Python入门指引
  2. python软件是免费的吗-7年程序员贡献出来的10大Python开源免费工具!
  3. python爬虫新手项目-33个Python爬虫项目实战(推荐)
  4. python处理多个excel文件-python多个excel文件合并成一个sheet
  5. UVa483 Word Scramble
  6. stl中的unique
  7. ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze 最短路+分层图
  8. 学习lulu之——tips 提示
  9. linux下rsync+inotify实现服务器之间文件实时同步
  10. 遇见那个对的人,便是爱情