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:
- 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:
You can also create dispatchers from code. For more dispatcher configuration examples click here.here
Reblogged this on knoldermanish.
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.