Skip to content

Commit 9ad06e6

Browse files
feat: update Actor Model implementation with multi-actor logic and loose coupling iluwatar#3251
1 parent dd1dcdd commit 9ad06e6

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

Diff for: actor-model/src/main/java/com/iluwatar/actormodel/Actor.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@
2424
*/
2525
package com.iluwatar.actormodel;
2626

27-
import lombok.Getter;
28-
import lombok.Setter;
2927
import java.util.concurrent.BlockingQueue;
3028
import java.util.concurrent.LinkedBlockingQueue;
29+
import lombok.Getter;
30+
import lombok.Setter;
3131

3232
public abstract class Actor implements Runnable {
3333

34-
@Setter
35-
@Getter
36-
private String actorId;
34+
@Setter @Getter private String actorId;
3735
private final BlockingQueue<Message> mailbox = new LinkedBlockingQueue<>();
3836
private volatile boolean active =
3937
true; // always read from main memory and written back to main memory,
@@ -50,7 +48,7 @@ public void stop() {
5048

5149
@Override
5250
public void run() {
53-
while ( active ) {
51+
while (active) {
5452
try {
5553
Message message = mailbox.take(); // Wait for a message
5654
onReceive(message); // Process it

Diff for: actor-model/src/main/java/com/iluwatar/actormodel/ActorSystem.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@
3131

3232
public class ActorSystem {
3333
private final ExecutorService executor = Executors.newCachedThreadPool();
34-
private final ConcurrentHashMap<String ,Actor > actorRegister = new ConcurrentHashMap<>();
34+
private final ConcurrentHashMap<String, Actor> actorRegister = new ConcurrentHashMap<>();
3535
private final AtomicInteger idCounter = new AtomicInteger(0);
3636

3737
public void startActor(Actor actor) {
38-
String actorId = "actor-" + idCounter.incrementAndGet(); //Generate a new and unique ID
38+
String actorId = "actor-" + idCounter.incrementAndGet(); // Generate a new and unique ID
3939
actor.setActorId(actorId); // assign the actor it's ID
40-
actorRegister.put(actorId,actor); //Register and save the actor with it's ID
40+
actorRegister.put(actorId, actor); // Register and save the actor with it's ID
4141
executor.submit(actor); // Run the actor in a thread
4242
}
4343

44-
public Actor getActorById(String actorId){
44+
public Actor getActorById(String actorId) {
4545
return actorRegister.get(actorId); // Find by Id
4646
}
4747

Diff for: actor-model/src/main/java/com/iluwatar/actormodel/App.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static void main(String[] args) throws InterruptedException {
3131
system.startActor(srijan);
3232
Actor ansh = new ExampleActor2(system);
3333
system.startActor(ansh);
34-
ansh.send(new Message("Hello Srijan" , srijan.getActorId()));
34+
ansh.send(new Message("Hello Srijan", srijan.getActorId()));
3535
srijan.send(new Message("Hello ansh!", srijan.getActorId()));
3636

3737
Thread.sleep(1000); // Give time for messages to process

Diff for: actor-model/src/main/java/com/iluwatar/actormodel/ExampleActor.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@
2727
import java.util.logging.Logger;
2828

2929
public class ExampleActor extends Actor {
30-
private final ActorSystem actorSystem;
30+
private final ActorSystem actorSystem;
3131

3232
public ExampleActor(ActorSystem actorSystem) {
3333
this.actorSystem = actorSystem;
3434
}
35+
3536
Logger logger = Logger.getLogger(getClass().getName());
3637

3738
@Override
3839
protected void onReceive(Message message) {
39-
logger.info("[" +getActorId()+ "]" + "Received : " + message.getContent());
40+
logger.info("[" + getActorId() + "]" + "Received : " + message.getContent());
4041

41-
Actor sender = actorSystem.getActorById(message.getSenderId());// sender actor id
42-
if(sender!=null && !message.getSenderId().equals(getActorId())) {
42+
Actor sender = actorSystem.getActorById(message.getSenderId()); // sender actor id
43+
if (sender != null && !message.getSenderId().equals(getActorId())) {
4344
sender.send(new Message("I got your message ", getActorId()));
4445
}
45-
4646
}
4747
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.actormodel;
226

327
import java.util.logging.Logger;
428

5-
public class ExampleActor2 extends Actor{
29+
public class ExampleActor2 extends Actor {
630
private final ActorSystem actorSystem;
731

832
public ExampleActor2(ActorSystem actorSystem) {
933
this.actorSystem = actorSystem;
1034
}
35+
1136
Logger logger = Logger.getLogger(getClass().getName());
1237

1338
@Override
1439
protected void onReceive(Message message) {
15-
logger.info("[" + getActorId()+"]" + "Received : " +message.getContent());
40+
logger.info("[" + getActorId() + "]" + "Received : " + message.getContent());
1641
}
1742
}

Diff for: actor-model/src/main/java/com/iluwatar/actormodel/Message.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public Message(String content, String senderId) {
3333
this.senderId = senderId;
3434
}
3535

36-
public String getContent() { return content; }
36+
public String getContent() {
37+
return content;
38+
}
3739

3840
public String getSenderId() {
3941
return senderId;

0 commit comments

Comments
 (0)