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>
Sun, 25 Oct 2020 15:57:38 +0000 (16:57 +0100)
src/main/java/de/juplo/boot/data/jdbc/Application.java
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]
src/main/java/de/juplo/boot/data/jdbc/UserStatus.java [new file with mode: 0644]

index d7301e1..956fd6f 100644 (file)
@@ -4,6 +4,10 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+
+import java.time.Clock;
+
 
 @SpringBootApplication
 public class Application {
@@ -11,6 +15,12 @@ public class Application {
     private final static Logger LOG = LoggerFactory.getLogger(Application.class);
 
 
+    @Bean
+    public Clock clock() {
+      return Clock.systemDefaultZone();
+    }
+
+
     public static void main(String[] args) {
         SpringApplication.run(Application.class, args);
     }
index 4eb1094..177b120 100644 (file)
@@ -2,13 +2,19 @@ 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.*;
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
 import org.springframework.web.util.UriComponents;
 
+import java.time.Clock;
 import java.time.LocalDateTime;
+import java.time.ZonedDateTime;
+
+import static de.juplo.boot.data.jdbc.UserStatus.CREATED;
+import static de.juplo.boot.data.jdbc.UserStatus.DELETED;
 
 @RestController
 @Transactional
@@ -19,10 +25,18 @@ public class UserController {
 
 
     private final UserRepository repository;
+    private final Clock clock;
+    private final ApplicationEventPublisher publisher;
 
 
-    public UserController(UserRepository repository) {
+    public UserController(
+            UserRepository repository,
+            Clock clock,
+            ApplicationEventPublisher publisher)
+    {
         this.repository = repository;
+        this.clock = clock;
+        this.publisher = publisher;
     }
 
 
@@ -33,6 +47,12 @@ public class UserController {
         String sanitizedUsername = UserController.sanitize(username);
         User user = new User(sanitizedUsername, LocalDateTime.now(), false);
         repository.save(user);
+        publisher.publishEvent(
+            new UserEvent(
+                this,
+                sanitizedUsername,
+                CREATED,
+                ZonedDateTime.now(clock)));
         UriComponents uri =
             builder
                 .fromCurrentRequest()
@@ -59,6 +79,12 @@ public class UserController {
             return ResponseEntity.notFound().build();
 
         repository.delete(user);
+        publisher.publishEvent(
+            new UserEvent(
+                this,
+                user.getUsername(),
+                DELETED,
+                ZonedDateTime.now(clock)));
 
         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..3693ebf
--- /dev/null
@@ -0,0 +1,28 @@
+package de.juplo.boot.data.jdbc;
+
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.ToString;
+import org.springframework.context.ApplicationEvent;
+
+import java.time.ZonedDateTime;
+
+
+@Getter
+@EqualsAndHashCode
+@ToString
+public class UserEvent extends ApplicationEvent
+{
+  private final String key;
+  private final UserStatus status;
+  private final ZonedDateTime time;
+
+
+  public UserEvent(Object source, String key, UserStatus status, ZonedDateTime time)
+  {
+    super(source);
+    this.key = key;
+    this.status = status;
+    this.time = time;
+  }
+}
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..bdc6cb0
--- /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.getTime(), event.getStatus(), event.getKey());
+    }
+}
diff --git a/src/main/java/de/juplo/boot/data/jdbc/UserStatus.java b/src/main/java/de/juplo/boot/data/jdbc/UserStatus.java
new file mode 100644 (file)
index 0000000..1c4f001
--- /dev/null
@@ -0,0 +1,9 @@
+package de.juplo.boot.data.jdbc;
+
+public enum UserStatus
+{
+  CREATED,
+  LOGIN,
+  LOGOUT,
+  DELETED
+}