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 {
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);
}
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
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;
}
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()
return ResponseEntity.notFound().build();
repository.delete(user);
+ publisher.publishEvent(
+ new UserEvent(
+ this,
+ user.getUsername(),
+ DELETED,
+ ZonedDateTime.now(clock)));
return ResponseEntity.ok(user);
}
--- /dev/null
+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;
+ }
+}
--- /dev/null
+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());
+ }
+}
--- /dev/null
+package de.juplo.boot.data.jdbc;
+
+public enum UserStatus
+{
+ CREATED,
+ LOGIN,
+ LOGOUT,
+ DELETED
+}