diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/App.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/App.java index ef6785391280..d23e7123dbf4 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/App.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/App.java @@ -1,3 +1,27 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.cleanarchitecture; import lombok.extern.slf4j.Slf4j; diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Cart.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Cart.java index 045ca8d22d5d..c4e65df94845 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Cart.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Cart.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartController.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartController.java index 9cfaa118e807..da93cc2a6d93 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartController.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartController.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä @@ -24,7 +22,6 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ - package com.iluwatar.cleanarchitecture; /** diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartRepository.java index 1463500766d4..844bc48345b4 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartRepository.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryCartRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryCartRepository.java index afdc866d7702..2965cddd57f3 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryCartRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryCartRepository.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryOrderRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryOrderRepository.java index cce7a3818b18..b8a17cd6045a 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryOrderRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryOrderRepository.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryProductRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryProductRepository.java index af052ef14781..c91677feeff5 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryProductRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryProductRepository.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Order.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Order.java index 63505dcf073b..70bf058dc2eb 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Order.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Order.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderController.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderController.java index 36844288d9b0..d61dad322750 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderController.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderController.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderRepository.java index f29b907592e1..4c7276fcb53f 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderRepository.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Product.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Product.java index dd7050ea9e4b..100613872865 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Product.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Product.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ProductRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ProductRepository.java index 6b9324fd5d05..713b62e799bc 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ProductRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ProductRepository.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using ZK framework - * licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ShoppingCartService.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ShoppingCartService.java index 960b1a18b300..cd74aa3145cf 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ShoppingCartService.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ShoppingCartService.java @@ -1,7 +1,5 @@ /* - * This project is licensed under the MIT license. - * Module model-view-viewmodel is using - * ZK framework licensed under LGPL (see lgpl-3.0.txt). + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). * * The MIT License * Copyright © 2014-2022 Ilkka Seppälä diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/package-info.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/package-info.java index 027c4b2dcf0e..7b8142f436ae 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/package-info.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/package-info.java @@ -1,3 +1,27 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ /** * Provides classes and interfaces for the clean architecture pattern implementation. * diff --git a/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/AppTest.java b/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/AppTest.java index e5904f3d0aae..86265d2886b7 100644 --- a/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/AppTest.java +++ b/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/AppTest.java @@ -1,3 +1,27 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.cleanarchitecture; import static org.junit.jupiter.api.Assertions.*; diff --git a/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/CartControllerTest.java b/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/CartControllerTest.java index 17af441718bc..788f4d871377 100644 --- a/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/CartControllerTest.java +++ b/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/CartControllerTest.java @@ -1,3 +1,27 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.cleanarchitecture; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/polling-publisher/README.md b/polling-publisher/README.md new file mode 100644 index 000000000000..10b0a0abb79c --- /dev/null +++ b/polling-publisher/README.md @@ -0,0 +1,47 @@ +# Polling Publisher-Subscriber Microservice Pattern + +This project implements a **Polling Publisher-Subscriber** system using **Spring Boot** and **Apache Kafka**. It consists of two microservices: + +1. **Publisher Service** → Periodically polls a data source and publishes updates via Kafka. +2. **Subscriber Service** → Listens to Kafka for updates and processes them. + +## 📌 **Project Structure** +``` +polling-publisher-subscriber/ +│️— pom.xml (Parent POM) +│️— README.md (This file) +│ +├── polling-service/ +│ ├── src/main/java/com/iluwatar/polling/ +│ ├── src/main/resources/application.yml +│ ├── pom.xml +│ └── README.md (Polling-specific documentation) +│ +├── subscriber-service/ +│ ├── src/main/java/com/iluwatar/subscriber/ +│ ├── src/main/resources/application.yml +│ ├── pom.xml +│ └── README.md (Subscriber-specific documentation) +``` + +## 🚀 **Tech Stack** +- **Spring Boot** (Microservices) +- **Apache Kafka** (Messaging) +- **Maven** (Build Tool) + +## 🛠 **Setup & Running** +### 1️⃣ **Start Kafka & Zookeeper** +If you don’t have Kafka installed, use Docker: +```sh +docker-compose up -d +``` + +### 2️⃣ **Build the Project** +```sh +mvn clean install +``` + +### 3️⃣ **Run Service** +```sh +mvn spring-boot:run +``` diff --git a/polling-publisher/etc/polling-publisher.urm.puml b/polling-publisher/etc/polling-publisher.urm.puml new file mode 100644 index 000000000000..02af47ddf261 --- /dev/null +++ b/polling-publisher/etc/polling-publisher.urm.puml @@ -0,0 +1,2 @@ +@startuml +@enduml \ No newline at end of file diff --git a/polling-publisher/polling-service/README.md b/polling-publisher/polling-service/README.md new file mode 100644 index 000000000000..1f742ddca160 --- /dev/null +++ b/polling-publisher/polling-service/README.md @@ -0,0 +1,41 @@ +# **Publisher(Polling)** +**README.md** (Inside `polling-module/`) + + +## Publisher Microservice + +This module is responsible for **polling a data source** at regular intervals and publishing updates to Kafka. + +## **How It Works** +- Uses a **scheduler** to poll data periodically. +- Sends updates to Kafka +- Exposes an API to manually trigger polling. + +## **Project Structure** + +``` +publisher-module/ +│️— src/main/java/com/iluawatar/polling/ +| ├── App.java +│ ├── DataRepository.java +│ ├── DataSourceService.java +│ ├── KafkaProducer.java +│ ├── PollingController.java +│ └── PollingScheduler.java +│ +│️— pom.xml +│️— README.md (This file) + +``` + +## 🛠 **Running the Publisher** +```sh +mvn spring-boot:run +``` + +## 📝 **Endpoints** +| Method | Endpoint | Description | +|--------|------------------|----------------------------------| +| `GET` | `/health` | check health of polling | +| `POST` | `/send?message=""` | Manually trigger data publishing | + diff --git a/polling-publisher/polling-service/etc/polling-service.urm.puml b/polling-publisher/polling-service/etc/polling-service.urm.puml new file mode 100644 index 000000000000..02af47ddf261 --- /dev/null +++ b/polling-publisher/polling-service/etc/polling-service.urm.puml @@ -0,0 +1,2 @@ +@startuml +@enduml \ No newline at end of file diff --git a/polling-publisher/polling-service/pom.xml b/polling-publisher/polling-service/pom.xml new file mode 100644 index 000000000000..a587a96521fe --- /dev/null +++ b/polling-publisher/polling-service/pom.xml @@ -0,0 +1,59 @@ + + + + 4.0.0 + + com.iluwatar + polling-publisher + 1.26.0-SNAPSHOT + + + polling-service + jar + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + + + com.iluwatar.polling-service.App + + + + + + + + + + diff --git a/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/App.java b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/App.java new file mode 100644 index 000000000000..0b5f9f4d00fa --- /dev/null +++ b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/App.java @@ -0,0 +1,45 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.polling; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +/** Polling-Publisher pattern paradigm. */ +@SpringBootApplication +@EnableScheduling +public class App { + + /** + * Program entry point. + * + * @param args command line args. + */ + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } +} diff --git a/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/DataRepository.java b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/DataRepository.java new file mode 100644 index 000000000000..61483e9dedcf --- /dev/null +++ b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/DataRepository.java @@ -0,0 +1,67 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.polling; + +import java.util.HashMap; +import java.util.Map; +import javax.annotation.PostConstruct; +import org.springframework.stereotype.Repository; + +/** Data repository to keep or store data. */ +@Repository +public class DataRepository { + + private final Map dataStorage = new HashMap<>(); + + /** init after map creation ... to put dummy data. */ + @PostConstruct + public void init() { + // Injecting dummy data at startup + dataStorage.put(2, "Initial Dummy Data - two - 2"); + dataStorage.put(3, "Initial Dummy Data - three - 3"); + dataStorage.put(4, "Initial Dummy Data - four - 4"); + } + + /** Save data to the repository. */ + public void save(int id, String value) { + dataStorage.put(id, value); + } + + /** Retrieve data by ID. */ + public String findById(int id) { + return dataStorage.getOrDefault(id, "Data not found"); + } + + /** Delete data by ID. */ + public void delete(int id) { + dataStorage.remove(id); + } + + /** Get all data. */ + public Map findAll() { + return dataStorage; + } +} diff --git a/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/DataSourceService.java b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/DataSourceService.java new file mode 100644 index 000000000000..a99144640379 --- /dev/null +++ b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/DataSourceService.java @@ -0,0 +1,77 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.polling; + +import java.util.Map; +import java.util.Random; +import org.springframework.stereotype.Service; + +/** This class is responsible for keep the events. */ +@Service +public class DataSourceService { + + private final DataRepository repository; + + /** Constructor & Scheduler to push random data. */ + public DataSourceService(DataRepository repository) { + this.repository = repository; + + // Start a separate thread to add data every 3 seconds + new Thread( + () -> { + Random random = new Random(); + while (true) { + try { + Thread.sleep(3000); // Add data every 3 seconds + int id = random.nextInt(100); // Random ID + String value = "Auto-Data-" + id; + this.addData(id, value); + System.out.println("🔵 Data Added: " + id + " -> " + value); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + break; + } + } + }) + .start(); + } + + public void addData(int id, String value) { + repository.save(id, value); + } + + public String getData(int id) { + return repository.findById(id); + } + + public void removeData(int id) { + repository.delete(id); + } + + public Map getAllData() { + return repository.findAll(); + } +} diff --git a/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/KafkaProducer.java b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/KafkaProducer.java new file mode 100644 index 000000000000..23479f83250a --- /dev/null +++ b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/KafkaProducer.java @@ -0,0 +1,50 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.polling; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.stereotype.Component; + +/** This class is responsible for sending messages to Kafka. */ +@Component +public class KafkaProducer { + + @Autowired private final KafkaTemplate kafkaTemplate; + + public KafkaProducer(KafkaTemplate kafkaTemplate) { + this.kafkaTemplate = kafkaTemplate; + } + + /** + * Sends a message to the Kafka topic. + * + * @param message The message to send. + */ + public void sendMessage(String topic, String message) { + kafkaTemplate.send(topic, message); + } +} diff --git a/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/PollingController.java b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/PollingController.java new file mode 100644 index 000000000000..958da6f59c09 --- /dev/null +++ b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/PollingController.java @@ -0,0 +1,50 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.polling; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** This class is responsible for contolling polling tasks. */ +@RestController +public class PollingController { + + @GetMapping("/health") + public String healthCheck() { + return "Polling Service is up and running!"; + } + + @Autowired private KafkaProducer kafkaProducer; + + @PostMapping("/send") + public String sendMessage(@RequestParam("message") String message) { + kafkaProducer.sendMessage("API", message); + return "Message sent: " + message; + } +} diff --git a/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/PollingScheduler.java b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/PollingScheduler.java new file mode 100644 index 000000000000..f35c05d45b09 --- /dev/null +++ b/polling-publisher/polling-service/src/main/java/com/iluwatar/polling/PollingScheduler.java @@ -0,0 +1,54 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.polling; + +import java.util.Random; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** This class is responsible for scheduling polling tasks. */ +@Component +public class PollingScheduler { + + @Autowired private DataSourceService dataSourceService; + + @Autowired private KafkaProducer kafkaProducer; + + /** Scheduler for poll data on each 5 second. */ + @Scheduled(fixedRate = 5000) // Poll every 5 seconds + public void pollDataSource() { + int id = new Random().nextInt(100); // Pick a random ID + String data = dataSourceService.getData(id); // Get data from service + + if (data != null) { + System.out.println("🟢 Publishing Data: " + data); + kafkaProducer.sendMessage("updates", data); + } else { + System.out.println("🔴 No Data Found for ID: " + id); + } + } +} diff --git a/polling-publisher/polling-service/src/main/resources/application.yml b/polling-publisher/polling-service/src/main/resources/application.yml new file mode 100644 index 000000000000..3ad766e6d017 --- /dev/null +++ b/polling-publisher/polling-service/src/main/resources/application.yml @@ -0,0 +1,36 @@ +# +# This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). +# +# The MIT License +# Copyright © 2014-2022 Ilkka Seppälä +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# + +server: + port: 8081 + +spring: + kafka: + bootstrap-servers: localhost:9092 # Kafka broker address(es) + consumer: + group-id: subscriber-group # Consumer group ID + auto-offset-reset: earliest # How to reset offset on consumer startup + # Add other consumer properties here + diff --git a/polling-publisher/polling-service/src/test/java/com/iluwatar/polling/AppTest.java b/polling-publisher/polling-service/src/test/java/com/iluwatar/polling/AppTest.java new file mode 100644 index 000000000000..d54972662be7 --- /dev/null +++ b/polling-publisher/polling-service/src/test/java/com/iluwatar/polling/AppTest.java @@ -0,0 +1,39 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.polling; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class AppTest { + + @Test + void polling() { + assertDoesNotThrow(() -> App.main(new String[] {})); + } +} diff --git a/polling-publisher/polling-service/src/test/java/com/iluwatar/polling/DataRepositoryTest.java b/polling-publisher/polling-service/src/test/java/com/iluwatar/polling/DataRepositoryTest.java new file mode 100644 index 000000000000..034019462a02 --- /dev/null +++ b/polling-publisher/polling-service/src/test/java/com/iluwatar/polling/DataRepositoryTest.java @@ -0,0 +1,78 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.polling; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class DataRepositoryTest { + + private DataRepository repository; + + @BeforeEach + void setUp() { + repository = new DataRepository(); // Initialize before each test + } + + @Test + void testSaveAndFindById() { + repository.save(1, "Test Data"); + + String result = repository.findById(1); + + assertEquals("Test Data", result, "The retrieved data should match the stored value."); + } + + @Test + void testFindById_NotFound() { + String result = repository.findById(99); + + assertEquals("Data not found", result, "Should return 'Data not found' for missing entries."); + } + + @Test + void testDelete() { + repository.save(2, "To be deleted"); + repository.delete(2); + + String result = repository.findById(2); + + assertEquals("Data not found", result, "Deleted data should not be retrievable."); + } + + @Test + void testFindAll() { + repository.save(1, "First"); + repository.save(2, "Second"); + + Map allData = repository.findAll(); + + assertEquals(2, allData.size(), "The repository should contain two items."); + assertTrue(allData.containsKey(1) && allData.containsKey(2), "Both keys should exist."); + } +} diff --git a/polling-publisher/polling-service/src/test/java/com/iluwatar/polling/DataSourceServiceTest.java b/polling-publisher/polling-service/src/test/java/com/iluwatar/polling/DataSourceServiceTest.java new file mode 100644 index 000000000000..ff03f29585c5 --- /dev/null +++ b/polling-publisher/polling-service/src/test/java/com/iluwatar/polling/DataSourceServiceTest.java @@ -0,0 +1,81 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.polling; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Map; +import org.junit.jupiter.api.*; + +public class DataSourceServiceTest { + + private DataRepository repository; + private DataSourceService service; + + @BeforeEach + void setUp() { + repository = new DataRepository(); + service = new DataSourceService(repository); + } + + @Test + void testAddData() { + service.addData(1, "Test Data"); + + assertEquals("Test Data", repository.findById(1)); + } + + @Test + void testGetData() { + repository.save(1, "Test Data"); + + String result = service.getData(1); + + assertEquals("Test Data", result, "The retrieved data should match."); + } + + @Test + void testRemoveData() { + repository.save(2, "Some Data"); + + service.removeData(2); + + assertEquals( + "Data not found", repository.findById(2), "Deleted data should not be retrievable."); + } + + @Test + void testGetAllData() { + repository.save(1, "First"); + repository.save(2, "Second"); + + Map result = service.getAllData(); + + assertEquals(2, result.size(), "Should return all stored data."); + assertEquals("First", result.get(1), "Value for key 1 should be 'First'."); + assertEquals("Second", result.get(2), "Value for key 2 should be 'Second'."); + } +} diff --git a/polling-publisher/pom.xml b/polling-publisher/pom.xml new file mode 100644 index 000000000000..626aec760dcc --- /dev/null +++ b/polling-publisher/pom.xml @@ -0,0 +1,167 @@ + + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.26.0-SNAPSHOT + + + polling-publisher + pom + + + polling-service + subscriber-service + + + + 21 + 3.4.4 + + + + + + + + + + + + + + + + + + + + + + + + + org.springframework.boot + spring-boot-starter-web + ${spring.boot.version} + + + + org.springframework.boot + spring-boot-starter + ${spring.boot.version} + + + + + org.springframework.boot + spring-boot-starter-test + ${spring.boot.version} + test + + + + + org.springframework.kafka + spring-kafka + 3.3.2 + + + + org.springframework.kafka + spring-kafka-test + 3.3.2 + test + + + + + org.springframework.boot + spring-boot + ${spring.boot.version} + + + + org.mockito + mockito-core + test + + + org.mockito + mockito-inline + 5.2.0 + test + + + + + javax.annotation + javax.annotation-api + 1.3.2 + + + + ch.qos.logback + logback-core + 1.5.18 + + + ch.qos.logback + logback-classic + 1.5.18 + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 3.8.1 + + 21 + 21 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 + + + + + + diff --git a/polling-publisher/subscriber-service/README.md b/polling-publisher/subscriber-service/README.md new file mode 100644 index 000000000000..477e1e235c48 --- /dev/null +++ b/polling-publisher/subscriber-service/README.md @@ -0,0 +1,29 @@ +# **Subscriber** +README.md (Inside `subscriber-module/`) + +## Subscriber Microservice + +This module **listens** to Kafka topic **`updates & API`** and processes data. + +## How It Works +- Uses **Spring Kafka** to consume messages. +- Listens +- Processes the received messages. + +## Project Structure +``` +subscriber-module/ +│➜ src/main/java/com/example/subscriber/ +| ├── App.java +| |-- KafkaConsumer.java +│ +│➜ pom.xml +│➜ README.md (This file) +``` + +## Running the Subscriber +```sh +mvn spring-boot:run +``` + +--- \ No newline at end of file diff --git a/polling-publisher/subscriber-service/etc/subscriber-service.urm.puml b/polling-publisher/subscriber-service/etc/subscriber-service.urm.puml new file mode 100644 index 000000000000..02af47ddf261 --- /dev/null +++ b/polling-publisher/subscriber-service/etc/subscriber-service.urm.puml @@ -0,0 +1,2 @@ +@startuml +@enduml \ No newline at end of file diff --git a/polling-publisher/subscriber-service/pom.xml b/polling-publisher/subscriber-service/pom.xml new file mode 100644 index 000000000000..384f1b66c244 --- /dev/null +++ b/polling-publisher/subscriber-service/pom.xml @@ -0,0 +1,59 @@ + + + + 4.0.0 + + com.iluwatar + polling-publisher + 1.26.0-SNAPSHOT + + + subscriber-service + jar + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + + + com.iluwatar.subscriber-service.App + + + + + + + + + + diff --git a/polling-publisher/subscriber-service/src/main/java/com/iluwatar/subscriber/App.java b/polling-publisher/subscriber-service/src/main/java/com/iluwatar/subscriber/App.java new file mode 100644 index 000000000000..b8dd8a23f4ec --- /dev/null +++ b/polling-publisher/subscriber-service/src/main/java/com/iluwatar/subscriber/App.java @@ -0,0 +1,37 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.subscriber; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** Polling-Publisher pattern paradigm. */ +@SpringBootApplication +public class App { + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } +} diff --git a/polling-publisher/subscriber-service/src/main/java/com/iluwatar/subscriber/KafkaConsumer.java b/polling-publisher/subscriber-service/src/main/java/com/iluwatar/subscriber/KafkaConsumer.java new file mode 100644 index 000000000000..6b45ad559501 --- /dev/null +++ b/polling-publisher/subscriber-service/src/main/java/com/iluwatar/subscriber/KafkaConsumer.java @@ -0,0 +1,44 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.subscriber; + +import org.springframework.kafka.annotation.KafkaListener; +import org.springframework.stereotype.Service; + +/** Kafka consumer service to consume messages / updates. */ +@Service +public class KafkaConsumer { + + @KafkaListener(topics = "updates", groupId = "subscriber-group") + public void listenUpdates(String message) { + System.out.println("[updates]: Received message: " + message); + } + + @KafkaListener(topics = "API", groupId = "subscriber-group") + public void listenApi(String message) { + System.out.println("[API]: Received message from /send : " + message); + } +} diff --git a/polling-publisher/subscriber-service/src/main/resources/application.yml b/polling-publisher/subscriber-service/src/main/resources/application.yml new file mode 100644 index 000000000000..16b8425f39b5 --- /dev/null +++ b/polling-publisher/subscriber-service/src/main/resources/application.yml @@ -0,0 +1,33 @@ +# +# This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). +# +# The MIT License +# Copyright © 2014-2022 Ilkka Seppälä +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# + +server: + port: 8082 + +spring: + kafka: + bootstrap-servers: localhost:9092 # Kafka broker address(es) + consumer: + group-id: subscriber-group \ No newline at end of file diff --git a/polling-publisher/subscriber-service/src/test/java/com/iluwatar/subscriber/AppTest.java b/polling-publisher/subscriber-service/src/test/java/com/iluwatar/subscriber/AppTest.java new file mode 100644 index 000000000000..b03222d435e0 --- /dev/null +++ b/polling-publisher/subscriber-service/src/test/java/com/iluwatar/subscriber/AppTest.java @@ -0,0 +1,40 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.subscriber; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class AppTest { + + @Test + void subscriber() { + assertDoesNotThrow(() -> App.main(new String[] {})); + } +} diff --git a/pom.xml b/pom.xml index 4f5c62663d78..50933db086d4 100644 --- a/pom.xml +++ b/pom.xml @@ -189,6 +189,7 @@ partial-response pipeline poison-pill + polling-publisher presentation-model private-class-data producer-consumer