3

Sample akka dispatcher configuration

 3 years ago
source link: https://blog.knoldus.com/sample-akka-dispatcher-configuration/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sample akka dispatcher configuration

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

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

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

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

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

xxxxxxxxxx
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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK