根据此链接中的程序: Java Akka's ActorRef async issues
是否有任何Akka本机函数可用作actor.tell
的调度函数?我想安排每个时间间隔自动“告诉”。
顺便说一句,我不选择Java的Executor,因为我不想浪费OS资源。
附上示例代码:
ActorSystem as1 = ActorSystem.create("actor1");
ActorRef ar1 = as1.actorOf(Props.create(Hello.class), "ar1");
ActorRef ar2 = as1.actorOf(Props.create(Hello.class), "ar2");
Router router = new Router(new RoundRobinRoutingLogic());
router = router.addRoutee(ar1);
router = router.addRoutee(ar2);
System.out.println("Start to say hello!");
router.route("Bob",ActorRef.noSender());
router.route("John",ActorRef.noSender());
System.out.println("Finish to say hello!”);
答案 0 :(得分:1)
这个section in the reference guide应该对此非常有帮助。 在演员中,您可以执行以下操作:
FiniteDuration delay = FiniteDuration.create(10, TimeUnit.MINUTES);
FiniteDuration initialDelay = FiniteDuration.create(2, TimeUnit.MINUTES);
getContext().system().scheduler().schedule(initialDelay, delay, self(), message, getContext().dispatcher(), ActorRef.noSender());
如果你有一个ActorRef ref
FiniteDuration delay = FiniteDuration.create(10, TimeUnit.MINUTES);
FiniteDuration initialDelay = FiniteDuration.create(2, TimeUnit.MINUTES);
actorSystem.scheduler().schedule(initialDelay, delay, self(), message, actorSystem.dispatcher(), ActorRef.noSender())
使用路由器:
ActorSystem as1 = ActorSystem.create("actor1");
ActorRef ar1 = as1.actorOf(Props.create(Hello.class), "ar1");
ActorRef ar2 = as1.actorOf(Props.create(Hello.class), "ar2");
List<String> routees = Arrays.asList(ar1, ar2);
ActorRef routerActorRef = as1.actorOf(new RoundRobinGroup(routees).props(), "router");
System.out.println("Start to say hello!");
FiniteDuration delay = FiniteDuration.create(10, TimeUnit.MINUTES);
FiniteDuration initialDelay = FiniteDuration.create(2, TimeUnit.MINUTES);
as1.scheduler().schedule(initialDelay, delay, routerActorRef, "Bob", as1.dispatcher(), ActorRef.noSender())
as1.scheduler().schedule(initialDelay, delay, routerActorRef, "John", as1.dispatcher(), ActorRef.noSender())
System.out.println("Finish to say hello!”);