Akka messaging mechanisms by example

I have enough Apache Camel (routing / mediation / orchestration mechanism, lightweight ESB), and I'm puzzling trying to understand the difference between Akka:

  • Dispatchers ( Dispatcher , PinnedDispatcher , CallingThreadDispatcher )
  • Routers
  • Pools
  • Groups
  • Event buses

According to the docs:

Dispatchers :

... this is what Akka does. The actors are ticking, it's the engine of the car, so to speak.

But this does not really explain what a dispatcher is or what it has to do with an actor.

Routers :

Messages can be sent through the router to efficiently route them to the target participants, known as its routes. The router can be used both inside and outside the actor, and you yourself can manage the routes or use an autonomous router-actor with configuration options. But that sounds very horrible, like a dispatcher.

Pools :

The [type] of the router [which] creates routes as child entities and removes them from the router if they complete.

Groups :

The actor’s [type] [where routes] are created outside the router, and the router sends messages to the specified path using the actor’s selection without observing completion.

Event Buses :

... a way to send messages to groups of participants

It sounds like dispatchers and routers.

So my main problems are:

  • What is the difference between dispatchers, routers and event buses, and when to use them?
  • When to use vs group pool?
+7
akka routing dispatcher event-bus
source share
1 answer

A dispatcher is basically a thread pool. Akka uses a dispatcher for several things (for example, in a message queue in the right mailbox or pick up a message from an actor’s mailbox and process it). Each time one of these actions is to be performed, a thread from the thread pool is selected and used for it. Akka comes with default-dispatcher by default-dispatcher with the configuration, which you can find here in reference.conf in the search for default-dispatcher . You are already using default-dispatcher , but you can define a different dispatcher to make sure that you have a reserved thread pool for other purposes (for example, for network flows when using akka-remote or akka-cluster).

A router in Akka is an actor who uses some sort of routing logic to route messages to a list of routes. Depending on the logic, there are many types of routers: Broadcast, Balancing, RoundRobin ... you can find them all in akka docs. Router routes can be a pool or a group. One of the most popular options for using routers is to scale your application vertically, which means maximum use of the entire processor available on your system (using multiple threads at the same time).

A pool means that routes are created on demand for this type. For example, you might need a RandomPool from MyFancyActor with three instances. Akka will create three actors MyFancyActor and a fourth, which will become the actual router. Each time a member of the router receives a message, he will forward the message to one of the 3 MyFancyActor participants. The pool takes over the restart of the participants and monitors their life cycle to make sure that you have multiple instances running.

A group means routes are created before you define a router. Once you define your router, you will need to pass a list of your route participants that you previously created. The group will not control the life cycle of your actors, and you will need to do it yourself.

Event buses are channels in which you can subscribe to a specific type of message with an actor. If there is a message of this particular type, you, the actor, will receive it. This is used for some internal Akka elements, such as subscribing to DeadLetter , when a message cannot reach its destination or events regarding the formation of a cluster (in an akka cluster). You will use this to know about events happening in your ActorSystem .

+15
source share

All Articles