Java: Yet another "get or create actor question"

107 views
Skip to first unread message

Guido Medina

unread,
May 30, 2015, 8:46:05 AM5/30/15
to akka...@googlegroups.com
Hi,

I have a method to create an account balance actor per currency for that account, I had that on a hash map but I think that if Akka has an internal hash map -concurrent or not- to keep actor references then why should I keep yet another hash map?, I was using a computeIfAbsent(...) on my own hash map so I'm wondering if:
  1. Will resolveOne() method wait up to 5 seconds if the actor doesn't exist?, I wish not to wait for so long if it really doesn't because such actors are children of the supervisor calling the method within onReceive(...)
  2. The following try to resolve one, if it succeed it just forward the message else tries to create it and forward the message, if it fails creating it after it couldn't resolve it -unlikely but possible-, it will forward the message via actor selection because another thread -the next call to resolveOne()- created it.
 
  private void applyToAccount(Currency currency, BalanceOperation operation) {
   
final ActorContext context = context();
    context
.actorSelection(currency.code).resolveOne(FIVE_SECONDS).onComplete(
     
new OnComplete<ActorRef>() {
       
@Override
       
public void onComplete(Throwable failure, ActorRef balanceRef) throws Throwable {
         
if (!(failure instanceof ActorNotFound)) {
           
// Actor exists, forward the message.
            balanceRef
.forward(operation, context);
         
} else {
           
try {
             
// Try to create it and forward the message.
              context
.actorOf(balancePersistorProps(currency), currency.code).forward(operation, context);
           
} catch (Exception e) {
             
// It failed creating it so looks the unlikely happened, forward the message via actor selection.
              context
.actorSelection(currency.code).forward(operation, context);
           
}
         
}
       
}
     
}, context.dispatcher());
 
}

Suggestions are very welcome,

Many thanks,

Guido.

Sebastián Ortega

unread,
May 31, 2015, 5:21:46 PM5/31/15
to akka...@googlegroups.com
Hello,

As you are using the currency code as the actor name you can just use context.child.

context.child(currency.code).getOrElse(... create a new one ...)

Regards

Guido Medina

unread,
May 31, 2015, 8:17:19 PM5/31/15
to akka...@googlegroups.com
Many thanks, that Optional pattern will come very handy to me.

Guido Medina

unread,
May 31, 2015, 8:50:53 PM5/31/15
to akka...@googlegroups.com
Is there any way to avoid the verbosity?, I try with a more functional approach which was OK for IntelliJ but not for the Java 8 compiler, here is the form that is working:

  private void applyToAccount(Currency currency, BalanceOperation operation) {

    context
().child(currency.code).
      getOrElse
(new AbstractFunction0<Option<ActorRef>>() {
       
@Override
       
public Option<ActorRef> apply() {
         
return Option.some(context().actorOf(balancePersistorProps(currency), currency.code));
       
}
     
}).
     
get().forward(operation, context);
 
}


But I was hoping the following would work, but Java 8 can't infer the type, strange IntelliJ recognizes it I guess the mix of Java and Scala drives it crazy:

 
private void applyToAccount(Currency currency, BalanceOperation operation) {

    context
().child(currency.code).
      getOrElse
(() -> Option.some(context().actorOf(balancePersistorProps(currency), currency.code))).
     
get().forward(operation, context);
 
}


Best regards.

Roland Kuhn

unread,
Jun 2, 2015, 5:43:51 AM6/2/15
to akka-user
Thanks for raising this, we should fix it for Akka 2.4 by offering a method that gives you an Optional instead of a Scala Option.

Regards,

Roland

--
>>>>>>>>>> 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 akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


Viktor Klang

unread,
Jun 2, 2015, 5:52:52 AM6/2/15
to Akka User List
In the mean time there's getChild + Optional.ofNullable
--
Cheers,
Reply all
Reply to author
Forward
0 new messages