首页 > 代码库 > Akka 设置延迟执行的Future_Patterns. after
Akka 设置延迟执行的Future_Patterns. after
Akka 设置延迟执行的Future_Patterns. after
akka.pattern.Patterns.after makes it easy to complete a Future with a value or exception after a
timeout.
代码如下,
package com.usoft; import akka.actor.ActorSystem; import akka.dispatch.Futures; import akka.dispatch.OnComplete; import akka.pattern.Patterns; import scala.concurrent.ExecutionContext; import scala.concurrent.Future; import scala.concurrent.duration.Duration; import java.util.Arrays; import java.util.concurrent.Callable; /** * Created by liyanxin on 2015/1/9. */ public class PatternAfterDemo { public static void main(String args[]) { ActorSystem system = ActorSystem.create("myActorSystem"); final ExecutionContext ec = system.dispatcher(); final Future<String> failExc = Futures.failed(new IllegalStateException("OHNOES1")); Future<String> delayed = Patterns.after(Duration.create(200, "millis"), system.scheduler(), ec, failExc); Future<String> future = Futures.future(new Callable<String>() { public String call() throws InterruptedException { Thread.sleep(1000); return "foo"; } }, ec); Future<String> result = Futures.firstCompletedOf(Arrays.asList(future, delayed), ec); result.onComplete(new OnComplete<String>() { @Override public void onComplete(Throwable failure, String success) throws Throwable { if (failure != null) { failure.printStackTrace(); } else { System.out.println(success); } } }, ec); } }
==================================END==================================
Akka 设置延迟执行的Future_Patterns. after
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。