|
| 1 | +--- |
| 2 | +shortTitle: Actor Model |
| 3 | +category: Concurrency |
| 4 | +language: de |
| 5 | +tag: |
| 6 | + - Concurrency |
| 7 | + - Messaging |
| 8 | + - Isolation |
| 9 | + - Asynchronous |
| 10 | + - Distributed Systems |
| 11 | + - Actor Model |
| 12 | +--- |
| 13 | + |
| 14 | +## Alternativbezeichnungen |
| 15 | + |
| 16 | +- Message-passing concurrency |
| 17 | +- Actor-based concurrency |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Zweck |
| 22 | + |
| 23 | +Das Actor-Model-Pattern ermöglicht die Konstruktion von hochparallelen fehlertoleranten verteilten Systemen, |
| 24 | +indem es isolierte Komponenten (Akteure) verwendet, die ausschließlich über asynchronen Nachrichtenaustausch interagieren. |
| 25 | + |
| 26 | +## Detaillierte Erklärung |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +### 📦 Reales Beispiel |
| 31 | + |
| 32 | +Stellen Sie sich ein Kundendienstsystem vor. |
| 33 | +- Jeder **Kundendienstmitarbeiter** ist ein **Aktor**. |
| 34 | +- Kunden **senden Anfragen (Nachrichten)** an die Mitarbeiter. |
| 35 | +- Jeder Mitarbeiter behandelt zu einem bestimmten Zeitpunkt genau eine Anfrage und kann diese **asychron beantworten**, |
| 36 | +- ohne dabei anderen Mitarbeitern in die Quere zu kommen. |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +### 🧠 In einfachen Worten |
| 41 | + |
| 42 | +> "Aktoren sind wie unabhängige Arbeiter, die keine Ressourcen teilen und nur über Nachrichten kommunizieren." |
| 43 | +
|
| 44 | +--- |
| 45 | + |
| 46 | +### 📖 Wikipedia sagt |
| 47 | + |
| 48 | +> Das [Actor Model](https://en.wikipedia.org/wiki/Actor_model) ist ein mathematisches Modell |
| 49 | +> für parallele Informationsverarbeitung, das "Aktoren" als universelle Ausführer von Aufgaben betrachtet. |
| 50 | +
|
| 51 | +--- |
| 52 | + |
| 53 | +### 🧹 Klassendiagramm |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## Programmbeispiel in Java |
| 60 | + |
| 61 | +### Actor.java |
| 62 | + |
| 63 | +```java |
| 64 | +public abstract class Actor implements Runnable { |
| 65 | + |
| 66 | + @Setter @Getter private String actorId; |
| 67 | + private final BlockingQueue<Message> mailbox = new LinkedBlockingQueue<>(); |
| 68 | + private volatile boolean active = true; |
| 69 | + |
| 70 | + |
| 71 | + public void send(Message message) { |
| 72 | + mailbox.add(message); |
| 73 | + } |
| 74 | + |
| 75 | + public void stop() { |
| 76 | + active = false; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void run() { |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + protected abstract void onReceive(Message message); |
| 85 | +} |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | +### Message.java |
| 90 | + |
| 91 | +```java |
| 92 | + |
| 93 | +@AllArgsConstructor |
| 94 | +@Getter |
| 95 | +@Setter |
| 96 | +public class Message { |
| 97 | + private final String content; |
| 98 | + private final String senderId; |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### ActorSystem.java |
| 103 | + |
| 104 | +```java |
| 105 | +public class ActorSystem { |
| 106 | + public void startActor(Actor actor) { |
| 107 | + String actorId = "actor-" + idCounter.incrementAndGet(); // Generate a new and unique ID |
| 108 | + actor.setActorId(actorId); // assign the actor it's ID |
| 109 | + actorRegister.put(actorId, actor); // Register and save the actor with it's ID |
| 110 | + executor.submit(actor); // Run the actor in a thread |
| 111 | + } |
| 112 | + public Actor getActorById(String actorId) { |
| 113 | + return actorRegister.get(actorId); // Find by Id |
| 114 | + } |
| 115 | + |
| 116 | + public void shutdown() { |
| 117 | + executor.shutdownNow(); // Stop all threads |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### App.java |
| 123 | + |
| 124 | +```java |
| 125 | +public class App { |
| 126 | + public static void main(String[] args) { |
| 127 | + ActorSystem system = new ActorSystem(); |
| 128 | + Actor srijan = new ExampleActor(system); |
| 129 | + Actor ansh = new ExampleActor2(system); |
| 130 | + |
| 131 | + system.startActor(srijan); |
| 132 | + system.startActor(ansh); |
| 133 | + ansh.send(new Message("Hello ansh", srijan.getActorId())); |
| 134 | + srijan.send(new Message("Hello srijan!", ansh.getActorId())); |
| 135 | + |
| 136 | + Thread.sleep(1000); // Give time for messages to process |
| 137 | + |
| 138 | + srijan.stop(); // Stop the actor gracefully |
| 139 | + ansh.stop(); |
| 140 | + system.shutdown(); // Stop the actor system |
| 141 | + } |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Verwendung |
| 148 | + |
| 149 | +- Bei der Konstruktion **paralleler oder verteilter Systeme** |
| 150 | +- Wenn **keine veränderlichen Zustände geteilt** werden sollen |
| 151 | +- Wenn **asynchrone, nachrichtenbasierte Kommunikation** benötigt wird |
| 152 | +- Wenn die Komponenten **isoliert und lose gekoppelt** sein sollen. |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +## Tutorials |
| 157 | + |
| 158 | +- [Baeldung – Akka with Java](https://www.baeldung.com/java-akka) |
| 159 | +- [Vaughn Vernon – Reactive Messaging Patterns](https://vaughnvernon.co/?p=1143) |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +## Reale Anwendungen |
| 164 | + |
| 165 | +- [Akka Framework](https://akka.io/) |
| 166 | +- [Concurrency in Erlang und Elixir](https://www.erlang.org/) |
| 167 | +- [Microsoft Orleans](https://learn.microsoft.com/en-us/dotnet/orleans/) |
| 168 | +- JVM-basierte Spiel-Engines und Simulatoren |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## Vor- und Nachteile |
| 173 | + |
| 174 | +### ✅ Benefits |
| 175 | +- Unterstützt hohes Maß an Parellelität |
| 176 | +- Leichte Skalierbarkeit über Zahl der Threads oder Prozessoren. |
| 177 | +- Fehlerisolation und -behebbarkeit. |
| 178 | +- Geordnete Nachrichten in den Aktoren |
| 179 | + |
| 180 | +### ⚠️ Trade-offs |
| 181 | +- Schwierigeres Debugging wegen asynchronen Verhaltens |
| 182 | +- Leichte Performance-Einbußen durch die Nachrichten-Warteschlangen |
| 183 | +- Komplexeres Design als bei einfachem Methodenaufruf |
| 184 | +--- |
| 185 | + |
| 186 | +## Verwandte 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 | +## Quellen |
| 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