This project implements a publisher-subscriber notification system using Java and Spring Boot. It allows subscribers to subscribe to topics, receive notifications for subscribed topics, and unsubscribe from topics.
Live Link: https://publishersubscriber.onrender.com/api
Note : The first request to this URL might take up to 50 seconds to respond. This is because I am using a free instance that automatically shuts down after a period of inactivity.
However, after the first request is completed, all subsequent requests will respond normally.
- Clone the repository:
git clone https://github.com/AryanP45/PublisherSubscriber.git-
Open the project in your preferred Java IDE. (IntelliJ IDEA)
-
Build the project using Maven.
Postman Collection: Link to API Collection
The system provides the following APIs:
This API allows subscribers to subscribe to a topic.
Note: Creates a new topic if it doesn't exist.
- Endpoint:
POST /api/subscribe
Try with live link: https://publishersubscriber.onrender.com/api/subscribe - Request Body:
{ "topicId": "string", "subscriberId": "string" } - Responses:
- 200 OK: Successfully subscribed.
- 409 Conflict: Subscriber already subscribed to the topic.
- 500 Internal Server Error: Error occurred while subscribing.
This API sends notifications to all subscribers of a particular topic.
- Endpoint:
GET /api/notify
Try with live link: https://publishersubscriber.onrender.com/api/notify - Query Parameters:
topicId: The ID of the topic to notify subscribers.
- Responses:
- 200 OK: Successfully notified subscribers.
- 404 Not Found: No subscribers have subscribed to the topic or topic not found.
- 500 Internal Server Error: Error occurred while notifying.
This API allows subscribers to unsubscribe from a topic.
- Endpoint:
POST /api/unsubscribe
Try with live link: https://publishersubscriber.onrender.com/api/unsubscribe - Request Body:
{ "topicId": "string", "subscriberId": "string" } - Responses:
- 200 OK: Successfully unsubscribed.
- 404 Not Found: Topic or subscriber not found.
- 500 Internal Server Error: Error occurred while unsubscribing.
This API retrieves all topics along with their subscribers.
- Endpoint:
GET /api/all
Try with live link: https://publishersubscriber.onrender.com/api/all - Responses:
- 200 OK: Successfully retrieved all topics with subscribers.
A driver function has been implemented to demonstrate the flow of the application. Below is a simple example:
public class Driver {
public static void main(String[] args) {
NotificationService notificationService = new NotificationService();
// Subscribe
String subscribeResult = notificationService.subscribe("topic1", "subscriber1");
System.out.println("Subscribe Result: " + subscribeResult);
// Notify
String notifyResult = notificationService.notify("topic1");
System.out.println("Notify Result: " + notifyResult);
// Unsubscribe
String unsubscribeResult = notificationService.unsubscribe("topic1", "subscriber1");
System.out.println("Unsubscribe Result: " + unsubscribeResult);
}
}output:
Subscribe Result: subscribed Successfully
Notification sent to subscriber with ID : subscriber1
Notify Result: Notified 1 Subscribers
Unsubscribe Result: Unsubscribed- Null topic ID or subscriber ID is handled in all APIs.
- API responses provide appropriate error messages for edge cases such as subscriber already subscribed, topic not found, etc.
- Unit tests have been implemented to cover various scenarios, including edge cases.
- Aryan Patil (@aryanp45)
If any query, contact: aryanitinpatil@gmail.com