From: Kai Moritz Date: Mon, 14 Sep 2020 19:49:58 +0000 (+0200) Subject: The app emitts events for created / deleted users X-Git-Tag: part-1~8 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=39a57180b2b7d2d01ac558d1a375c826260c7119;p=demos%2Fspring%2Fdata-jdbc The app emitts events for created / deleted users --- diff --git a/src/main/java/de/juplo/boot/data/jdbc/Application.java b/src/main/java/de/juplo/boot/data/jdbc/Application.java index d7301e1..956fd6f 100644 --- a/src/main/java/de/juplo/boot/data/jdbc/Application.java +++ b/src/main/java/de/juplo/boot/data/jdbc/Application.java @@ -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); } diff --git a/src/main/java/de/juplo/boot/data/jdbc/UserController.java b/src/main/java/de/juplo/boot/data/jdbc/UserController.java index 4eb1094..177b120 100644 --- a/src/main/java/de/juplo/boot/data/jdbc/UserController.java +++ b/src/main/java/de/juplo/boot/data/jdbc/UserController.java @@ -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 index 0000000..3693ebf --- /dev/null +++ b/src/main/java/de/juplo/boot/data/jdbc/UserEvent.java @@ -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 index 0000000..bdc6cb0 --- /dev/null +++ b/src/main/java/de/juplo/boot/data/jdbc/UserEventListener.java @@ -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 index 0000000..1c4f001 --- /dev/null +++ b/src/main/java/de/juplo/boot/data/jdbc/UserStatus.java @@ -0,0 +1,9 @@ +package de.juplo.boot.data.jdbc; + +public enum UserStatus +{ + CREATED, + LOGIN, + LOGOUT, + DELETED +}