diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/controller/NotificationController.java b/booklore-api/src/main/java/com/adityachandel/booklore/controller/NotificationController.java index 6494c7d38..091623f6a 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/controller/NotificationController.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/controller/NotificationController.java @@ -1,9 +1,9 @@ package com.adityachandel.booklore.controller; +import com.adityachandel.booklore.model.stomp.Topic; import com.adityachandel.booklore.service.NotificationService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.MessageMapping; -import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.stereotype.Controller; @Controller @@ -28,7 +28,7 @@ public class NotificationController { System.out.println("Received message: " + message); } - public void sendMessageToTopic(String topic, Object message) { + public void sendMessageToTopic(Topic topic, Object message) { notificationService.sendMessage(topic, message); } } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/stomp/LogNotification.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/stomp/LogNotification.java new file mode 100644 index 000000000..245cc28f3 --- /dev/null +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/stomp/LogNotification.java @@ -0,0 +1,21 @@ +package com.adityachandel.booklore.model.stomp; + +import lombok.Getter; + +import java.time.Instant; + + +@Getter +public class LogNotification { + + private final Instant timestamp = Instant.now(); + private final String message; + + private LogNotification(String message) { + this.message = message; + } + + public static LogNotification createLogNotification(String message) { + return new LogNotification(message); + } +} diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/stomp/Topic.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/stomp/Topic.java new file mode 100644 index 000000000..814be9179 --- /dev/null +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/stomp/Topic.java @@ -0,0 +1,16 @@ +package com.adityachandel.booklore.model.stomp; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public enum Topic { + BOOK("/topic/book"), + LOG("/topic/log"); + + private final String path; + + @Override + public String toString() { + return path; + } +} \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryProcessingService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryProcessingService.java index d35b981fd..ffea47aec 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryProcessingService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryProcessingService.java @@ -7,6 +7,8 @@ import com.adityachandel.booklore.model.entity.Book; import com.adityachandel.booklore.model.entity.Library; import com.adityachandel.booklore.model.enums.BookFileType; import com.adityachandel.booklore.model.stomp.BookNotification; +import com.adityachandel.booklore.model.stomp.LogNotification; +import com.adityachandel.booklore.model.stomp.Topic; import com.adityachandel.booklore.repository.BookRepository; import com.adityachandel.booklore.repository.LibraryRepository; import lombok.AllArgsConstructor; @@ -24,6 +26,7 @@ import java.util.stream.Collectors; import static com.adityachandel.booklore.model.stomp.BookNotification.Action.BOOKS_REMOVED; import static com.adityachandel.booklore.model.stomp.BookNotification.Action.BOOK_ADDED; +import static com.adityachandel.booklore.model.stomp.LogNotification.createLogNotification; @Service @AllArgsConstructor @@ -39,15 +42,19 @@ public class LibraryProcessingService { @Transactional public void processLibrary(long libraryId) throws IOException { Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); + notificationService.sendMessage(Topic.LOG, createLogNotification("Started processing library: " + library.getName())); List libraryFiles = getLibraryFiles(library); processLibraryFiles(libraryFiles); + notificationService.sendMessage(Topic.LOG, createLogNotification("Finished processing library: " + library.getName())); } @Transactional public void refreshLibrary(long libraryId) throws IOException { Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); + notificationService.sendMessage(Topic.LOG, createLogNotification("Started refreshing library: " + library.getName())); processLibraryFiles(getUnProcessedFiles(library)); deleteRemovedBooks(getRemovedBooks(library)); + notificationService.sendMessage(Topic.LOG, createLogNotification("Finished refreshing library: " + library.getName())); } @Transactional @@ -56,7 +63,7 @@ public class LibraryProcessingService { Set bookIds = removedBooks.stream().map(Book::getId).collect(Collectors.toSet()); bookRepository.deleteByIdIn(bookIds); BookNotification notification = BookNotification.builder().action(BOOKS_REMOVED).removedBookIds(bookIds).build(); - notificationService.sendMessage("/topic/books", notification); + notificationService.sendMessage(Topic.BOOK, notification); log.info("Books removed: {}", bookIds); } } @@ -68,8 +75,8 @@ public class LibraryProcessingService { BookDTO bookDTO = processLibraryFile(libraryFile); if (bookDTO != null) { BookNotification notification = BookNotification.builder().action(BOOK_ADDED).addedBook(bookDTO).build(); - notificationService.sendMessage("/topic/books", notification); - notificationService.sendMessage("/topic/logs", "Book added: " + bookDTO.getFileName()); + notificationService.sendMessage(Topic.BOOK, notification); + notificationService.sendMessage(Topic.LOG, createLogNotification("Book added: " + bookDTO.getFileName())); log.info("Processed file: {}", libraryFile.getFilePath()); } } @@ -130,7 +137,4 @@ public class LibraryProcessingService { } return libraryFiles; } - - -} - +} \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/NotificationService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/NotificationService.java index 4a847c933..34697a915 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/NotificationService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/NotificationService.java @@ -1,5 +1,6 @@ package com.adityachandel.booklore.service; +import com.adityachandel.booklore.model.stomp.Topic; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Service; @@ -12,7 +13,7 @@ public class NotificationService { this.messagingTemplate = messagingTemplate; } - public void sendMessage(String topic, Object message) { - messagingTemplate.convertAndSend(topic, message); + public void sendMessage(Topic topic, Object message) { + messagingTemplate.convertAndSend(String.valueOf(topic), message); } } diff --git a/booklore-ui/src/app/app.component.ts b/booklore-ui/src/app/app.component.ts index e823cb725..3ce8ce61c 100644 --- a/booklore-ui/src/app/app.component.ts +++ b/booklore-ui/src/app/app.component.ts @@ -4,6 +4,7 @@ import {Message} from '@stomp/stompjs'; import {BookService} from './book/service/book.service'; import {Action, parseBookNotification} from './book/model/book-notification.model'; import {EventService} from './book/service/event.service'; +import {parseLogNotification} from './book/model/log-notification.model'; @Component({ selector: 'app-root', @@ -17,7 +18,7 @@ export class AppComponent implements OnInit { } ngOnInit(): void { - this.rxStompService.watch('/topic/books').subscribe((message: Message) => { + this.rxStompService.watch('/topic/book').subscribe((message: Message) => { const notification = parseBookNotification(message.body); if (notification.action === Action.BOOK_ADDED) { this.bookService.handleNewlyCreatedBook(notification.addedBook!); @@ -26,8 +27,9 @@ export class AppComponent implements OnInit { } }); - this.rxStompService.watch('/topic/logs').subscribe((message: Message) => { - this.eventService.handleIncomingEvent(message.body); + this.rxStompService.watch('/topic/log').subscribe((message: Message) => { + let logNotification = parseLogNotification(message.body); + this.eventService.handleIncomingLog(logNotification); }); } diff --git a/booklore-ui/src/app/book/component/books-browser/books-browser.component.html b/booklore-ui/src/app/book/component/books-browser/books-browser.component.html index f00ab8b3a..71f1e8e70 100644 --- a/booklore-ui/src/app/book/component/books-browser/books-browser.component.html +++ b/booklore-ui/src/app/book/component/books-browser/books-browser.component.html @@ -1,6 +1,6 @@
-

{{ (entityType$ | async) }}: {{ (entity$ | async)?.name }}

+

{{ (entityType$ | async) }}: {{ (entity$ | async)?.name }}

- +
- {{ latestEvent }} +

{{ latestEvent.timestamp }}

+

{{ latestEvent.message }}