Skip to content

Commit c4e9276

Browse files
committed
added two service polling service & publisher service to handle two different operation & prepared initial skeleton
1 parent faf0a12 commit c4e9276

File tree

9 files changed

+386
-37
lines changed

9 files changed

+386
-37
lines changed

Diff for: polling-publisher/README.md

+55-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,61 @@
1-
# Polling Publisher Pattern
1+
# Polling Publisher-Subscriber System
22

3-
## Intent
4-
The Polling Publisher pattern enables a system to periodically check a data source for changes and notify its subscribers when updates occur.
3+
This project implements a **Polling Publisher-Subscriber** system using **Spring Boot** and **Apache Kafka**. It consists of two microservices:
54

6-
## Structure
5+
1. **Publisher Module** → Periodically polls a data source and publishes updates via Kafka.
6+
2. **Subscriber Module** → Listens to Kafka for updates and processes them.
77

8+
## 📌 **Project Structure**
9+
```
10+
polling-publisher-subscriber/
11+
│️— pom.xml (Parent POM)
12+
│️— README.md (This file)
13+
14+
├── publisher-module/
15+
│ ├── src/main/java/com/iluwatar/polling-service/
16+
│ ├── src/main/resources/application.yml
17+
│ ├── pom.xml
18+
│ └── README.md (Polling-specific documentation)
19+
20+
├── subscriber-module/
21+
│ ├── src/main/java/com/iluwatar/subscriber-service/
22+
│ ├── src/main/resources/application.yml
23+
│ ├── pom.xml
24+
│ └── README.md (Subscriber-specific documentation)
25+
```
826

9-
## Example Output
27+
## 🚀 **Tech Stack**
28+
- **Spring Boot** (Microservices)
29+
- **Apache Kafka** (Messaging)
30+
- **Maven** (Build Tool)
1031

32+
## 🛠 **Setup & Running**
33+
### 1️⃣ **Start Kafka & Zookeeper**
34+
If you don’t have Kafka installed, use Docker:
35+
```sh
36+
docker-compose up -d
37+
```
1138

12-
## How to Run
13-
Compile and execute `Main.java`, which initializes the polling service and registers subscribers.
39+
### 2️⃣ **Build the Project**
40+
```sh
41+
mvn clean install
42+
```
43+
44+
### 3️⃣ **Run the Publisher Module**
45+
```sh
46+
mvn spring-boot:run -pl publisher-module
47+
```
48+
49+
### 4️⃣ **Run the Subscriber Module**
50+
```sh
51+
mvn spring-boot:run -pl subscriber-module
52+
```
53+
54+
## 📝 **Endpoints**
55+
| Service | Endpoint | Description |
56+
|---------|----------|-------------|
57+
| Publisher | `GET /publish` | Manually trigger data publishing |
58+
| Subscriber | (Kafka Consumer) | Listens for updates |
59+
60+
## 🛠 **Testing**
61+
You can test Kafka messages using:

Diff for: polling-publisher/polling-service/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# **Publisher(Polling)**
2+
**README.md** (Inside `polling-module/`)
3+
4+
5+
## Publisher Module
6+
7+
This module is responsible for **polling a data source** at regular intervals and publishing updates to Kafka.
8+
9+
## **How It Works**
10+
- Uses a **scheduler** to poll data periodically.
11+
- Sends updates to Kafka
12+
- Exposes an API to manually trigger polling.
13+
14+
## **Project Structure**
15+
16+
```
17+
publisher-module/
18+
│️— src/main/java/com/iluawatar/polling/
19+
| ├── App.java
20+
21+
│️— pom.xml
22+
│️— README.md (This file)
23+
24+
```
25+
26+
## 🛠 **Running the Publisher**
27+
```sh
28+
mvn spring-boot:run
29+
```
30+
31+
## 📝 **Endpoints**
32+
| Method | Endpoint | Description |
33+
|--------|----------|-------------|
34+
| `GET` | `/publish` | Manually trigger data publishing |
35+
36+
## 🛠 **Testing Kafka Output**

Diff for: polling-publisher/polling-service/pom.xml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
5+
6+
The MIT License
7+
Copyright © 2014-2022 Ilkka Seppälä
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
-->
28+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<modelVersion>4.0.0</modelVersion>
30+
<parent>
31+
<groupId>com.iluwatar</groupId>
32+
<artifactId>polling-publisher</artifactId>
33+
<version>1.26.0-SNAPSHOT</version>
34+
</parent>
35+
36+
<groupId>com.iluwatar</groupId>
37+
<artifactId>polling-service</artifactId>
38+
<version>1.26.0-SNAPSHOT</version>
39+
<packaging>pom</packaging>
40+
41+
<dependencyManagement>
42+
<dependencies>
43+
44+
<dependency>
45+
<groupId>org.mockito</groupId>
46+
<artifactId>mockito-core</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-assembly-plugin</artifactId>
57+
<executions>
58+
<execution>
59+
<configuration>
60+
<archive>
61+
<manifest>
62+
<mainClass>com.iluwatar.polling-service.App</mainClass>
63+
</manifest>
64+
</archive>
65+
</configuration>
66+
</execution>
67+
</executions>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
72+
</project>

