|
| 1 | +--- |
| 2 | +title: "Actor Model Pattern in Java: Building Concurrent Systems with Elegance" |
| 3 | +shortTitle: Actor Model |
| 4 | +description: "Explore the Actor Model pattern in Java with real-world examples and practical implementation. Learn how to build scalable, message-driven systems using actors, messages, and asynchronous communication." |
| 5 | +category: Concurrency |
| 6 | +language: en |
| 7 | +tag: |
| 8 | + - Concurrency |
| 9 | + - Messaging |
| 10 | + - Isolation |
| 11 | + - Asynchronous |
| 12 | + - Distributed Systems |
| 13 | + - Actor Model |
| 14 | +--- |
| 15 | + |
| 16 | +## Also Known As |
| 17 | + |
| 18 | +- Message-passing concurrency |
| 19 | +- Actor-based concurrency |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Intent of Actor Model Pattern |
| 24 | + |
| 25 | +The Actor Model pattern enables the construction of highly concurrent, distributed, and fault-tolerant systems by using isolated components (actors) that interact exclusively through asynchronous message passing. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## Detailed Explanation of Actor Model Pattern with Real-World Examples |
| 30 | + |
| 31 | +### 📦 Real-world Example |
| 32 | + |
| 33 | +Imagine a customer service system: |
| 34 | +- Each **customer support agent** is an **actor**. |
| 35 | +- Customers **send questions (messages)** to agents. |
| 36 | +- Each agent handles one request at a time and can **respond asynchronously** without interfering with other agents. |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +### 🧠 In Plain Words |
| 41 | + |
| 42 | +> "Actors are like independent workers that never share memory and only communicate through messages." |
| 43 | +
|
| 44 | +--- |
| 45 | + |
| 46 | +### 📖 Wikipedia Says |
| 47 | + |
| 48 | +> [Actor model](https://en.wikipedia.org/wiki/Actor_model) is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent computation. |
| 49 | +
|
| 50 | +--- |
| 51 | + |
| 52 | +### 🧹 Architecture Diagram |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Programmatic Example of Actor Model Pattern in Java |
| 59 | + |
| 60 | +### Actor.java |
| 61 | + |
| 62 | +```java |
| 63 | +public abstract class Actor implements Runnable { |
| 64 | + |
| 65 | + @Setter @Getter private String actorId; |
| 66 | + private final BlockingQueue<Message> mailbox = new LinkedBlockingQueue<>(); |
| 67 | + private volatile boolean active = true; |
| 68 | + |
| 69 | + |
| 70 | + public void send(Message message) { |
| 71 | + mailbox.add(message); |
| 72 | + } |
| 73 | + |
| 74 | + public void stop() { |
| 75 | + active = false; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void run() { |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + protected abstract void onReceive(Message message); |
| 84 | +} |
| 85 | + |
| 86 | +``` |
| 87 | + |
| 88 | +### Message.java |
| 89 | + |
| 90 | +```java |
| 91 | + |
| 92 | +@AllArgsConstructor |
| 93 | +@Getter |
| 94 | +@Setter |
| 95 | +public class Message { |
| 96 | + private final String content; |
| 97 | + private final String senderId; |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### ActorSystem.java |
| 102 | + |
| 103 | +```java |
| 104 | +public class ActorSystem { |
| 105 | + public void startActor(Actor actor) { |
| 106 | + String actorId = "actor-" + idCounter.incrementAndGet(); // Generate a new and unique ID |
| 107 | + actor.setActorId(actorId); // assign the actor it's ID |
| 108 | + actorRegister.put(actorId, actor); // Register and save the actor with it's ID |
| 109 | + executor.submit(actor); // Run the actor in a thread |
| 110 | + } |
| 111 | + public Actor getActorById(String actorId) { |
| 112 | + return actorRegister.get(actorId); // Find by Id |
| 113 | + } |
| 114 | + |
| 115 | + public void shutdown() { |
| 116 | + executor.shutdownNow(); // Stop all threads |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### App.java |
| 122 | + |
| 123 | +```java |
| 124 | +public class App { |
| 125 | + public static void main(String[] args) { |
| 126 | + ActorSystem system = new ActorSystem(); |
| 127 | + Actor srijan = new ExampleActor(system); |
| 128 | + Actor ansh = new ExampleActor2(system); |
| 129 | + |
| 130 | + system.startActor(srijan); |
| 131 | + system.startActor(ansh); |
| 132 | + ansh.send(new Message("Hello ansh", srijan.getActorId())); |
| 133 | + srijan.send(new Message("Hello srijan!", ansh.getActorId())); |
| 134 | + |
| 135 | + Thread.sleep(1000); // Give time for messages to process |
| 136 | + |
| 137 | + srijan.stop(); // Stop the actor gracefully |
| 138 | + ansh.stop(); |
| 139 | + system.shutdown(); // Stop the actor system |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## When to Use the Actor Model Pattern in Java |
| 147 | + |
| 148 | +- When building **concurrent or distributed systems** |
| 149 | +- When you want **no shared mutable state** |
| 150 | +- When you need **asynchronous, message-driven communication** |
| 151 | +- When components should be **isolated and loosely coupled** |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +## Actor Model Pattern Java Tutorials |
| 156 | + |
| 157 | +- [Baeldung – Akka with Java](https://www.baeldung.com/java-akka) |
| 158 | +- [Vaughn Vernon – Reactive Messaging Patterns](https://vaughnvernon.co/?p=1143) |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## Real-World Applications of Actor Model Pattern in Java |
| 163 | + |
| 164 | +- [Akka Framework](https://akka.io/) |
| 165 | +- [Erlang and Elixir concurrency](https://www.erlang.org/) |
| 166 | +- [Microsoft Orleans](https://learn.microsoft.com/en-us/dotnet/orleans/) |
| 167 | +- JVM-based game engines and simulators |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Benefits and Trade-offs of Actor Model Pattern |
| 172 | + |
| 173 | +### ✅ Benefits |
| 174 | +- High concurrency support |
| 175 | +- Easy scaling across threads or machines |
| 176 | +- Fault isolation and recovery |
| 177 | +- Message ordering within actors |
| 178 | + |
| 179 | +### ⚠️ Trade-offs |
| 180 | +- Harder to debug due to asynchronous behavior |
| 181 | +- Slight performance overhead due to message queues |
| 182 | +- More complex to design than simple method calls |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +## Related Java Design Patterns |
| 187 | + |
| 188 | +- [Command Pattern](../command) |
| 189 | +- [Mediator Pattern](../mediator) |
| 190 | +- [Event-Driven Architecture](../event-driven-architecture) |
| 191 | +- [Observer Pattern](../observer) |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +## References and Credits |
| 196 | + |
| 197 | +- *Programming Erlang*, Joe Armstrong |
| 198 | +- *Reactive Design Patterns*, Roland Kuhn |
| 199 | +- *The Actor Model in 10 Minutes*, [InfoQ Article](https://www.infoq.com/articles/actor-model/) |
| 200 | +- [Akka Documentation](https://doc.akka.io/docs/akka/current/index.html) |
| 201 | + |
0 commit comments