This commit is contained in:
Viktoria committed 2022-09-01 10:46:56 +02:00
1 parent ff67f31cf3
commit cc068fc3a8
26 files changed
+104 -519

No files matched your search

@@ -1,28 +0,0 @@
package site.easystartup.web.notification.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import site.easystartup.web.notification.domian.Notification;
import java.security.Principal;
import java.util.logging.Logger;
@Controller
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class NotificationController {
private final SimpMessagingTemplate messagingTemplate;
Logger logger = Logger.getGlobal();
@GetMapping("/notify")
public void notification(Principal principal, @Payload Notification notification) {
logger.info("Web socket sent notification to " + principal.getName());
messagingTemplate.convertAndSendToUser(principal.getName(), "queue/notification", notification);
}
}
@@ -1,10 +0,0 @@
package site.easystartup.web.notification.domian;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Notification {
private String text;
}
@@ -1,12 +0,0 @@
package site.easystartup.web.notification.event;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface EventHandler {
}
@@ -1,9 +0,0 @@
package site.easystartup.web.notification.event;
public interface NotificationEvent {
void invoke();
String getRecipient();
String getMessage();
}
@@ -1,36 +0,0 @@
package site.easystartup.web.notification.event;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Component
@Scope("singleton")
public class NotificationEventManager {
private List<NotificationListener> listeners = new ArrayList<>();
public void registerListener(NotificationListener listener) {
listeners.add(listener);
}
public void removeListener(NotificationListener listener) {
listeners.remove(listener);
}
public void notifyListeners(Class<? extends NotificationEvent> event) {
listeners.forEach(listener -> Arrays.stream(listener.getClass().getMethods()).
filter(m -> m.isAnnotationPresent(EventHandler.class)).forEach(method -> {
if (method.getParameterTypes()[0].equals(event.getClass())) {
try {
method.invoke(listener, event);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}));
}
}
@@ -1,4 +0,0 @@
package site.easystartup.web.notification.event;
public interface NotificationListener {
}
@@ -1,22 +0,0 @@
package site.easystartup.web.notification.event.realtime;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import site.easystartup.web.notification.domian.Notification;
import site.easystartup.web.notification.event.EventHandler;
import site.easystartup.web.notification.event.NotificationEvent;
import site.easystartup.web.notification.event.NotificationListener;
import site.easystartup.web.notification.service.NotificationService;
import site.easystartup.web.post.domain.event.RepostEvent;
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class RealTimeListener implements NotificationListener {
private final NotificationService notificationService;
//Обрабатывает все ивенты
@EventHandler
public void onEvent(NotificationEvent event) {
notificationService.sendNotification(
new Notification(event.getMessage()));
}
}
@@ -1,17 +0,0 @@
package site.easystartup.web.notification.service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
import site.easystartup.web.notification.domian.Notification;
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class NotificationService {
private final SimpMessagingTemplate messagingTemplate;
public void sendNotification(Notification notification) {
messagingTemplate.convertAndSendToUser("server", "queue/notification", notification);
}
}