Improve notifications

This commit is contained in:
aditya.chandel
2024-12-27 11:14:31 -07:00
parent ea716bfb69
commit 3f13917191
13 changed files with 93 additions and 30 deletions
@@ -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);
}
}
@@ -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);
}
}
@@ -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;
}
}
@@ -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<LibraryFile> 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<Long> 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;
}
}
}
@@ -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);
}
}