Diff for: polling-publisher/src/main/java/com/iluwatar/polling-publisher/App.java renamed to polling-publisher/polling-service/src/main/java/com/iluwatar/polling-service/App.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@
2323
* THE SOFTWARE.
2424
*/
2525
package com.iluwatar.polling.publisher;
26-
import lombok.extern.slf4j.Slf4j;
2726

2827
/**
29-
*
28+
* Polling-Publisher pattern paradigm.
3029
*/
31-
@Slf4j
3230
public class App {
3331

3432
private static final String DEFAULT_URL = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/polling-publisher/README.md";
3533

36-
private App() {
37-
executor = Executors.newFixedThreadPool(2);
38-
stopLatch = new CountDownLatch(2);
34+
35+
App() {
36+
37+
}
38+
39+
int sum(int x, int y){
40+
return x+y;
3941
}
4042

4143
/**
@@ -47,11 +49,9 @@ private App() {
4749
public static void main(String[] args) throws InterruptedException {
4850
var app = new App();
4951
try {
50-
52+
System.out.println("checking...");
5153
} finally {
52-
54+
System.out.println("finally-block");
5355
}
5456
}
55-
56-
5757
}

Diff for: polling-publisher/pom.xml

+43-19
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,49 @@
2626
2727
-->
2828
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29-
<modelVersion>4.0.0</modelVersion>
30-
<parent>
29+
<modelVersion>4.0.0</modelVersion>
30+
<parent>
31+
<groupId>com.iluwatar</groupId>
32+
<artifactId>java-design-patterns</artifactId>
33+
<version>1.26.0-SNAPSHOT</version>
34+
</parent>
35+
3136
<groupId>com.iluwatar</groupId>
32-
<artifactId>java-design-patterns</artifactId>
37+
<artifactId>polling-publisher</artifactId>
3338
<version>1.26.0-SNAPSHOT</version>
34-
</parent>
35-
<artifactId>promise</artifactId>
36-
<dependencies>
37-
<dependency>
38-
<groupId>org.junit.jupiter</groupId>
39-
<artifactId>junit-jupiter-engine</artifactId>
40-
<scope>test</scope>
41-
</dependency>
42-
<dependency>
43-
<groupId>org.mockito</groupId>
44-
<artifactId>mockito-core</artifactId>
45-
<scope>test</scope>
46-
</dependency>
47-
</dependencies>
48-
<build>
39+
<packaging>pom</packaging>
40+
41+
42+
<properties>
43+
<java.version>21</java.version>
44+
<spring.boot.version>3.2.0</spring.boot.version>
45+
</properties>
46+
47+
<dependencyManagement>
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-dependencies</artifactId>
52+
<version>${spring.boot.version}</version>
53+
<type>pom</type>
54+
<scope>import</scope>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.junit.jupiter</groupId>
59+
<artifactId>junit-jupiter-engine</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.mockito</groupId>
65+
<artifactId>mockito-core</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
</dependencies>
69+
</dependencyManagement>
70+
71+
<!-- <build>
4972
<plugins>
5073
<plugin>
5174
<groupId>org.apache.maven.plugins</groupId>
@@ -63,5 +86,6 @@
6386
</executions>
6487
</plugin>
6588
</plugins>
66-
</build>
89+
</build> -->
90+
6791
</project>

Diff for: polling-publisher/subscriber-service/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# **Subscriber**
2+
README.md (Inside `subscriber-module/`)
3+
4+
## Subscriber Module
5+
6+
This module **listens** to Kafka topic **`updates_topic`** and processes updates.
7+
8+
## How It Works
9+
- Uses **Spring Kafka** to consume messages.
10+
- Listens
11+
- Processes the received messages.
12+
13+
## Project Structure
14+
```
15+
subscriber-module/
16+
│➜ src/main/java/com/example/subscriber/
17+
| ├── App.java
18+
19+
│➜ pom.xml
20+
│➜ README.md (This file)
21+
```
22+
23+
## Running the Subscriber
24+
```sh
25+
mvn spring-boot:run
26+
```
27+
28+
## Testing
29+
Verify subscriber output:
30+
```sh
31+
mvn spring-boot:run
32+
```
33+
Expected console output:
34+
```
35+
36+
```
37+
38+
---

0 commit comments

Comments
 (0)