Scala中与阿卡最佳实践和第三方Java库 [英] Best practices with Akka in Scala and third-party Java libraries

查看:337
本文介绍了Scala中与阿卡最佳实践和第三方Java库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用的memcached的Java API 在我的斯卡拉/阿卡code。该API为您提供了同步和异步方法。异步的人返回<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html\">java.util.concurrent.Future.有一个问题在这里关于与Java期货斯卡拉处理这里的How我换一个java.util.concurrent.Future在阿卡未来?。然而,在我来说,我有两个选择:

I need to use memcached Java API in my Scala/Akka code. This API gives you both synchronous and asynchronous methods. The asynchronous ones return java.util.concurrent.Future. There was a question here about dealing with Java Futures in Scala here How do I wrap a java.util.concurrent.Future in an Akka Future?. However in my case I have two options:


  1. 使用同步API和包装堵在未来code和阻止标记:

  1. Using synchronous API and wrapping blocking code in future and mark blocking:

Future {
  blocking {
    cache.get(key) //synchronous blocking call
  } 
}


  • 使用异步的Java API并做检查轮询每n Java的未来毫秒如果未来完成(在上面的链接的问题在上面的答案一个像描述)。

  • Using asynchronous Java API and do polling every n ms on Java Future to check if the future completed (like described in one of the answers above in the linked question above).

    哪一个更好?我倾向于第一种选择,因为投票可以极大地影响响应时间。不应该阻塞全池堵塞{} 块prevent?

    Which one is better? I am leaning towards the first option because polling can dramatically impact response times. Shouldn't blocking { } block prevent from blocking the whole pool?

    推荐答案

    我总是第一个选项去。但我在一个稍微不同的方式做这件事。我不使用拦截功能。 (其实我还没想过呢。)相反,我提供一个包装同步阻塞调用自定义执行上下文未来。因此,它基本上是这样的:

    I always go with the first option. But i am doing it in a slightly different way. I don't use the blocking feature. (Actually i have not thought about it yet.) Instead i am providing a custom execution context to the Future that wraps the synchronous blocking call. So it looks basically like this:

    val ecForBlockingMemcachedStuff = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(100)) // whatever number you think is appropriate
    // i create a separate ec for each blocking client/resource/api i use
    
    Future {
        cache.get(key) //synchronous blocking call
    }(ecForBlockingMemcachedStuff) // or mark the execution context implicit. I like to mention it explicitly.
    

    因此​​,所有的阻塞调用将使用专用的执行上下文(=线程池)。因此,它是由负责非阻塞东西,你的主执行上下文分开。

    So all the blocking calls will use a dedicated execution context (= Threadpool). So it is separated from your main execution context responsible for non blocking stuff.

    这方法是在扮演在线培训视频还解释/阿卡通过类型安全的规定。有一个在第4课如何处理阻塞调用视频。它被解释的 Nilanjan Raychaudhuri 的(希望我正确的拼写),谁是斯卡拉书籍的知名作家。

    This approach is also explained in a online training video for Play/Akka provided by Typesafe. There is a video in lesson 4 about how to handle blocking calls. It is explained by Nilanjan Raychaudhuri (hope i spelled it correctly), who is a well known author for Scala books.

    更新:我有Nilanjan在twitter上一个讨论。他解释什么办法之间, 和一个自定义的区别阻塞的ExecutionContext 是。在拦截功能刚刚创建一个特殊的的ExecutionContext 。它提供了一个天真的做法的问题,你有多少线程需要。它每一次,当所有池中的其他现有的线程都很忙产生一个新的线程。因此,它实际上是一个不受控制的ExecutionContext。它可以创建大量线程,并导致像内存不足的错误的问题。因此,与自定义执行上下文的解决方案实际上是更好,因为它使这个问题明显。 Nilanjan还补充说,你需要考虑断路这个池获取与请求超载的情况。

    Update: I had a discussion with Nilanjan on twitter. He explained what the difference between the approach with blocking and a custom ExecutionContext is. The blocking feature just creates a special ExecutionContext. It provides a naive approach to the question how many threads you will need. It spawns a new thread every time, when all the other existing threads in the pool are busy. So it is actually an uncontrolled ExecutionContext. It could create lots of threads and lead to problems like an out of memory error. So the solution with the custom execution context is actually better, because it makes this problem obvious. Nilanjan also added that you need to consider circuit breaking for the case this pool gets overloaded with requests.

    TLDR:是啊,阻塞调用吸吮。 使用了阻塞调用自定义/专用的ExecutionContext。还要考虑断路。

    TLDR: Yeah, blocking calls suck. Use a custom/dedicated ExecutionContext for blocking calls. Also consider circuit breaking.

    这篇关于Scala中与阿卡最佳实践和第三方Java库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