From e9c06e73397fbde1819bc3431e9c681f8843e61f Mon Sep 17 00:00:00 2001 From: "emanuel.trandafir" Date: Mon, 26 Feb 2024 20:59:07 +0100 Subject: [PATCH] BAEL-7523: article improvements --- .../spring-boot-libraries-3/pom.xml | 5 +++ .../EventExternalizationConfig.java | 8 +++- .../infra/IncompleteEvents.java | 38 +++++++++++++++++++ .../src/main/resources/application.yml | 2 +- 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/infra/IncompleteEvents.java diff --git a/spring-boot-modules/spring-boot-libraries-3/pom.xml b/spring-boot-modules/spring-boot-libraries-3/pom.xml index 50659ae834..988ce0bafe 100644 --- a/spring-boot-modules/spring-boot-libraries-3/pom.xml +++ b/spring-boot-modules/spring-boot-libraries-3/pom.xml @@ -38,6 +38,11 @@ spring-modulith-events-kafka ${spring-modulith-events-kafka.version} + + org.springframework.modulith + spring-modulith-starter-jpa + ${spring-modulith-events-kafka.version} + org.springframework.boot diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/EventExternalizationConfig.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/EventExternalizationConfig.java index ee508fb261..6555694df9 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/EventExternalizationConfig.java +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/EventExternalizationConfig.java @@ -4,13 +4,14 @@ import org.springframework.boot.autoconfigure.kafka.KafkaProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.kafka.core.DefaultKafkaProducerFactory; +import org.springframework.kafka.core.KafkaOperations; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.core.ProducerFactory; -import org.springframework.kafka.core.KafkaOperations; import org.springframework.modulith.events.EventExternalizationConfiguration; -import org.springframework.modulith.events.Externalized; import org.springframework.modulith.events.RoutingTarget; +import java.util.Objects; + @Configuration class EventExternalizationConfig { @@ -44,6 +45,9 @@ class EventExternalizationConfig { } record PostPublishedKafkaEvent(String slug, String title) { + PostPublishedKafkaEvent { + Objects.requireNonNull(slug, "Article Slug must not be null!"); + } } } diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/infra/IncompleteEvents.java b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/infra/IncompleteEvents.java new file mode 100644 index 0000000000..bf0b96e78b --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/java/com/baeldung/springmodulith/events/externalization/infra/IncompleteEvents.java @@ -0,0 +1,38 @@ +package com.baeldung.springmodulith.events.externalization.infra; + +import com.baeldung.springmodulith.events.externalization.ArticlePublishedEvent; +import org.springframework.modulith.events.CompletedEventPublications; +import org.springframework.modulith.events.IncompleteEventPublications; +import org.springframework.stereotype.Component; + +import java.time.Duration; +import java.time.Instant; + +@Component +class PublicationEvents { + private final IncompleteEventPublications incompleteEvent; + private final CompletedEventPublications completeEvents; + + public PublicationEvents(IncompleteEventPublications incompleteEvent, CompletedEventPublications completeEvents) { + this.incompleteEvent = incompleteEvent; + this.completeEvents = completeEvents; + } + + public void resubmitUnpublishedEvents() { + incompleteEvent.resubmitIncompletePublicationsOlderThan(Duration.ofSeconds(60)); + + // or + incompleteEvent.resubmitIncompletePublications(it -> + it.getPublicationDate().isBefore(Instant.now().minusSeconds(60)) + && it.getEvent() instanceof ArticlePublishedEvent); + } + + public void clearPublishedEvents() { + completeEvents.deletePublicationsOlderThan(Duration.ofSeconds(60)); + + // or + completeEvents.deletePublications(it -> + it.getPublicationDate().isBefore(Instant.now().minusSeconds(60)) + && it.getEvent() instanceof ArticlePublishedEvent); + } +} diff --git a/spring-boot-modules/spring-boot-libraries-3/src/main/resources/application.yml b/spring-boot-modules/spring-boot-libraries-3/src/main/resources/application.yml index 9885be68b4..c6797b57d0 100644 --- a/spring-boot-modules/spring-boot-libraries-3/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-libraries-3/src/main/resources/application.yml @@ -18,7 +18,6 @@ logging.level.org.springframework.orm.jpa: TRACE spring: datasource: -# url: jdbc:postgresql://localhost:5432/test_db username: test_user password: test_pass jpa: @@ -26,3 +25,4 @@ spring: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect hbm2ddl.auto: create +