future

Akka Futures Exceptions

最后都变了- 提交于 2019-12-03 04:23:25
What happens when an actor of a future throws an exception? According to the Akka documentation at http://doc.akka.io/docs/akka/snapshot/scala/futures.html : It doesn't matter if an Actor or the dispatcher is completing the Future, if an Exception is caught the Future will contain it instead of a valid result. If a Future does contain an Exception, calling Await.result will cause it to be thrown again so it can be handled properly. I am not sure this is what I am seeing when running this piece of code: class Worker extends Actor { def receive = { case i: Int => throw new RuntimeException } }

Are Futures executed on a single thread? (Scala)

余生长醉 提交于 2019-12-03 03:49:32
Using the default implicit execution context in Scala, will each new future be computed on a single, dedicated thread or will the computation be divided up and distributed to multiple threads in the thread pool? I don't know if this helps, the background to this question is that I want to perform multiple concurrent operations using the HtmlUnit API. To do this, I would wrap each new WebClient instance in a Future. The only problem is that the WebClient class is not thread safe, so I'm worried that it might broken up and sent to different threads. dk14 One future is executed on a single thread

How to combine Futures of different types into a single Future without using zip()

喜欢而已 提交于 2019-12-03 03:37:54
问题 I want to create a Future of type Future[(Class1,Class2,Class3)] from below code. However the only way I have found to do this is by using zip(). I find the solution ugly and properly not optimal. Can anybody enlightened me. val v = for ( a <- { val f0:Future[Class1] = process1 val f1:Future[Class2] = process2 val f2:Future[Class3] = process3 f0.zip(f1).zip(f2).map(x => (x._1._1,x._1._2,x._2)) } yield a // Future[(Class1,Class2,Class3)] I have also tried to use Future.sequence(List(f0, f1, f2

Using Futures in Akka Actors

China☆狼群 提交于 2019-12-03 03:15:31
I'm just starting to learn Akka Actors in Scala. My understanding is that messages received by an Actor are queued in an Actor's mailbox, and processed one at a time. By processing messages one at a time, concurrency issues (race conditions, deadlocks) are mitigated. But what happens if the Actor creates a future to do the work associated with a message? Since the future is async, the Actor could begin processing the next several messages while the future associated with the prior message is still running. Wouldn't this potentially create race conditions? How can one safely use futures within

ability to get the progress on a Future<T> object

泪湿孤枕 提交于 2019-12-03 02:08:14
With reference to the java.util.concurrent package and the Future interface I notice (unless I am mistaken) that the ability to start a lengthy tasks and be able to query on the progress only comes with the SwingWorker implementing class. This begs the following question: Is there a way, in a non-GUI, non-Swing application (imaging a console application) to start a lengthy task in the background and allow the other threads to inspect the progress ? It seems to me that there is no reason why this capability should be limited to swing / GUI applications. Otherwise, the only available option, the

Using futures and Thread.sleep

泪湿孤枕 提交于 2019-12-03 02:00:52
By executing this scala code, I don't have any output in the console. (I don't really understand what is happening) If I remove Console.println("Console.println OK!") => everything seems fine. If I remove Thread.sleep(2000) => everything seems fine. Do you have any ideas about this ? Thank you very much! Clément import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration._ import scala.concurrent.{Await, Future} import scala.language.postfixOps object ScalaFuture { def main(args: Array[String]) { val f: Future[String] = Future { Thread.sleep(2000) "future value"

Akka Actor ask and Type Safety

爱⌒轻易说出口 提交于 2019-12-03 01:45:15
How can I use Akka Actor ask and maintain type safety? or avoid using ask in favour of tells? When calling ? or ask on an Akka Actor, a Future[Any] is returned and I have to do an explicit cast via future.mapTo[MyType] . I don't like losing this type safety. If I use Futures directly (with no actors) I can explicitly return Future[MyType] and maintain type safety. My specific use case involves an actor delegating it's message to two child actors and then aggregating the results from those actors and returning that to the parent's sender. My parent's receive method looks similar to this

ScheduledExecutorService调度线程池运行几次后停止某一个线程

匿名 (未验证) 提交于 2019-12-02 23:59:01
开发中偶尔会碰到一些轮询需求,比如我碰到的和银行对接,在做完某一个业务后银行没有同步给到结果,这时候就需要查询返回结果,我们的需求是5分钟一次,查询3次,3次过后如果没有结果则T+1等银行的文件,对于这种任务我们的要求是轮询不是很严格,所以我采取调度线程池方式,如果有查询任务,加入线程池,设置好执行次数及执行时间间隔,具体代码如下: 1 import org.junit.Test; 2 import org.slf4j.Logger; 3 import org.slf4j.LoggerFactory; 4 5 import java.util.concurrent.*; 6 7 8 public class LocalTest { 9 10 private static final Logger log = LoggerFactory.getLogger(LocalTest.class); 11 12 @Test 13 public void test01() throws InterruptedException { 14 ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); 15 ConcurrentHashMap<String, Future> futureMap =

Run multiple futures in parallel, return default value on timeout

十年热恋 提交于 2019-12-02 23:11:29
I have to run multiple futures in parallel and the program shouldn't crash or hang. For now I wait on futures one by one, and use fallback value if there is TimeoutException. val future1 = // start future1 val future2 = // start future2 val future3 = // start future3 // <- at this point all 3 futures are running // waits for maximum of timeout1 seconds val res1 = toFallback(future1, timeout1, Map[String, Int]()) // .. timeout2 seconds val res2 = toFallback(future2, timeout2, List[Int]()) // ... timeout3 seconds val res3 = toFallback(future3, timeout3, Map[String, BigInt]()) def toFallback[T](f

AppBoxFuture(һ). Hello Future!

匿名 (未验证) 提交于 2019-12-02 23:05:13
极简的分布式系统架构 根据需要可单节点或集群部署,如下图所示: 模型驱动开发(MDD): 框架将应用系统所涉及的数据结构、业务逻辑、用户界面、工作流、报表、权限等抽象为各类型的模型,通过组合模型以形成完整的应用系统,通过调整模型以适应业务的需求变更。由于模型具备抽象性开发人员可以快速根据业务需求构建相应的模型;由于模型具备规范性约束,可以有效的保障不同技能的开发人员开发出高质量的系统;由于模型具备关联性约束,可以有效的保障调整模型时不引入新的缺陷。 集成强一致性数据存储: 框架基于Raft及RocksDB实现了分布式强一致性的存储引擎,由于分布式存储天生的特性,随着数据量的不断增长可以通过低成本的方式横向扩展(Scaleout)节点,并且在集群的多数派节点存活的条件下可保障系统连续不间断运行。 目前框架还在开发过程中,现就已完成的部分做一个技术预览,让我们开始体验吧: 安装与启动: 注意:目前仅支持Ubuntu18.04 安装依赖包 $ > sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF echo "deb https://download.mono-project.com/repo/ubuntu