Hi Dominik,
your code will execute on the ExecutionContext which you pass in.
To make your code less verbose, you can use implicit values. Define or
import an implicit value of ExecutionContext in the scope where you use
Future{} or pipe() and you will not need to pass that in explicitly anymore.
On Thu, Nov 13, 2014 at 2:30 PM, Dominik Dorn <[email protected]>
wrote:
> Thanks for your answer Will,
> I've seen that documentation yesterday and tried to implement it.
>
> I couldn't get the
> future pipeTo(actor) to run, because I always got an exception like:
> [error] actors/HeartbeatProcessingActor.scala:62: value pipeTo is not a
> member of
> scala.concurrent.Future[actors.HeartbeatProcessingActor.EnhancedProcessUpdateMessage]
> even with an import akka.pattern._ it did not compile (but did not show a
> compile error in my IDE either.. strange)
>
> I now managed to get it running with this code:
> val primaryDBExecutionContext: ExecutionContext =
> context.system.dispatchers.lookup("akka.actor.primary-db-execution-context")
> val f = Future {
> val immutableSerialMap: Map[String, Long] = DB.withTransaction({
> implicit conn =>
> ....
> }) }(primaryDBExecutionContext)
>
> * pipe(f)(primaryDBExecutionContext) to context.self*
>
> Could you please elaborate if specifying the additional ExecutionContext
> (primaryDBExecutionContext) *on the future itself* (f) *and* *the pipe*
> is the right thing to do?
>
> I want to be sure to not block the actor thread pool.
>
> Thanks,
> Dominik
>
>
>
> On Thursday, November 13, 2014 10:04:24 AM UTC+1, Will Sargent wrote:
>>
>> Hi Dominik,
>>
>> Good talking to you yesterday -- here's the documentation I was referring
>> to about futures and pipeTo.
>>
>> http://doc.akka.io/docs/akka/current/scala/futures.html
>> http://doc.akka.io/docs/akka/current/scala/actors.html#Ask_
>> _Send-And-Receive-Future
>>
>> Will Sargent
>> Consultant, Professional Services
>> Typesafe <http://typesafe.com>, the company behind Play Framework
>> <http://www.playframework.com>, Akka <http://akka.io> and Scala
>> <http://www.scala-lang.org/>
>>
>> On Wed, Nov 12, 2014 at 5:26 PM, Dominik Dorn <[email protected]>
>> wrote:
>>
>>> Hi,
>>>
>>> Will showed me today at Devoxx that there's the pipeTo mechanism to do
>>> blocking code in actors at a different thread, so the actor stays non
>>> blocking. The code I came up looks like this. It would be very nice if you
>>> could take a look and tell me if I'm doing something obviously wrong:
>>>
>>> object MyActor {
>>> def props = Props[MyActor]
>>> case class InputMessage()
>>> case class MyActorResult(data)
>>> }
>>>
>>> class HeartbeatProcessingActor extends Actor {
>>>
>>> lazy val otherActor1: ActorRef = context.system.actorOf(otherActor1.props,
>>> "process-events1")
>>> lazy val otherActor2: ActorRef = context.system.actorOf(otherActor2.props,
>>> "process-events2")
>>> lazy val otherActor3: ActorRef = context.system.actorOf(otherActor3.props,
>>> "process-events3")
>>>
>>> override def receive = {
>>> case x : MyActorResult =>
>>> otherActor1 ! x
>>> otherActor2 ! x
>>> otherActor3 ! x
>>>
>>> case x: InputMessage => {
>>>
>>>
>>> val primaryDBExecutionContext: ExecutionContext =
>>> context.system.dispatchers.lookup("akka.actor.primary-db-
>>> execution-context")
>>>
>>> import akka.pattern.pipe
>>>
>>> import play.api.Play.current // I need this somehow for
>>> DB.withTransaction
>>> val f = new PipeableFuture(Future {
>>> val immutableSerialMap: Map[String, Long] = DB.withTransaction({
>>> implicit conn =>
>>> // do some blocking JDBC IO and fetch results
>>> immutableSerialMap
>>> })
>>>
>>> // TODO specify return value of future that gets piped to
>>> myself
>>> MyActorResult(immutableSerialMap)
>>> }
>>> (primaryDBExecutionContext))
>>> (primaryDBExecutionContext)
>>> .pipeTo(context.self)
>>> }
>>> }
>>> }
>>>
>>> Questions:
>>> 1. Is this code correct in respect to not blocking the akka-thread-pool
>>> ?
>>> 2. the wrapping of Future with PipeableFuture looks strange.. is this
>>> correct
>>> 3. passing the _same_ explicit execution context to both the inner
>>> future as well as the PipeableFuture looks strange.. is this correct?
>>> 4. I know, its a Play-thing, but do you see any problem with the "import
>>> play.api.Play.current" ?
>>>
>>> Thanks,
>>> Dominik
>>>
>>> --
>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/
>>> current/additional/faq.html
>>> >>>>>>>>>> Search the archives: https://groups.google.com/
>>> group/akka-user
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "Akka User List" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/akka-user.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>
--
Martynas Mickevičius
Typesafe <http://typesafe.com/> – Reactive
<http://www.reactivemanifesto.org/> Apps on the JVM
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.