I'm trying to understand how
thread-pool-executor works.In my config I have:
queue-ec {
type = Dispatcher
executor = "thread-pool-executor"
thread-pool-executor {
fixed-pool-size = 1
}
throughput = 1
}
and I have a piece of code that executes following loop - it basically is a Future that calls itself on complete
def receive[T](n:Int):Future[T] = ...
implicit val ec =system.dispatchers.lookup("queue-ec")
def looper: Unit =
receive(queueSize) onComplete {
case Success(msgs) =>
val t = Thread.currentThread()
println(s"Looper thread ${t.getName}/${t.getId}/${t.hashCode()}")
looper
case Failure(failure) =>
looper
}
What I can see from logs is that each invocation of looper is executed on another thread, whereas I would expect that all invocations are on the same thread from "queue-ec" thread-pool-executor
logs:
10:10:36 [sgActors-queue-ec-52] INFO m.s.g.SqsGroupsProcessingQueueConsumer: Looper thread sgActors-queue-ec-52/134/2039717271
10:10:46 [sgActors-queue-ec-57] INFO m.s.g.SqsGroupsProcessingQueueConsumer: Looper thread sgActors-queue-ec-57/142/1130275026
10:10:57 [sgActors-queue-ec-61] INFO m.s.g.SqsGroupsProcessingQueueConsumer: Looper thread sgActors-queue-ec-61/153/1511413879
10:11:07 [sgActors-queue-ec-63] INFO m.s.g.SqsGroupsProcessingQueueConsumer: Looper thread sgActors-queue-ec-63/158/6809925
when I replace thread-pool-executor with
implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(1))
I have expected behavior of execution on the same thread in execution context:
10:18:15 [pool-19-thread-1] INFO m.s.g.SqsGroupsProcessingQueueConsumer: Looper thread pool-19-thread-1/133/4527812
10:18:25 [pool-19-thread-1] INFO m.s.g.SqsGroupsProcessingQueueConsumer: Looper thread pool-19-thread-1/133/4527812
10:18:35 [pool-19-thread-1] INFO m.s.g.SqsGroupsProcessingQueueConsumer: Looper thread pool-19-thread-1/133/4527812
And more general question: is it a good practice to use Akka dispatchers for pure scala Futures, or should I rather use separate ExecutionContext for that purpose ?