First of all, you shouldn't ever block like this, as you do with Await.
As for your question - this sounds like something the parent actor should
be responsible for.
I'm not even sure the code above works (it at least never occurred to me to
try to create an actor using a full path as I always understood the docs as
if it shouldn't be possible).
I think allowing the parent actor to determine if one of his children
exists or not is cleaner.
It also has direct access to this through the actor context, so the answer
is immediate.
On Sunday, April 5, 2015 at 10:50:28 PM UTC+3, Fatih Dönmez wrote:
>
> Hi,
>
> I want to create an actor if it didn't created already. To check its
> existence I use actorSelection. If there is no actor defined by the path, I
> create a new one with actorOf.
> Is this approach correct? How can I improve the design? Here is the code I
> come up with currently;
>
> val fullPath: String = path.format(appId)
> var starter: ActorRef = null
> implicit val timeout = Timeout(5 seconds)
>
> try {
> starter =
> Await.result(context.actorSelection(fullPath).resolveOne,FiniteDuration(5,TimeUnit.SECONDS))
> } catch {
> case e: ActorNotFound => {
> starter = context.actorOf(Props(new StarterNode(customer, appId)),
> name = fullPath)
> logger.info("Actor [" + fullPath + "] creating for first time")
> }
> case e: Exception => logger.error("Actor [" + fullPath + "]
> selection failed",e)
> }
>
> //TODO
> if(starter != null) {
> starter ! event
> } else {
> logger.warn("Starter node failed with timeout")
> }
>
>
>
--
>>>>>>>>>> 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.