java-design-patterns icon indicating copy to clipboard operation
java-design-patterns copied to clipboard

#2673: Microservice pattern: Polling publisher #3243

Open quantdevv opened this issue 6 months ago • 6 comments

Pull Request

What does this PR do?

This PR introduces a microservice-based architecture that includes a polling-publisher and a subscriber-service communicating via Apache Kafka. It demonstrates an in-memory data flow mechanism using Kafka topics.

The PR includes:

  • Kafka producer REST endpoint for sending messages
  • Kafka consumer that listens automatically and processes messages
  • Spring Boot configuration for both microservices
  • Initial testing and local setup for message flow validation

Fixes #2673

Type of change

  • [x] New feature (non-breaking change)
  • [x] Documentation update

How has this been tested?

  • Manually tested Kafka message publishing via /send endpoint
  • Verified automatic listening and processing in subscriber microservice
  • Logs and terminal output confirm end-to-end communication

Additional context

Both microservices are designed for local Kafka communication using port 9092. Make sure the Kafka broker is running before testing

quantdevv avatar Jun 01 '25 18:06 quantdevv

PR Summary

This PR introduces a microservice-based architecture using Kafka for communication between a polling publisher and a subscriber service. It includes REST endpoints, Kafka producers and consumers, Spring Boot configurations, and testing for message flow validation.

Changes

File Summary
polling-publisher/README.md This file provides a comprehensive guide to the Polling Publisher-Subscriber pattern, explaining its intent, architecture, real-world examples, and implementation using Spring Boot and Kafka. It also covers when to use this pattern, its benefits and trade-offs, and related design patterns.
polling-publisher/etc/polling-publisher.urm.puml New file.
polling-publisher/polling-service/etc/polling-service.urm.puml New file.
polling-publisher/polling-service/pom.xml This file configures the Maven project for the polling service microservice, defining dependencies and build settings. It includes Spring Boot, Kafka, and testing dependencies.
polling-publisher/polling-service/src/main/java/com/iluwatar/polling/App.java This is the main application class for the polling service microservice. It uses Spring Boot annotations to enable the application context and scheduling.
polling-publisher/polling-service/src/main/java/com/iluwatar/polling/DataRepository.java This class defines an in-memory repository for storing and retrieving data. It uses a HashMap for data storage and provides methods for saving, retrieving, deleting, and finding all data.
polling-publisher/polling-service/src/main/java/com/iluwatar/polling/DataSourceService.java This service is responsible for managing data, including adding, retrieving, and removing data. It uses a DataRepository and a scheduler to periodically generate and add new data.
polling-publisher/polling-service/src/main/java/com/iluwatar/polling/KafkaProducer.java This class handles sending messages to Kafka. It uses Spring's KafkaTemplate to publish messages to specified topics.
polling-publisher/polling-service/src/main/java/com/iluwatar/polling/PollingController.java This REST controller provides a /send endpoint for manually sending messages to Kafka and a health check endpoint.
polling-publisher/polling-service/src/main/java/com/iluwatar/polling/PollingScheduler.java This class schedules the data polling and publishing tasks using Spring's @Scheduled annotation. It retrieves data from the DataSourceService and sends it to Kafka.
polling-publisher/polling-service/src/main/resources/application.yml This file contains the Spring Boot configuration for the polling service, including Kafka broker settings, consumer group ID, and port number.
polling-publisher/polling-service/src/test/java/com/iluwatar/polling/AppTest.java This file contains a test case to verify the application context loading.
polling-publisher/polling-service/src/test/java/com/iluwatar/polling/DataRepositoryTest.java This test suite verifies the functionality of the DataRepository class, including saving, retrieving, deleting, and finding all data.
polling-publisher/polling-service/src/test/java/com/iluwatar/polling/DataSourceServiceTest.java This test suite contains test cases for the DataSourceService, covering adding, retrieving, removing, and getting all data.
polling-publisher/pom.xml This is the main pom file for the polling-publisher module. It aggregates the polling-service and subscriber-service modules and defines common dependencies.
polling-publisher/subscriber-service/etc/subscriber-service.urm.puml New file.
polling-publisher/subscriber-service/pom.xml This file configures the Maven project for the subscriber service microservice, defining dependencies and build settings. It includes Spring Boot, Kafka, and testing dependencies.
polling-publisher/subscriber-service/src/main/java/com/iluwatar/subscriber/App.java This is the main application class for the subscriber service microservice. It uses Spring Boot annotations to enable the application context.
polling-publisher/subscriber-service/src/main/java/com/iluwatar/subscriber/KafkaConsumer.java This class consumes messages from Kafka topics. It uses Spring's @KafkaListener annotation to listen for messages on the updates and API topics.
polling-publisher/subscriber-service/src/main/resources/application.yml This file contains the Spring Boot configuration for the subscriber service, including Kafka broker settings, consumer group ID, and port number.
polling-publisher/subscriber-service/src/test/java/com/iluwatar/subscriber/AppTest.java This file contains a test case to verify the application context loading.
pom.xml The polling-publisher module was added to the list of modules.

autogenerated by presubmit.ai

github-actions[bot] avatar Jun 01 '25 18:06 github-actions[bot]

Quality Gate Failed Quality Gate failed

Failed conditions
2 Security Hotspots
79.7% Coverage on New Code (required ≥ 80%)
D Reliability Rating on New Code (required ≥ A)
E Security Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

sonarqubecloud[bot] avatar Jun 01 '25 18:06 sonarqubecloud[bot]

Hi @iluwatar , can you please have a look on this..?

quantdevv avatar Jun 14 '25 19:06 quantdevv

Quality Gate Failed Quality Gate failed

Failed conditions
2 Security Hotspots
75.0% Coverage on New Code (required ≥ 80%)
E Security Rating on New Code (required ≥ A)
D Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

sonarqubecloud[bot] avatar Jul 12 '25 16:07 sonarqubecloud[bot]

Hi @iluwatar can you please have a look..?

quantdevv avatar Jul 15 '25 13:07 quantdevv

This PR is stale because it has been open 60 days with no activity.

github-actions[bot] avatar Sep 16 '25 02:09 github-actions[bot]