Sample akka dispatcher configuration

Table of contents
Reading Time: 2 minutes

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”.

 

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”.

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.

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.

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

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

Written by 

Girish is a Software Consultant, with experience of more than 3.5 years. 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 can work in both supervised and unsupervised environment and have a craze for computers whether working or not, he is almost always in front of his laptop's screen. His hobbies include reading books and listening to music. 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.

2 thoughts on “Sample akka dispatcher configuration2 min read

  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.

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading