File tree 6 files changed +66
-21
lines changed
actor-model/src/main/java/com/iluwatar/actormodel
6 files changed +66
-21
lines changed Original file line number Diff line number Diff line change 24
24
*/
25
25
package com .iluwatar .actormodel ;
26
26
27
+ import lombok .Getter ;
28
+ import lombok .Setter ;
27
29
import java .util .concurrent .BlockingQueue ;
28
30
import java .util .concurrent .LinkedBlockingQueue ;
29
31
30
32
public abstract class Actor implements Runnable {
33
+
34
+ @ Setter
35
+ @ Getter
36
+ private String actorId ;
31
37
private final BlockingQueue <Message > mailbox = new LinkedBlockingQueue <>();
32
38
private volatile boolean active =
33
39
true ; // always read from main memory and written back to main memory,
@@ -44,7 +50,7 @@ public void stop() {
44
50
45
51
@ Override
46
52
public void run () {
47
- while (active ) {
53
+ while ( active ) {
48
54
try {
49
55
Message message = mailbox .take (); // Wait for a message
50
56
onReceive (message ); // Process it
Original file line number Diff line number Diff line change 24
24
*/
25
25
package com .iluwatar .actormodel ;
26
26
27
+ import java .util .concurrent .ConcurrentHashMap ;
27
28
import java .util .concurrent .ExecutorService ;
28
29
import java .util .concurrent .Executors ;
30
+ import java .util .concurrent .atomic .AtomicInteger ;
29
31
30
32
public class ActorSystem {
31
33
private final ExecutorService executor = Executors .newCachedThreadPool ();
34
+ private final ConcurrentHashMap <String ,Actor > actorRegister = new ConcurrentHashMap <>();
35
+ private final AtomicInteger idCounter = new AtomicInteger (0 );
32
36
33
- public Actor actorOf (Actor actor ) {
37
+ public void startActor (Actor actor ) {
38
+ String actorId = "actor-" + idCounter .incrementAndGet (); //Generate a new and unique ID
39
+ actor .setActorId (actorId ); // assign the actor it's ID
40
+ actorRegister .put (actorId ,actor ); //Register and save the actor with it's ID
34
41
executor .submit (actor ); // Run the actor in a thread
35
- return actor ;
42
+ }
43
+
44
+ public Actor getActorById (String actorId ){
45
+ return actorRegister .get (actorId ); // Find by Id
36
46
}
37
47
38
48
public void shutdown () {
Original file line number Diff line number Diff line change 27
27
public class App {
28
28
public static void main (String [] args ) throws InterruptedException {
29
29
ActorSystem system = new ActorSystem ();
30
- Actor Srijan = system .actorOf (new ExampleActor ());
31
-
32
- /* Actor Srijan = new ExampleActor() ;
33
- system.actorOf(Srijan); this is also acceptable
34
- */
35
-
36
- Srijan .send (new Message ("Hello Actor!" , Srijan ));
37
- Srijan .send (new Message ("Another message" , Srijan ));
30
+ Actor srijan = new ExampleActor (system );
31
+ system .startActor (srijan );
32
+ Actor ansh = new ExampleActor2 (system );
33
+ system .startActor (ansh );
34
+ ansh .send (new Message ("Hello Srijan" , srijan .getActorId ()));
35
+ srijan .send (new Message ("Hello ansh!" , srijan .getActorId ()));
38
36
39
37
Thread .sleep (1000 ); // Give time for messages to process
40
38
41
- Srijan .stop (); // Stop the actor gracefully
39
+ srijan .stop (); // Stop the actor gracefully
40
+ ansh .stop ();
42
41
system .shutdown (); // Stop the actor system
43
42
}
44
43
}
Original file line number Diff line number Diff line change 24
24
*/
25
25
package com .iluwatar .actormodel ;
26
26
27
+ import java .util .logging .Logger ;
28
+
27
29
public class ExampleActor extends Actor {
30
+ private final ActorSystem actorSystem ;
31
+
32
+ public ExampleActor (ActorSystem actorSystem ) {
33
+ this .actorSystem = actorSystem ;
34
+ }
35
+ Logger logger = Logger .getLogger (getClass ().getName ());
36
+
28
37
@ Override
29
38
protected void onReceive (Message message ) {
30
- System .out .println ("Received :" + message .getContent ());
39
+ logger .info ("[" +getActorId ()+ "]" + "Received : " + message .getContent ());
40
+
41
+ Actor sender = actorSystem .getActorById (message .getSenderId ());// sender actor id
42
+ if (sender !=null && !message .getSenderId ().equals (getActorId ())) {
43
+ sender .send (new Message ("I got your message " , getActorId ()));
44
+ }
45
+
31
46
}
32
47
}
Original file line number Diff line number Diff line change
1
+ package com .iluwatar .actormodel ;
2
+
3
+ import java .util .logging .Logger ;
4
+
5
+ public class ExampleActor2 extends Actor {
6
+ private final ActorSystem actorSystem ;
7
+
8
+ public ExampleActor2 (ActorSystem actorSystem ) {
9
+ this .actorSystem = actorSystem ;
10
+ }
11
+ Logger logger = Logger .getLogger (getClass ().getName ());
12
+
13
+ @ Override
14
+ protected void onReceive (Message message ) {
15
+ logger .info ("[" + getActorId ()+"]" + "Received : " +message .getContent ());
16
+ }
17
+ }
Original file line number Diff line number Diff line change 26
26
27
27
public class Message {
28
28
private final String content ;
29
- private final Actor sender ;
29
+ private final String senderId ;
30
30
31
- public Message (String content , Actor sender ) {
31
+ public Message (String content , String senderId ) {
32
32
this .content = content ;
33
- this .sender = sender ;
33
+ this .senderId = senderId ;
34
34
}
35
35
36
- public String getContent () {
37
- return content ;
38
- }
36
+ public String getContent () { return content ; }
39
37
40
- public Actor getSender () {
41
- return sender ;
38
+ public String getSenderId () {
39
+ return senderId ;
42
40
}
43
41
}
You can’t perform that action at this time.
0 commit comments