Listeners example

880 views
Skip to first unread message

George Moschovitis

unread,
Apr 1, 2012, 9:06:08 AM4/1/12
to akka...@googlegroups.com
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.

Viktor Klang

unread,
Apr 1, 2012, 4:50:36 PM4/1/12
to akka...@googlegroups.com
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.

Alexandre Bertails

unread,
Apr 2, 2012, 12:11:29 AM4/2/12
to akka...@googlegroups.com
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

Akka Team

unread,
Apr 2, 2012, 4:26:11 AM4/2/12
to akka...@googlegroups.com
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

George Moschovitis

unread,
Apr 3, 2012, 6:17:54 AM4/3/12
to akka...@googlegroups.com
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!

√iktor Ҡlang

unread,
Apr 3, 2012, 7:18:34 AM4/3/12
to akka...@googlegroups.com
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

George Moschovitis

unread,
Apr 3, 2012, 8:14:39 AM4/3/12
to akka...@googlegroups.com
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.

Akka Team

unread,
Apr 3, 2012, 8:23:12 AM4/3/12
to akka...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages