HotSwap

Another key functionality of Akka is the ability to HotSwap an actor's message loop functionality at runtime. The functionality is provided via the getContext().become() and getContext().unbecome() methods. The HotSwapped code is kept in a stack, which can be pushed and popped when the become() or unbecome() methods are invoked.

Let's see a simple Ping Pong example, where we develop a logic to handle the messages swapped at runtime.

Java:

public class PingPongActor extends UntypedActor { static String PING = "PING"; static String PONG = "PONG"; int count = 0; public void onReceive(Object message) throws Exception { if (message instanceof String) { if (((String) message).matches(PING)) { System.out.println("PING"); count += 1; Thread.sleep(100); ...

Get Akka Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.