Use Scala ExecutionContext when third party library requests ExecutorService

I am using Play Framework (Scala version) with Amazon AWS Java SDK to integrate Amazon S3 into an application.

The AWS SDK has a TransferManager class that provides an abstraction for managing a pool of threads to handle upload / download to S3.

I'm trying to determine if it is possible to integrate the basic support that Play has for custom ExecutionContexts into this SDK-provided object. In particular, when instantiating the TransferManager provided by the AWS SDK, you can provide a custom ExecutorService as an optional parameter.

The Scala ExecutionClass binds the ExecutorService class using the "c" keyword in its class declaration, so I'm wondering if there is some mechanism to get the ExecutorService object from the ExecutionContext like a method that converts ExecutionContext => ExecutorService.

If not, is there any other approach? At the moment I'm just instantiating the ExecutorService directly in the class outside of the standard Play approach which is described here:

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

This seems messy and contrary to the conventions provided by the framework.

Thank you for your time.

+3


source to share


2 answers


If you create your context like this (don't copy-paste this blindly - it's set up for blocking operations):

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

      



Then you should be able to get an instance ExecutorService

from it:

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

      

+5


source


Some ExecutionContext implementations implement the ExecutionContextExecutorService trait, which is a sub-interface of both ExecutionContext and ExecutorService. You can check if ExecutionContext from Play / Akka is one of those.



0


source







All Articles