BAEL-7523: article improvements
This commit is contained in:
parent
138163ed74
commit
e9c06e7339
|
@ -38,6 +38,11 @@
|
||||||
<artifactId>spring-modulith-events-kafka</artifactId>
|
<artifactId>spring-modulith-events-kafka</artifactId>
|
||||||
<version>${spring-modulith-events-kafka.version}</version>
|
<version>${spring-modulith-events-kafka.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.modulith</groupId>
|
||||||
|
<artifactId>spring-modulith-starter-jpa</artifactId>
|
||||||
|
<version>${spring-modulith-events-kafka.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|
|
@ -4,13 +4,14 @@ import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
|
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
|
||||||
|
import org.springframework.kafka.core.KafkaOperations;
|
||||||
import org.springframework.kafka.core.KafkaTemplate;
|
import org.springframework.kafka.core.KafkaTemplate;
|
||||||
import org.springframework.kafka.core.ProducerFactory;
|
import org.springframework.kafka.core.ProducerFactory;
|
||||||
import org.springframework.kafka.core.KafkaOperations;
|
|
||||||
import org.springframework.modulith.events.EventExternalizationConfiguration;
|
import org.springframework.modulith.events.EventExternalizationConfiguration;
|
||||||
import org.springframework.modulith.events.Externalized;
|
|
||||||
import org.springframework.modulith.events.RoutingTarget;
|
import org.springframework.modulith.events.RoutingTarget;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
class EventExternalizationConfig {
|
class EventExternalizationConfig {
|
||||||
|
|
||||||
|
@ -44,6 +45,9 @@ class EventExternalizationConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
record PostPublishedKafkaEvent(String slug, String title) {
|
record PostPublishedKafkaEvent(String slug, String title) {
|
||||||
|
PostPublishedKafkaEvent {
|
||||||
|
Objects.requireNonNull(slug, "Article Slug must not be null!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,7 +18,6 @@ logging.level.org.springframework.orm.jpa: TRACE
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
datasource:
|
datasource:
|
||||||
# url: jdbc:postgresql://localhost:5432/test_db
|
|
||||||
username: test_user
|
username: test_user
|
||||||
password: test_pass
|
password: test_pass
|
||||||
jpa:
|
jpa:
|
||||||
|
@ -26,3 +25,4 @@ spring:
|
||||||
hibernate:
|
hibernate:
|
||||||
dialect: org.hibernate.dialect.PostgreSQLDialect
|
dialect: org.hibernate.dialect.PostgreSQLDialect
|
||||||
hbm2ddl.auto: create
|
hbm2ddl.auto: create
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue