From 6c6491387e40bfc6d142d3203ad6bb2154b6f235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz> Date: Sat, 5 Apr 2025 20:21:50 +0200 Subject: [PATCH] Listener Resolves: # --- README.md | 36 +++++++++++++++++++ pom.xml | 9 ----- .../example/library/BookEventListener.java | 5 +++ .../example/library/BookEventRegistry.java | 5 +++ .../com/example/library/BookEventService.java | 22 ++++++++++++ .../java/com/example/library/BookSender.java | 21 ----------- .../library/BookeEventRegistryImpl.java | 13 +++++++ .../com/example/library/config/JmsConfig.java | 15 -------- src/main/resources/application.properties | 3 +- .../example/library/LibraryServiceTest.java | 0 10 files changed, 83 insertions(+), 46 deletions(-) create mode 100644 README.md create mode 100644 src/main/java/com/example/library/BookEventListener.java create mode 100644 src/main/java/com/example/library/BookEventRegistry.java create mode 100644 src/main/java/com/example/library/BookEventService.java delete mode 100644 src/main/java/com/example/library/BookSender.java create mode 100644 src/main/java/com/example/library/BookeEventRegistryImpl.java delete mode 100644 src/main/java/com/example/library/config/JmsConfig.java create mode 100644 src/test/java/com/example/library/LibraryServiceTest.java diff --git a/README.md b/README.md new file mode 100644 index 0000000..b1f1f0f --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# 📚 Bookworm Library + +Jednoduchá Java aplikace pro správu knihovny – REST API, frontend a event-driven architektura pomocà Spring Boot a JMS (ActiveMQ Artemis). + +--- + +## 🚀 Funkce + +- ✅ Přidánà knihy (REST + GUI) +- ✅ Půjčenà a vrácenà knihy +- ✅ Zobrazenà seznamu knih (Thymeleaf) +- ✅ Event-driven reakce na vytvořenà knihy +- ✅ In-memory databáze (H2) +- ✅ Embedded JMS (ActiveMQ Artemis) +- ✅ Swagger dokumentace + +--- + +## 🔧 Technologie + +- Java 21 +- Spring Boot 3.2+ +- Spring Web, JPA, Thymeleaf, JMS +- H2 database +- Mockito, JUnit 5 + +--- + +## ▶️ Jak aplikaci spustit + +1. **Klonuj nebo rozbal projekt** +2. Ujisti se, že máš Javu 21+ +3. V root složce spusť: + +```bash +./mvnw spring-boot:run \ No newline at end of file diff --git a/pom.xml b/pom.xml index 44fc8a0..f17bb3f 100644 --- a/pom.xml +++ b/pom.xml @@ -64,16 +64,7 @@ <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-activemq</artifactId> - </dependency> - <dependency> - <groupId>org.apache.activemq</groupId> - <artifactId>activemq-broker</artifactId> - <version>5.17.6</version> <!-- aktuálnà verze k dubnu 2025 --> - </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> diff --git a/src/main/java/com/example/library/BookEventListener.java b/src/main/java/com/example/library/BookEventListener.java new file mode 100644 index 0000000..5855e50 --- /dev/null +++ b/src/main/java/com/example/library/BookEventListener.java @@ -0,0 +1,5 @@ +package com.example.library; + +public interface BookEventListener { + void onBookCreated(Book book); +} diff --git a/src/main/java/com/example/library/BookEventRegistry.java b/src/main/java/com/example/library/BookEventRegistry.java new file mode 100644 index 0000000..e61fce5 --- /dev/null +++ b/src/main/java/com/example/library/BookEventRegistry.java @@ -0,0 +1,5 @@ +package com.example.library; + +public interface BookEventRegistry { + void registerListener(BookEventListener listener); +} diff --git a/src/main/java/com/example/library/BookEventService.java b/src/main/java/com/example/library/BookEventService.java new file mode 100644 index 0000000..6a76959 --- /dev/null +++ b/src/main/java/com/example/library/BookEventService.java @@ -0,0 +1,22 @@ +package com.example.library; + +import jakarta.annotation.PostConstruct; +import org.springframework.stereotype.Service; + +@Service +public class BookEventService { + + private final BookEventRegistry registry; + + public BookEventService(BookEventRegistry registry) { + this.registry = registry; + } + + @PostConstruct + public void register() { + registry.registerListener(book -> { + // např. logika ukládánà do DB + System.out.println("Book created: " + book.getTitle()); + }); + } +} diff --git a/src/main/java/com/example/library/BookSender.java b/src/main/java/com/example/library/BookSender.java deleted file mode 100644 index 867352d..0000000 --- a/src/main/java/com/example/library/BookSender.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.library; - -import jakarta.jms.Queue; -import org.springframework.jms.core.JmsTemplate; -import org.springframework.stereotype.Service; - -@Service -public class BookSender { - - private final JmsTemplate jmsTemplate; - private final Queue bookQueue; - - public BookSender(JmsTemplate jmsTemplate, Queue bookQueue) { - this.jmsTemplate = jmsTemplate; - this.bookQueue = bookQueue; - } - - public void sendBook(Book book) { - jmsTemplate.convertAndSend(bookQueue, book); - } -} \ No newline at end of file diff --git a/src/main/java/com/example/library/BookeEventRegistryImpl.java b/src/main/java/com/example/library/BookeEventRegistryImpl.java new file mode 100644 index 0000000..b9c1ff6 --- /dev/null +++ b/src/main/java/com/example/library/BookeEventRegistryImpl.java @@ -0,0 +1,13 @@ +package com.example.library; + +import org.springframework.stereotype.Service; + +@Service +public class BookeEventRegistryImpl implements BookEventRegistry { + + + @Override + public void registerListener(BookEventListener listener) { + + } +} diff --git a/src/main/java/com/example/library/config/JmsConfig.java b/src/main/java/com/example/library/config/JmsConfig.java deleted file mode 100644 index dd7afd0..0000000 --- a/src/main/java/com/example/library/config/JmsConfig.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.example.library.config; - -import jakarta.jms.Queue; -import org.apache.activemq.command.ActiveMQQueue; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class JmsConfig { - - @Bean - public Queue bookQueue() { - return new ActiveMQQueue("bookQueue"); - } -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6588bd0..bd4ac64 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,4 +5,5 @@ spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.hibernate.ddl-auto=update -spring.h2.console.enabled=true \ No newline at end of file +spring.h2.console.enabled=true + diff --git a/src/test/java/com/example/library/LibraryServiceTest.java b/src/test/java/com/example/library/LibraryServiceTest.java new file mode 100644 index 0000000..e69de29 -- GitLab