As for implementation, I'd probably do something like this: (WARNING: NOT
COMPILED)
import akka.pattern.after
def keepAsking(recipient: ActorRef, msg: Any, every:
FiniteDuration)(implicit ec: ExecutionContext, scheduler: Scheduler):
Future[Any] = {
def retry(): Future[Any] = after(every, scheduler)((recipient ?
msg)(every)) recoverWith { case _ => retry() }
retry()
}
On Wed, Jan 15, 2014 at 6:22 PM, √iktor Ҡlang <[email protected]>wrote:
>
>
>
> On Wed, Jan 15, 2014 at 6:11 PM, Simon Schäfer <[email protected]> wrote:
>
>> I have the following code:
>>
>> import scala.concurrent.Future
>> import scala.concurrent.Promise
>> import scala.concurrent.duration.DurationInt
>> import scala.concurrent.duration.FiniteDuration
>>
>> import akka.actor.Actor
>> import akka.actor.ActorRef
>> import akka.pattern.ask
>> import akka.util.Timeout.durationToTimeout
>>
>> trait ActorHelpers {
>>
>> self: Actor =>
>>
>> import context.dispatcher
>>
>> def keepAsking(recipient: ActorRef, msg: Any, interval:
>> FiniteDuration): Future[Any] = {
>> val p = Promise[Any]()
>> val cancel = context.system.scheduler.schedule(0.millis, interval) {
>> if (context != null)
>> (recipient ? msg)(1.hour) foreach p.trySuccess
>> }
>> val f = p.future
>> f.onComplete(_ => cancel.cancel())
>> f
>> }
>>
>> }
>>
>> The idea of this code snippet is that a message is resend as long as an
>> answer is retrieved. The problem is that I sometimes get a NPE on the line
>> that contains the ask.
>>
>> Without the check if context is null I get even more NPEs. I don't
>> understand why I get NPEs here, because I can't see where context or some
>> of its state is accessed. Can someone explain it?
>>
>
> Hint: remove import context.dispatcher
>
>
>>
>> --
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>> >>>>>>>>>> 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/groups/opt_out.
>>
>
>
>
> --
> Cheers,
> √
>
> * Viktor Klang*
> *Director of Engineering*
> Typesafe <http://www.typesafe.com/>
>
> Twitter: @viktorklang
>
--
Cheers,
√
*Viktor Klang*
*Concurrent, Distributed*
Twitter: @viktorklang
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> 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/groups/opt_out.