Listeners example

Showing 1-8 of 8 messages
Listeners example George Moschovitis 4/1/12 6:06 AM
I would like to add some listener actors to another actor (parent). I.e. when the parent actor receives a specific message, the listener actors should be notified. The list of listeners should be dynamically modifiable.

I am wondering what's the best way to implement something like this. I have seen akka.routing.Listeners, akka.routing.Listen, etc in the reference, but the documentation is rather lacking. Any tips would be appreciated.

thanks,
-g.
Re: [akka-user] Listeners example 4/1/12 1:50 PM
Hi George,

On Sun, Apr 1, 2012 at 3:06 PM, George Moschovitis <george.mo...@gmail.com> wrote:
I would like to add some listener actors to another actor (parent). I.e. when the parent actor receives a specific message, the listener actors should be notified. The list of listeners should be dynamically modifiable.

I am wondering what's the best way to implement something like this. I have seen akka.routing.Listeners, akka.routing.Listen, etc in the reference, but the documentation is rather lacking. Any tips would be appreciated.



class MyActor extends Actor with Listeners {
  def receive = {
     case yourmessages => gossip("To All my listeners")
  } orElse listenerManagement
}


val myActor = context.actorOf(Props[MyActor])

myActor ! Listen(someActorRef)

myActor ! "pigdog"

 

Happy hAkking!

Cheers,


thanks,
-g.

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/I3d-r07OunkJ.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Re: [akka-user] Listeners example Alexandre Bertails 4/1/12 9:11 PM
On Sun, Apr 1, 2012 at 4:50 PM, Viktor Klang <viktor...@gmail.com> wrote:
> Hi George,
>
> On Sun, Apr 1, 2012 at 3:06 PM, George Moschovitis
> <george.mo...@gmail.com> wrote:
>>
>> I would like to add some listener actors to another actor (parent). I.e.
>> when the parent actor receives a specific message, the listener actors
>> should be notified. The list of listeners should be dynamically modifiable.
>>
>> I am wondering what's the best way to implement something like this. I
>> have seen akka.routing.Listeners, akka.routing.Listen, etc in the reference,
>> but the documentation is rather lacking. Any tips would be appreciated.
>
>
>
>
> class MyActor extends Actor with Listeners {
>   def receive = {
>      case yourmessages => gossip("To All my listeners")
>   } orElse listenerManagement
> }
>
>
> val myActor = context.actorOf(Props[MyActor])
>
> myActor ! Listen(someActorRef)
>
> myActor ! "pigdog"

That's very interesting (I couldn't find a mention to this feature in
the documentation).

I have one question: what happens when a listening actor is stopped?
Or more precisely: does the Listeners take care of that automatically,
or do we still have to pay attention to it? The scaladoc [1] does not
say anything about it.

Cheers,

Alexandre.

[1] http://doc.akka.io/api/akka/2.0/#akka.routing.Listeners

>
>
>
> Happy hAkking!
>
> Cheers,
> √
>
>>
>> thanks,
>> -g.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Akka User List" group.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msg/akka-user/-/I3d-r07OunkJ.
>> To post to this group, send email to akka...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> akka-user+...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/akka-user?hl=en.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To post to this group, send email to akka...@googlegroups.com.
> To unsubscribe from this group, send email to
> akka-user+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/akka-user?hl=en.

Re: [akka-user] Listeners example Akka Team 4/2/12 1:26 AM


On Mon, Apr 2, 2012 at 6:11 AM, Alexandre Bertails <alex...@bertails.org> wrote:
On Sun, Apr 1, 2012 at 4:50 PM, Viktor Klang <viktor...@gmail.com> wrote:
> Hi George,
>
> On Sun, Apr 1, 2012 at 3:06 PM, George Moschovitis
> <george.mo...@gmail.com> wrote:
>>
>> I would like to add some listener actors to another actor (parent). I.e.
>> when the parent actor receives a specific message, the listener actors
>> should be notified. The list of listeners should be dynamically modifiable.
>>
>> I am wondering what's the best way to implement something like this. I
>> have seen akka.routing.Listeners, akka.routing.Listen, etc in the reference,
>> but the documentation is rather lacking. Any tips would be appreciated.
>
>
>
>
> class MyActor extends Actor with Listeners {
>   def receive = {
>      case yourmessages => gossip("To All my listeners")
>   } orElse listenerManagement
> }
>
>
> val myActor = context.actorOf(Props[MyActor])
>
> myActor ! Listen(someActorRef)
>
> myActor ! "pigdog"

That's very interesting (I couldn't find a mention to this feature in
the documentation).

I have one question: what happens when a listening actor is stopped?

 
Or more precisely: does the Listeners take care of that automatically,
or do we still have to pay attention to it? The scaladoc [1] does not
say anything about it.

See the sauce above.
If you want to add DeathWatch, just create your own version:

trait MyListeners { self: Actor ⇒
  protected val listeners = new ConcurrentSkipListSet[ActorRef]

  protected def listenerManagement: Actor.Receive = {
    case Listen(l)        ⇒ if (listeners add l) self.context watch l
    case Deafen(l)        ⇒ if (listeners remove l) self.context unwatch l
    case WithListeners(f) ⇒ listeners foreach f
    case Terminated(l) if listeners contains l => listeners remove l
  }

  protected def gossip(msg: Any) = listeners foreach (_ ! msg)
}

Cheers,



--
Akka Team
Typesafe - The software stack for applications that scale
Blog: letitcrash.com
Twitter: @akkateam

Re: [akka-user] Listeners example George Moschovitis 4/3/12 3:17 AM
Thank you,

maybe a similar example should be included in the documentation.

-g.

On Sunday, April 1, 2012 11:50:36 PM UTC+3, √ wrote:
Hi George, 
... Happy hAkking!

Re: [akka-user] Listeners example 4/3/12 4:18 AM


On Tue, Apr 3, 2012 at 12:17 PM, George Moschovitis <george.mo...@gmail.com> wrote:
Thank you,

maybe a similar example should be included in the documentation.

Similar in what way?

Cheers,
 

-g.

On Sunday, April 1, 2012 11:50:36 PM UTC+3, √ wrote:
Hi George, 
... Happy hAkking!

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/jxyLqK0MxnsJ.

To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.



--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

Re: [akka-user] Listeners example George Moschovitis 4/3/12 5:14 AM
maybe a similar example should be included in the documentation.
Similar in what way?

I meant the same example (or at least Listeners should be mentioned).

-g.
Re: [akka-user] Listeners example Akka Team 4/3/12 5:23 AM


Yup, that'd be nice. You could also use the ActorSystem's EventStream (context.system.eventStream) to do pub/sub.

Cheers,
 

-g.

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/F_wx0GYGknEJ.

To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.



--
Akka Team
Typesafe - The software stack for applications that scale
Blog: letitcrash.com
Twitter: @akkateam