BAEL-7523: article improvements

This commit is contained in:
emanuel.trandafir 2024-02-26 20:59:07 +01:00
parent 138163ed74
commit e9c06e7339
4 changed files with 50 additions and 3 deletions

View File

@ -38,6 +38,11 @@
<artifactId>spring-modulith-events-kafka</artifactId>
<version>${spring-modulith-events-kafka.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-jpa</artifactId>
<version>${spring-modulith-events-kafka.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -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!");
}
}
}

View File

@ -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);
}
}

View File

@ -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