Sample akka dispatcher configuration


Akka dispatchers plays important role in application performance. After reading this blog you will be able to configure and use different dispatchers for your application. I have tried to make it as simple as possible so that you can easily start working on akka dispatchers.

By default akka uses default dispatcher “fork-join-executor” if no configuration is defined for custom dispatchers. To set up a dispatchers for your application you have to provide dispatcher configuration in configuration file.

To read more about dispatchers click here.

Let’s start implementing the dispatcher configuration now.

To configure a dispatcher for your application we can select any of the dispatchers according to the use cases. Lets start with a simple one. Here is an example of dispatcher configuration. This dispatcher uses “fork-join executor”.

    my-dispatcher {
      type = Dispatcher
      executor = "fork-join-executor"
      fork-join-executor {
        parallelism-min = 2
        parallelism-factor = 2.0
        parallelism-max = 10
      }
      throughput = 100
    }

 

Once you are done with configuration, you can use this dispatcher in two ways:

  1. Dispatcher allocation for specific actors using configuration file:

You can allocate a dispatcher to specific user from configuration. Here is an example of configuration that allocate “my-dispatcher” to the actor named “wordCounter”.

akka.actor.deployment {
	/wordCounter {
    	dispatcher = my-dispatcher
    }
}

Once the configuration is done. You can create a new actor using following lines. The dispatcher allocated to the actor will be “my-dispatcher”. You can check the allocated dispatcher by printing thread name within receive function of actor definition.

import akka.actor.Props
val system = ActorSystem("wordprocessor")
val wordCounterActor = system.actorOf(Props[WordCountActor], "wordCounter")

2. Dispatcher allocation from code:

We can also allocate specific dispatcher for an actor from code itself. Here is the configuration code for “thread-pool-executor”. You can write configuration in application.conf file of your application.

my-thread-pool-dispatcher {
type = Dispatcher
executor = "thread-pool-executor";
thread-pool-executor {
	core-pool-size-min = 2
	core-pool-size-factor = 2.0
	core-pool-size-max = 10
}
throughput = 100
}

After writing configuration in conf file. You can create a new actor with configured dispatcher using ActorContext.


class WordCountActor extends Actor {

  val logger = LoggerFactory.getLogger(this.getClass)

  def receive = {
    case Count(line: String) => {
      sender ! line.length()
      val printer = context.actorOf(Props[PrinterActor].withDispatcher("my-thread-pool-dispatcher"),"printerActor")
      val actorpath = printer.path
      printer ! Print(line)
    }
    case _ => logger.debug("Oops..!! I did'nt understand the message..!!")
  }
}

You can see the allocated dispatcher by printing the thread name within the new actor object or from logs.

You can check and clone a sample project for the dispatcher configuration implementation here:

AkkaDispatcherConfigSample

You can also create dispatchers from code. For more dispatcher configuration examples click here.here

Advertisements

About Girish Bharti

Girish is Sr. Software Consultant at Knoldus Software LLP. He is a scala developer and very passionate about his interest towards Scala Eco-system. He has also done many projects in different languages like Java and Asp.net. He is self motivated, dedicated and focused towards his work. He believes in developing quality products. He wants to work on different projects and different domains. He is curious to gain knowledge of different domains and try to provide solutions that can utilize resources and improve performance. His personal interests include reading books, video games, cricket and social networking. He has done Masters in Computer Applications from Lal Bahadur Shastri Institute of Management, New Delhi.
This entry was posted in Akka, Scala and tagged , , , . Bookmark the permalink.

2 Responses to Sample akka dispatcher configuration

  1. Reblogged this on Girish Bharti and commented:
    Akka is one of the most popular framework to utilize resources and provide concurrency. Akka dispatchers plays very important role in making akka actors work. Here is an example of how you can configure akka dispatchers for your application in a simple and easy way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s