Akka学习笔记:ActorSystem(调度)

10 篇文章 0 订阅

调度

  正如你在ActorSystem中的API看到的,如下:

1 //Light-weight scheduler for running asynchronous tasks after some deadline in the future.
2 def   scheduler : Scheduler

  在 ActorSystem 中有大量的方法调用scheduler,而scheduler返回的是Scheduler。Scheduler中有大量的schedule方法,利用他们我们可以在Actor环境下做大量的有趣的事情。

A、SCHEDULE SOMETHING TO EXECUTE ONCE


如果想及时了解 Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号: iteblog_hadoop

  在我们的Student-Teacher例子里面,假如在我们的测试用例程序中StudentActor想在接收到InitSignal消息后的5秒中之后才发送消息给Teacher。我们的代码看起来像这样的:

01 class StudentDelayedActor (teacherActorRef:ActorRef) extends Actor with ActorLogging {
02  
03   def receive = {
04     case InitSignal=> {
05       import context.dispatcher
06       context.system.scheduler.scheduleOnce(5 seconds, teacherActorRef, QuoteRequest)
07       //teacherActorRef!QuoteRequest
08     }
09     ...
10     ...
11   }
12 }

  1、测试用例
  我们来写个测试用例来测试这个:

01 "A delayed student" must {
02  
03    "fire the QuoteRequest after 5 seconds when an InitSignal is sent to it" in {
04  
05      import me.rerun.akkanotes.messaging.protocols.StudentProtocol._
06  
07      val teacherRef = system.actorOf(Props[TeacherActor], "teacherActorDelayed")
08      val studentRef = system.actorOf(Props(new StudentDelayedActor(teacherRef)),
09                                                           "studentDelayedActor")
10  
11      EventFilter.info (start="Printing from Student Actor", occurrences=1).intercept{
12           studentRef!InitSignal
13      }
14    }
15 }

  2、将Eventfilter interception的超时时间增大
EventFilter在等待消息在 EventStream 中出现的默认的超时时间是3秒。我们将它增加到7秒来测试我们的程序, 我们可以通过filter-leeway 配置属性实现。

01 class RequestResponseTest extends TestKit(ActorSystem("TestUniversityMessageSystem",
02                                   ConfigFactory.parseString(""
03                                             akka{
04                                               loggers = ["akka.testkit.TestEventListener"]
05                                               test{
06                                                   filter-leeway = 7s
07                                               }
08                                             }
09                                     """)))
10   with WordSpecLike
11   with MustMatchers
12   with BeforeAndAfterAll
13   with ImplicitSender {
14   ...
15   ...

B. SCHEDULE SOMETHING TO EXECUTE REPEATEDLY

  为了能够重复地运行任务,你可以用Scheduler的schedule 方法。最常用的schedule方法将定期地给Actor发送消息,它接收四个参数:
  1、在第一次运行的时候需要等待多少时间;
  2、子序列循序的频率;
  3、我们想发送消息的目标ActorRef ;
  4、消息

1 case InitSignal=> { 
2       import context.dispatcher
3       context.system.scheduler.schedule(0 seconds, 5 seconds, teacherActorRef, QuoteRequest)
4       //teacherActorRef!QuoteRequest
5     }

  琐事
  在这里引入import context.dispatcher非常重要。schedule方法需要一个很重要的隐形参数ExecutionContext,查看schedule 方法的实现就知道原因很明显

01 final def schedule( 
02     initialDelay: FiniteDuration,
03     interval: FiniteDuration,
04     receiver: ActorRef,
05     message: Any)(implicit executor: ExecutionContext,
06                   sender: ActorRef = Actor.noSender): Cancellable =
07     schedule(initialDelay, interval, new Runnable {
08       def run = {
09         receiver ! message
10         if (receiver.isTerminated)
11           throw new SchedulerException("timer active for terminated actor")
12       }
13     })

schedule 方法仅仅在Runnable中包装了tell,而它最后被我们传进来的ExecutionContext所调用。为了使得ExecutionContext 在这个范围内隐式可用,我们利用了上下文中可用的隐式dispatcher。可以从 ActorCell.scala (Context)代码里面看到

1 /**
2    * Returns the dispatcher (MessageDispatcher) that is used for this Actor.
3    * Importing this member will place an implicit ExecutionContext in scope.
4    */
5   implicit def dispatcher: ExecutionContextExecutor
本博客文章除特别声明,全部都是原创!
尊重原创,转载请注明: 转载自过往记忆(http://www.iteblog.com/)
本文链接: 【Akka学习笔记:ActorSystem(调度)】(http://www.iteblog.com/archives/1166)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值