send a broadcast message

141 views
Skip to first unread message

Manar Elkady

unread,
Jan 16, 2016, 6:05:01 AM1/16/16
to Akka User List

Hi,

In my application, I'd like to broadcast a time unit every 2 sec to all actors instances of Worker. The time unit is sent by the master to all the worker
I am trying to send a broadcast message to set of workers by the master actor, but it doesn't work with me. I attach my test code here. There is a compile error in the line 
             router ! Broadcast("any message") 
"Error: value is not a member of akka.routing.Router" Could anyone tell me what is the problem here.
 

import akka.actor._
import akka.routing.{ ActorRefRoutee, RoundRobinRoutingLogic, Router }
import akka.routing.Broadcast
import akka.routing.Router
import akka.routing.RouterActor

object Messages{
 object Work
 object Terminated
}

object MainRouterDriver extends App {
  import Messages._
  // an actor needs an ActorSystem
val system = ActorSystem("HelloSystem")
// create and start the actor
val routingMaster = system.actorOf(Props[Master], name = "helloactor")
// send the actor two messages
routingMaster ! Work
  
}

class Worker extends Actor{
  def receive = {
    case _ =>
        println("Hi I am a Worker")
  }
  
  
}
class Master extends Actor {
  var router = {
  val routees = Vector.fill(5) {
      val r = context.actorOf(Props[Worker])
      context watch r
      ActorRefRoutee(r)
    }
  akka.routing.Router(RoundRobinRoutingLogic(), routees)
  }
def receive = {
    case Work =>
             router ! Broadcast("any message") 
    
  }

}



Johan Andrén

unread,
Jan 17, 2016, 9:40:52 AM1/17/16
to Akka User List
Hi Manar,

To send messages using a Router you would use the .route(message, sender) method
and not ! (this is because the router isn't an actor)

For more details about how routers work, look in the docs here:

--
Johan Andrén
Typesafe -  Reactive apps on the JVM
Twitter: @apnylle

Manar Elkady

unread,
Jan 18, 2016, 3:01:34 AM1/18/16
to Akka User List
Thanks, Johan,  for illustration, but do you mean that there is no way to broadcast messages to all actors in a system context?

Manar

Johan Andrén

unread,
Jan 18, 2016, 3:10:13 AM1/18/16
to Akka User List
Not sure what you mean with "all actors in a system context", but if it is
the broadcast to all routees of a router that you initially wanted to do, 
then you can still do that, just that you need to use .route instead of !
to send your message wrapped with akka.routing.Broadcast.

--
Johan Andrén
Typesafe -  Reactive apps on the JVM
Twitter: @apnylle

Manar Elkady

unread,
Jan 18, 2016, 4:08:30 AM1/18/16
to Akka User List
Johan,

Please forgive me for not illustrative question because I am a newcomer to akka. I will illustrate to you what I mean.

I have three different actors types (actor1, actor2, actor3) in my application and each one of them has different state and behaviors while communicating with each other. Initially, I created set of instances from each actor type. To synchronize the actions taken by them during the application running I want broadcast a Time_Unit message to all instances of these actors  by a controller actor. 

I decided to use routers because I don't have other options to broadcast messages. Is there any other way to do that?

Manar,

Johan Andrén

unread,
Jan 18, 2016, 4:32:41 AM1/18/16
to akka...@googlegroups.com
Manar,

I would say that isn’t the regular use case for routers.

Built into the actor system there is an event bus which might be a better fit.

Actors can register to the event bus that they are interested in events of a specific type. This way you can publish the TimeUnit message from one place without knowing what actors should receive it, the actors themselves will be responsible to subscribe when they start up.

You can read more about the event bus in the docs here:

--
Johan Andrén
Typesafe -  Reactive apps on the JVM
Twitter: @apnylle

--
>>>>>>>>>> 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 a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/y0hVf6fq58M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Manar Elkady

unread,
Jan 18, 2016, 5:07:05 AM1/18/16
to Akka User List
Thanks, Johan, for illustrating this idea. I'll examine the EventBus.

Manar,
Reply all
Reply to author
Forward
0 new messages