Skip to content
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

feat: add publisher-subscriber design pattern (#2898) #3223

Merged
merged 13 commits into from
Mar 30, 2025

Conversation

sanurah
Copy link
Contributor

@sanurah sanurah commented Mar 29, 2025

This PR is adding the publisher subscriber design pattern for feature #2898

Copy link

github-actions bot commented Mar 29, 2025

PR Summary

This PR implements the Publisher-Subscriber design pattern. It introduces a Publisher that manages topics and Subscribers that listen to specific topics. The implementation includes classes for Message, Topic, Publisher, and various Subscribers (WeatherSubscriber, DelayedWeatherSubscriber, CustomerSupportSubscriber). Unit tests are also included.

Changes

File Summary
pom.xml The pom.xml file was modified to include the newly added publish-subscribe module.
publish-subscribe/README.md This README file provides a comprehensive explanation of the Publisher-Subscriber design pattern, including real-world examples, a detailed description of the implementation, and usage scenarios. It also includes an architectural diagram.
publish-subscribe/etc/pub-sub.png New file: Architectural diagram for the Publisher-Subscriber pattern.
publish-subscribe/pom.xml This file defines the dependencies for the publish-subscribe module, including junit-jupiter-engine for testing and logback-classic for logging.
publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java This is the main application class demonstrating the Publisher-Subscriber pattern. It creates topics, registers subscribers, publishes messages, and waits for message processing.
publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Message.java This class represents a message that holds the published content. It uses a record to define a simple immutable data structure.
publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Topic.java This class represents a topic with a name and a set of subscribers. It includes methods to add, remove, and publish messages to subscribers.
publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/Publisher.java This interface defines the contract for a Publisher, including methods to register topics and publish messages.
publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/PublisherImpl.java This class implements the Publisher interface, managing a set of topics and publishing messages to the appropriate subscribers.
publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/CustomerSupportSubscriber.java This class implements a subscriber that handles customer support messages. It logs a message when a new support request is received.
publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/DelayedWeatherSubscriber.java This subscriber simulates a delayed processing scenario by introducing a 2-second delay before processing a message.
publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/Subscriber.java This interface defines the contract for a Subscriber, which includes a method to handle incoming messages.
publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/WeatherSubscriber.java This class implements a subscriber that handles weather-related messages. It logs a message when a new weather event is received.
publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/AppTest.java This test class verifies that the main application runs without throwing any exceptions.
publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/LoggerExtension.java This class is a JUnit extension that captures log messages for testing purposes.
publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/model/MessageTest.java This test class contains unit tests for the Message class.
publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/model/TopicTest.java This test class contains unit tests for the Topic class, including adding and removing subscribers.
publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/publisher/PublisherTest.java This test class contains unit tests for the Publisher class, including registering topics and publishing messages.
publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/subscriber/SubscriberTest.java This test class contains unit tests for the Subscriber class, including subscribing to multiple topics, receiving messages, and unsubscribing.

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 (1)
Files Processed (19)
  • pom.xml (1 hunk)
  • publish-subscribe/README.md (1 hunk)
  • publish-subscribe/pom.xml (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/CustomerSupportContent.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Message.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Topic.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/TopicName.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/WeatherContent.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/Publisher.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/PublisherImpl.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/CustomerSupportSubscriber.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/Subscriber.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/WeatherSubscriber.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/LoggerExtension.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/model/MessageTest.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/model/TopicTest.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/publisher/PublisherTest.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/subscriber/SubscriberTest.java (1 hunk)
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 (11)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/CustomerSupportContent.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Message.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Topic.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/TopicName.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/WeatherContent.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/Publisher.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/PublisherImpl.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/CustomerSupportSubscriber.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/Subscriber.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/WeatherSubscriber.java (1 hunk)
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)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/CustomerSupportContent.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Message.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Topic.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/TopicName.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/WeatherContent.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/Publisher.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/PublisherImpl.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/Subscriber.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/WeatherSubscriber.java (1 hunk)
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 (1)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/CustomerSupportSubscriber.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/CustomerSupportSubscriber.java [5-16]

    redundancy: "Redundant logger initialization."

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 (1)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/AppTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/AppTest.java [9-12]

    enhancement: "Add more comprehensive tests to verify core functionality."

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
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)
  • 7fdc594: Merge branch 'master' into master
Files Processed (0)
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 (1)
  • publish-subscribe/pom.xml (1 hunk)
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 (12)
  • publish-subscribe/README.md (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/CustomerSupportContent.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Message.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Topic.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/TopicName.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/WeatherContent.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/Publisher.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/PublisherImpl.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/CustomerSupportSubscriber.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/Subscriber.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/WeatherSubscriber.java (1 hunk)
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 (11)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/CustomerSupportContent.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Message.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/Publisher.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/CustomerSupportSubscriber.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/WeatherSubscriber.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/AppTest.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/LoggerExtension.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/model/MessageTest.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/publisher/PublisherTest.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/subscriber/SubscriberTest.java (1 hunk)
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 (12)
  • publish-subscribe/README.md (1 hunk)
  • publish-subscribe/etc/pub-sub.png (0 hunks)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Topic.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/PublisherImpl.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/CustomerSupportSubscriber.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/DelayedWeatherSubscriber.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/WeatherSubscriber.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/model/MessageTest.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/model/TopicTest.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/publisher/PublisherTest.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/subscriber/SubscriberTest.java (1 hunk)
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 (4)
  • publish-subscribe/README.md (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java (1 hunk)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/DelayedWeatherSubscriber.java (1 hunk)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/subscriber/SubscriberTest.java (1 hunk)
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 (1)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/subscriber/SubscriberTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • publish-subscribe/src/test/java/com/iluwatar/publish/subscribe/subscriber/SubscriberTest.java [175-181]

    enhancement: "Improve waiting mechanism in tests."

@sanurah sanurah requested a review from iluwatar March 30, 2025 12:50
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 (1)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/DelayedWeatherSubscriber.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/DelayedWeatherSubscriber.java [28-36]

    performance: "Performance impact of artificial delay."

@iluwatar iluwatar merged commit 4185571 into iluwatar:master Mar 30, 2025
2 checks passed
@iluwatar
Copy link
Owner

Looks good! Thank you for the contribution 🎉

@all-contributors please add @sanurah for code

Copy link
Contributor

@iluwatar

I've put up a pull request to add @sanurah! 🎉

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.

None yet

2 participants