Skip to content

feat: Implement Actor Model pattern with automatic actor ID and communication with loose coupling #3251Actor model #3255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

ssrijan-007-sys
Copy link

Description

This PR enhances the implementation of the Actor Model pattern in Java.

What’s included:

  • Automatic ID assignment to actors (e.g., actor-1, actor-2)
  • Multi-actor interaction with message passing
  • Ansh and Srijan actors demonstrate safe communication
  • Fixes infinite message loop using reply limit logic
  • README.md and etc/ are updated

Closes #3251

Checklist ✅

  • Code follows Google Java Style Guide
  • Code formatted using ./mvnw spotless:apply
  • Includes:
    • src/
    • README.md
    • etc/ diagram
    • pom.xml
  • Module added to parent pom.xml
  • Code builds with ./mvnw clean install

Looking forward to your feedback!

Copy link

github-actions bot commented Apr 15, 2025

PR Summary

This PR implements the Actor Model pattern in Java. It includes automatic actor ID assignment, multi-actor interaction with message passing, safe communication between actors, and fixes an infinite message loop. The implementation uses asynchronous message passing to enable building concurrent and distributed systems. The PR also includes updated documentation and diagrams.

Changes

File Summary
actor-model/README.md This file provides a comprehensive guide to the Actor Model pattern in Java. It includes a detailed explanation, real-world examples, architecture diagrams, and code snippets illustrating the implementation of actors, messages, and the actor system.
actor-model/etc/Actor_Model_UML_Class_Diagram.png New file: UML class diagram visualizing the Actor Model implementation.
actor-model/etc/actor-model.urm.puml New file: PlantUML script for generating the Actor Model UML class diagram.
actor-model/pom.xml New file: Maven project configuration file for the Actor Model module. It includes dependencies for testing and logging.
actor-model/src/main/java/com/iluwatar/actormodel/Actor.java This class defines the Actor interface, which includes methods for sending messages (send), stopping actors (stop), and handling received messages (onReceive).
actor-model/src/main/java/com/iluwatar/actormodel/ActorSystem.java This class implements the ActorSystem, which is responsible for creating, starting, and managing the lifecycle of actors. It uses an executor service for running actors in separate threads.
actor-model/src/main/java/com/iluwatar/actormodel/App.java This class demonstrates the usage of the Actor Model pattern. It creates an ActorSystem, starts two actors, sends messages between them, and then shuts down the system.
actor-model/src/main/java/com/iluwatar/actormodel/ExampleActor.java This class extends the Actor class and implements the onReceive method to handle messages. It logs received messages and sends a reply to the sender.
actor-model/src/main/java/com/iluwatar/actormodel/ExampleActor2.java This class extends the Actor class and implements the onReceive method to handle messages. It logs received messages.
actor-model/src/main/java/com/iluwatar/actormodel/Message.java This class defines the Message object, which is used for communication between actors. It contains the message content and the sender's ID.
actor-model/src/test/java/com/iluwatar/actor/ActorModelTest.java This class contains unit tests for the Actor Model implementation. It tests the main method and message passing between actors.
pom.xml The actor-model module was added to the root pom.xml to include it in the project build.

autogenerated by presubmit.ai

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (5)
Files Processed (11)
  • actor-model/README.md (1 hunk)
  • actor-model/etc/actor-model.urm.puml (1 hunk)
  • actor-model/etc/actor_model-Actor_Model___UML_Class_Diagram.png (0 hunks)
  • actor-model/pom.xml (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/Actor.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/ActorSystem.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/App.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/ExampleActor.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/ExampleActor2.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/Message.java (1 hunk)
  • pom.xml (2 hunks)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (1)
Files Processed (9)
  • actor-model/pom.xml (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/Actor.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/ActorSystem.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/App.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/ExampleActor.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/ExampleActor2.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/Message.java (1 hunk)
  • actor-model/src/test/java/com/iluwatar/actor/ActorModelTest.java (1 hunk)
  • pom.xml (2 hunks)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
  • c0ecf29: test: add test for App.java to increase coverage
Files Processed (1)
  • actor-model/src/test/java/com/iluwatar/actor/ActorModelTest.java (1 hunk)
Actionable Comments (1)
  • actor-model/src/test/java/com/iluwatar/actor/ActorModelTest.java [44-57]

    possible bug: "Improve test coverage for message passing."

Skipped Comments (0)

Comment on lines 44 to 57
public void testMessagePassing() {
ActorSystem system = new ActorSystem();

Actor srijan = new ExampleActor(system);
Actor ansh = new ExampleActor2(system);

system.startActor(srijan);
system.startActor(ansh);

ansh.send(new Message("Hello Srijan", srijan.getActorId()));

// You can improve this later by capturing output or using mocks
assertNotNull(srijan.getActorId());
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test testMessagePassing doesn't verify the actual message passing between actors. It only asserts that actor IDs are not null. Consider adding assertions to check if the messages were correctly received and processed by the actors.

@ssrijan-007-sys
Copy link
Author

✅ Implementation Completed
Implemented the Actor Model pattern with automatic actor ID and loose-coupling message communication.

Added unit tests for all key classes and verified actual message passing logic.

Increased code coverage to 93.9% (from 45.5%).

Fixed Spotless formatting violations.

Build & tests pass locally via mvn clean install and mvn test.

Quality Gate passed on SonarCloud.

Please review the PR when you get a chance. I'm happy to make further adjustments if needed!

@@ -0,0 +1,52 @@
# Actor Model
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.0</version>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the version from the parent pom.xml

<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add maven-assembly-plugin so that we can execute the jar from the command line

*/
package com.iluwatar.actormodel;

public class App {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, above the App class, describe the actor model. What are the main components and interactions? Add commentary how this example implements the pattern.

this.actorSystem = actorSystem;
}

Logger logger = Logger.getLogger(getClass().getName());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lombok's @slf4j annotation for logging

Comment on lines 31 to 34
public Message(String content, String senderId) {
this.content = content;
this.senderId = senderId;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lombok's @AllArgsConstructor

Comment on lines 36 to 42
public String getContent() {
return content;
}

public String getSenderId() {
return senderId;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use @Getter

Comment on lines 55 to 56
// You can improve this later by capturing output or using mocks
assertNotNull(srijan.getActorId());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to implement the message passing test properly

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (1)
Files Processed (8)
  • actor-model/README.md (1 hunk)
  • actor-model/etc/Actor_Model_UML_Class_Diagram.png (0 hunks)
  • actor-model/pom.xml (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/App.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/ExampleActor.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/ExampleActor2.java (1 hunk)
  • actor-model/src/main/java/com/iluwatar/actormodel/Message.java (1 hunk)
  • actor-model/src/test/java/com/iluwatar/actor/ActorModelTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants