The app emitts events for created / deleted users
authorKai Moritz <kai@juplo.de>
Mon, 14 Sep 2020 19:49:58 +0000 (21:49 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 24 Oct 2020 08:55:26 +0000 (10:55 +0200)
src/main/java/de/juplo/boot/data/jdbc/UserController.java
src/main/java/de/juplo/boot/data/jdbc/UserEvent.java [new file with mode: 0644]
src/main/java/de/juplo/boot/data/jdbc/UserEventListener.java [new file with mode: 0644]

index 4eb1094..3087290 100644 (file)
@@ -2,6 +2,7 @@ package de.juplo.boot.data.jdbc;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.http.ResponseEntity;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
@@ -10,6 +11,9 @@ import org.springframework.web.util.UriComponents;
 
 import java.time.LocalDateTime;
 
+import static de.juplo.boot.data.jdbc.UserEvent.Type.CREATED;
+import static de.juplo.boot.data.jdbc.UserEvent.Type.DELETED;
+
 @RestController
 @Transactional
 @RequestMapping("/users")
@@ -19,10 +23,15 @@ public class UserController {
 
 
     private final UserRepository repository;
+    private final ApplicationEventPublisher publisher;
 
 
-    public UserController(UserRepository repository) {
+    public UserController(
+            UserRepository repository,
+            ApplicationEventPublisher publisher)
+    {
         this.repository = repository;
+        this.publisher = publisher;
     }
 
 
@@ -33,6 +42,7 @@ public class UserController {
         String sanitizedUsername = UserController.sanitize(username);
         User user = new User(sanitizedUsername, LocalDateTime.now(), false);
         repository.save(user);
+        publisher.publishEvent(new UserEvent(this, CREATED, sanitizedUsername));
         UriComponents uri =
             builder
                 .fromCurrentRequest()
@@ -59,6 +69,7 @@ public class UserController {
             return ResponseEntity.notFound().build();
 
         repository.delete(user);
+        publisher.publishEvent(new UserEvent(this, DELETED, username));
 
         return ResponseEntity.ok(user);
     }
diff --git a/src/main/java/de/juplo/boot/data/jdbc/UserEvent.java b/src/main/java/de/juplo/boot/data/jdbc/UserEvent.java
new file mode 100644 (file)
index 0000000..fc8877c
--- /dev/null
@@ -0,0 +1,20 @@
+package de.juplo.boot.data.jdbc;
+
+import org.springframework.context.ApplicationEvent;
+
+
+public class UserEvent extends ApplicationEvent
+{
+    public enum Type { CREATED, LOGIN, LOGOUT, DELETED }
+
+    final Type type;
+    final String user;
+
+
+    public UserEvent(Object source, Type type, String user)
+    {
+        super(source);
+        this.type = type;
+        this.user = user;
+    }
+}
diff --git a/src/main/java/de/juplo/boot/data/jdbc/UserEventListener.java b/src/main/java/de/juplo/boot/data/jdbc/UserEventListener.java
new file mode 100644 (file)
index 0000000..27dc798
--- /dev/null
@@ -0,0 +1,19 @@
+package de.juplo.boot.data.jdbc;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.event.EventListener;
+import org.springframework.stereotype.Component;
+
+@Component
+public class UserEventListener
+{
+    private static final Logger LOG = LoggerFactory.getLogger(UserEventListener.class);
+
+
+    @EventListener
+    public void onUserEvent(UserEvent event)
+    {
+        LOG.info("{}: {}", event.type, event.user);
+    }
+}