0

I have a scala file that has this import

import scala.concurrent.ExecutionContext.Implicits.global

This works fine as the Future I am using works fine in my methods in that file. However, I have created a separate method that will be used for disk IO and I have a new execution context. I want this method to use this context while other methods can continue to use the default context. How do I guarantee that? I currently do this as follows

private def testContext():Future[Int] = { val system = ActorSystem.create() implicit val myexecutionContext = system.dispatchers.lookup("blocking-io-dispatcher.db-backup-context") Future{logger.error("inside my new thread pool wonderland");10}{myexecutionContext}

Is there a way without specifying the "myexecutionContext" like this at the end of each Future call, I can make this method still use "myexecutionContext" for all Future calls? In short, I do not want to be specifying "myexecutionContext" again and again as a parameter inside my "testContext" method. How do I do that?

curiousengineer
  • 2,196
  • 5
  • 40
  • 59

1 Answers1

3

You don't need to specify myexecutionContext inside testContext(). myexecutionContext is defined inside testContext(), which takes precedence over the global context imported outside the method. All Futures created in testContext() will use myexecutionContext by default.

Ziyang Liu
  • 810
  • 4
  • 10
  • Is this because I have declared it as "implicit" inside my method? What if I declare ``myexecutionContext`` as ``val ..`` instead of ``implicit val``. Also is there a way to verify which pool was used for executing the ``Future`` just for my own satisfaction? – curiousengineer Sep 03 '17 at 01:56
  • You need to declare it as `implicit val` for it to be used as an implicit parameter. Otherwise the global one will be used. For verification, see this question: https://stackoverflow.com/questions/38517712/in-scala-is-there-a-way-to-tell-which-execution-context-you-are-running-in – Ziyang Liu Sep 03 '17 at 02:07
  • yes thanks for the help, I just did the same ``Thread.currentThread.getName`` like we do in Java and I am getting the name of my context. One last question is why does it prepend the word "default" before it in my log. Here is what I see in the log ``inside my new thread pool wonderland and context is default-blocking-io-dispatcher.db-backup-context-7`` – curiousengineer Sep 03 '17 at 02:30
  • Am not super familiar with Akka dispatchers, it may involve something that is undefined (maybe `blocking-io-dispatcher` doesn't exist?) but it's just a wild guess. – Ziyang Liu Sep 03 '17 at 03:00
  • I am using play and I create that in the ``application.conf`` ``blocking-io-dispatcher { db-backup-context { thread-pool-executor { core-pool-size-factor = 10.0 core-pool-size-max = 10 } }`` – curiousengineer Sep 03 '17 at 03:07