Wednesday 23 November 2016

"Akka in Action" Notes: Chapter 10-12


Point-to-Point Channel
It's possible for a point-to-point channel to have multiple receivers, but the channel makes sure that only one receiver receives the message. ActorRef is the implementation of a point-to-point channel.
All messages sent will be delivered to one actor, and the order of the messages sent to the ActorRef will not change.


Publish-subscribe Channel
Akka has an EventStream that implements a pub-sub channel. The channel, instead of the sender, is responsible for keeping track of the receivers who need the message. Every ActorSystem has only one EventStream. The EventStream can be seen as a manager of multiple pub-sub channels, because the actor can subscribe to a specific message type.

system.eventStream.publish(new Order())
system.eventStream.subscribe(DeliverOrder.ref, classOf[Order])

Dead Letter Channel

By monitoring this channel, you know which messages aren't processed and can take corrective actions. Akka uses EventStream to implement the dead-letter queue so that only the actors that are interested in the failed messages receive them. The message is wrapped into a DeadLetter object, which contains the original message, the sender of the message, and the intended receiver.
When messages are sent to a terminated actor, these message will be sent to the DeadLetter queue.

Guaranteed Delivery Channel

This channel is a point-to-point channel with the guarantee that the message will be delivered to the receiver. Akka can't guarantee exactly once message delivery in all cases.
Sending local messages will not likely fail, because it's like a normal method call. The delivery is guaranteed as long as there are no critical VM errors.

Losing messages becomes an issue when you're using remote actors. At-most-once delivery is guaranteed.

Finite-State Machine

FSM allows you to transition things from one state to another, in atomic , isolated way. Actors receive one message at a time, so no locks will be needed. The changing is triggered by an event or condition. The events in an Akka FSM are the messages the actor receives.

Two biggest steps are defining the state and then the transitions.
State is usually a sealed trait with case objects extending it.
StateData that we use when we need a state condition to decide which transition is fired.

When() declaration is a partial function to handle all the possible events in the specified state. The transition declaration is updating the StateData.

The actual functionality is done by the entry and exit actions, which implemented in the onTransition declaration. Jus as the transitions are declared for each state, the actions are also implemented for each state. The transition callback is also a partial function and takes as input the current state and the next state.

onTransition {
    case CurrentState -> NextState =>
    ... }

FSM supports timers to detect an idle connection or a failure, because the reply isn't received within a specified time.


Agents

An agent guards the shared state and allows multiple threads to get the state, and is responsible for updating it on behalf of the various threads. Because the agent does the updating, the threads don't need to know about locking. Updating can only be done by asynchronously sending actions to the agent. All updates to the state must be done within the agent's execution context.


Akka Http

Endpoints are part of our system, and send and/or receive messages between the external system and the rest of our system.
Translating the different messages into a common message is called the normalizer pattern.
It's a good parctice to separate the route definitions from the usage of an actor, because this enables you to test the routes without starting the actor or by injecting a TestProbe.

A Route uses directives to match an HTTP request, and extracts data from it. The route needs to complete the HTTP request with an HTTP response for every matched pattern.



No comments:

Post a Comment