sarah w
sarah w

Reputation: 3515

how to check my akka dispatchers defined in config file working or not

HI I am new to akka dispatchers i took help from the akka documentation

I want to check either i tuned up the dispatcher correctly or not here is my applica.conf

include "DirectUserWriteMongoActor" 

akka {
   loggers = ["akka.event.slf4j.Slf4jLogger"]
   loglevel = "DEBUG"

}

here is my DirectUserWriteMongoActor.conf

akka {
  actor{

    ############################### Setting for a Dispatcher #####################################              
    directUserWriteMongoActor-dispatcher {
         type = Dispatcher
    executor = "fork-join-executor"
  fork-join-executor {
    parallelism-min = 2
    parallelism-factor = 2.0
    parallelism-max = 10
  }
  throughput = 10         
                  } #end default-dispatcher 
     ############################### Setting for a Router #####################################              
     deployment{
     /directUserWritwMongoActorRouter{
     router = round-robin
     nr-of-instances = 5
     }
     }#end deployment

   }  #end Actor
}  #end Akka

And here is my code

object TestActor extends App{

      val config = ConfigFactory.load().getConfig("akka.actor")

      val system = ActorSystem("TestActorSystem",config)

      val DirectUserWriteMongoActor = system.actorOf(Props[DirectUserWriteMongoActor].withDispatcher("directUserWriteMongoActor-dispatcher"), name = "directwritemongoactor")

class DirectUserWriteMongoActor extends Actor {
def receive =
{
case _ => 
}
}

when i run it the code compiles but i am wondering how do i get to know whether akka dispatcher is working or not please help

Upvotes: 1

Views: 617

Answers (2)

You can always print the thread name from inside an Actor, since thread pools (used by dispatchers) will have proper names you should be able to identify if it's running on the dispatcher you expected it to run.

class CheckingThread extends Actor {
  def receive = { 
    case _ => 
      println(s"Using thread: ${Thread.currentThread().getName}")
      // or reply with it: sender() ! Thread.currentThread().getName 
  }
}

You can also just investigate the running threads by opening visual vm or any jvm profiler that you like / have available.

Upvotes: 2

Ende Neu
Ende Neu

Reputation: 15783

You can use the system object to get the current dispatcher:

scala> val config = ConfigFactory.load().getConfig("akka.actor")
config: com.typesafe.config.Config = Config(SimpleConfigObject(... "default-dispatcher" ... )

scala>   val system = ActorSystem("ArteciateActorSystem", config)
system: akka.actor.ActorSystem = akka://ArteciateActorSystem

scala>   system.dispatcher.toString
res7: String = Dispatcher[akka.actor.default-dispatcher]

Note that I've cut the code in Config to the relevant part, what you get there is much more verbose.

Upvotes: 1

Related Questions