From: Kai Moritz Date: Mon, 14 Sep 2020 19:49:58 +0000 (+0200) Subject: The app emitts events for created / deleted users X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=0d30fafa6f618d5b4f9818cdc0405214bc19cfc8;p=demos%2Fkafka%2Foutbox The app emitts events for created / deleted users --- 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..3087290 100644 --- a/src/main/java/de/juplo/boot/data/jdbc/UserController.java +++ b/src/main/java/de/juplo/boot/data/jdbc/UserController.java @@ -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 index 0000000..fc8877c --- /dev/null +++ b/src/main/java/de/juplo/boot/data/jdbc/UserEvent.java @@ -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 index 0000000..27dc798 --- /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.type, event.user); + } +}