gpt4 book ai didi

java - 当第 3 方库请求 ExecutorService 时使用 Scala ExecutionContext

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:02 27 4
gpt4 key购买 nike

我正在使用 Play Framework(Scala 版本)和 Amazon AWS Java SDK 将 Amazon S3 集成到应用程序中。

AWS SDK 有一个 TransferManager 类,它提供了一个抽象来管理线程池,用于处理到 S3 的下载/上传。

我正在尝试确定是否可以将 Play 对自定义 ExecutionContext 的核心支持集成到 SDK 提供的这个对象中。特别是,在实例化 AWS SDK 提供的 TransferManager 时,您可以提供自定义 ExecutorService 作为可选参数。

Scala 的 ExecutionClass 通过其类声明中的“with”关键字绑定(bind) ExecutorService 类,所以我想知道是否有某种机制可以从 ExecutionContext 获取 ExecutorService 对象,例如转换 ExecutionContext => ExecutorService 的方法。

如果不行,有没有其他办法?目前,我只是在此处概述的 Play 标准方法之外的类中直接实例化自定义 ExecutorService:

https://www.playframework.com/documentation/2.3.x/ThreadPools

这感觉很乱,并且违反了框架提供的约定。

感谢您的宝贵时间。

最佳答案

如果您像这样创建您的上下文(不要盲目地复制粘贴它 - 它被配置为阻止操作):

val blockingContext: ExecutionContext = {
val executor = new ThreadPoolExecutor(100, 100, 1, TimeUnit.MINUTES, new LinkedBlockingQueue(1000))
executor.allowCoreThreadTimeOut(true)
ExecutionContext.fromExecutorService(executor) // main part
}

然后你将能够从中获取ExecutorService实例:

val executor: ExecutorService = blockingContext.prepare().asInstanceOf[ExecutorService]

关于java - 当第 3 方库请求 ExecutorService 时使用 Scala ExecutionContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26005304/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com