这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions spring-boot-modules/spring-boot-libraries-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
Expand All @@ -27,6 +32,17 @@
<version>${postgresql.version}</version>
</dependency>

<dependency>
<groupId>io.eventuate.tram.core</groupId>
<artifactId>eventuate-tram-spring-jdbc-kafka</artifactId>
<version>${eventuate.tram.version}</version>
</dependency>
<dependency>
<groupId>io.eventuate.tram.core</groupId>
<artifactId>eventuate-tram-spring-events</artifactId>
<version>${eventuate.tram.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-events-api</artifactId>
Expand Down Expand Up @@ -84,6 +100,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>${json-unit-assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down Expand Up @@ -112,7 +134,23 @@
<postgresql.version>42.3.1</postgresql.version>
<h2.version>2.2.224</h2.version>
<jolokia-support-spring.version>2.2.1</jolokia-support-spring.version>
<eventuate.tram.version>0.36.0.RELEASE</eventuate.tram.version>
<json-unit-assertj.version>3.5.0</json-unit-assertj.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.baeldung.springmodulith;
package com.baeldung;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* use the appropriate profile: eventuate|modulith
*/
@SpringBootApplication
public class Application {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.baeldung.eventuate.tram;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baeldung.eventuate.tram.domain.CommentService;
import com.baeldung.eventuate.tram.domain.Comment;

@RestController
public class CommentsController {

private final CommentService commentService;

public CommentsController(CommentService service) {
this.commentService = service;
}

@PostMapping("/api/articles/{slug}/comments")
public ResponseEntity<Long> addComment(@RequestBody AddCommentDto dto, @PathVariable String slug) {
Comment comment = new Comment(dto.text(), slug, dto.commentAuthor());
long id = commentService.save(comment);
return ResponseEntity.status(HttpStatus.CREATED)
.body(id);
}

record AddCommentDto(String text, String commentAuthor) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.eventuate.tram;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import io.eventuate.tram.spring.events.publisher.TramEventsPublisherConfiguration;
import io.eventuate.tram.spring.messaging.producer.jdbc.TramMessageProducerJdbcConfiguration;

@Configuration
@Import({ TramEventsPublisherConfiguration.class, TramMessageProducerJdbcConfiguration.class })
class EventuateConfig {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.eventuate.tram.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Comment {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String text;
private String articleSlug;
private String commentAuthor;

public Comment(String text, String articleSlug, String commentAuthor) {
this.text = text;
this.articleSlug = articleSlug;
this.commentAuthor = commentAuthor;
}

Comment() {
}

public Long getId() {
return id;
}

public String getText() {
return text;
}

public String getArticleSlug() {
return articleSlug;
}

public String getCommentAuthor() {
return commentAuthor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.baeldung.eventuate.tram.domain;

import io.eventuate.tram.events.common.DomainEvent;

record CommentAddedEvent(Long id, String articleSlug) implements DomainEvent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.baeldung.eventuate.tram.domain;

import org.springframework.data.jpa.repository.JpaRepository;

public interface CommentRepository extends JpaRepository<Comment, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.baeldung.eventuate.tram.domain;

import static java.util.Collections.singletonList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import io.eventuate.tram.events.publisher.DomainEventPublisher;
import jakarta.transaction.Transactional;

@Service
public class CommentService {

private static final Logger log = LoggerFactory.getLogger(CommentService.class);

private final CommentRepository comments;
private final DomainEventPublisher domainEvents;

public CommentService(CommentRepository commentRepository, DomainEventPublisher domainEvents) {
this.comments = commentRepository;
this.domainEvents = domainEvents;
}

@Transactional
public Long save(Comment comment) {
Comment saved = this.comments.save(comment);
log.info("Comment created: {}", saved);

CommentAddedEvent commentAdded = new CommentAddedEvent(saved.getId(), saved.getArticleSlug());
domainEvents.publish("baeldung.comment.added", saved.getId(), singletonList(commentAdded));
return saved.getId();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.application.events.orders;
package com.baeldung.spring.modulith.application.events.orders;

import java.time.Instant;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.application.events.orders;
package com.baeldung.spring.modulith.application.events.orders;

import java.time.Instant;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.application.events.orders;
package com.baeldung.spring.modulith.application.events.orders;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.application.events.orders;
package com.baeldung.spring.modulith.application.events.orders;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.application.events.rewards;
package com.baeldung.spring.modulith.application.events.rewards;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.baeldung.springmodulith.application.events.rewards;
package com.baeldung.spring.modulith.application.events.rewards;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import com.baeldung.springmodulith.application.events.orders.OrderCompletedEvent;
import com.baeldung.spring.modulith.application.events.orders.OrderCompletedEvent;

@Service
public class LoyaltyPointsService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.events.externalization;
package com.baeldung.spring.modulith.events.externalization;

import static jakarta.persistence.GenerationType.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.events.externalization;
package com.baeldung.spring.modulith.events.externalization;

import org.springframework.modulith.events.Externalized;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.events.externalization;
package com.baeldung.spring.modulith.events.externalization;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.events.externalization;
package com.baeldung.spring.modulith.events.externalization;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.events.externalization;
package com.baeldung.spring.modulith.events.externalization;

import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.springmodulith.events.externalization;
package com.baeldung.spring.modulith.events.externalization;

import org.springframework.modulith.events.Externalized;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.baeldung.springmodulith.events.externalization.infra;
package com.baeldung.spring.modulith.events.externalization.infra;

import org.springframework.context.event.EventListener;
import org.springframework.kafka.core.KafkaOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.event.TransactionalEventListener;
import org.springframework.util.Assert;

import com.baeldung.springmodulith.events.externalization.ArticlePublishedEvent;
import com.baeldung.spring.modulith.events.externalization.ArticlePublishedEvent;

//@Component
// this is used in sections 3 and 4 of tha article
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.baeldung.springmodulith.events.externalization.infra;
package com.baeldung.spring.modulith.events.externalization.infra;

import com.baeldung.springmodulith.events.externalization.ArticlePublishedEvent;
import com.baeldung.spring.modulith.events.externalization.ArticlePublishedEvent;
import org.springframework.modulith.events.CompletedEventPublications;
import org.springframework.modulith.events.IncompleteEventPublications;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: sa
password: password
driver-class-name: org.postgresql.Driver
jpa:
generate-ddl: true
properties:
hibernate:
hbm2ddl.auto: create
kafka:
bootstrap-servers: localhost:9092

logging:
level:
root: INFO
io.eventuate: DEBUG

# not needed for this article:
spring.modulith:
republish-outstanding-events-on-restart: false
events.jdbc.schema-initialization.enabled: false
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ spring.kafka:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
type-mapping: com.baeldung.annotation.events.externalization.producer.ArticlePublished
consumer:
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
auto-offset-reset: earliest

spring.modulith:
republish-outstanding-events-on-restart: true
Expand All @@ -20,6 +16,7 @@ spring:
datasource:
username: test_user
password: test_pass
driver-class-name: org.postgresql.Driver
jpa:
properties:
hibernate:
Expand Down
Loading