O'Reilly logo

Akka Essentials by Munish K. Gupta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required