Skip to content

Commit 22e363e

Browse files
authored
BAEL-9234: Eventuate Tram (#18587)
* BAEL-9234: repackaged springmodulith code * BAEL-9234: refactoring * BAEL-9234: eventuate code samples
1 parent 8b4cf42 commit 22e363e

38 files changed

+574
-67
lines changed

spring-boot-modules/spring-boot-libraries-3/drawio/eventuate-tram.drawio

Lines changed: 134 additions & 0 deletions
Large diffs are not rendered by default.

spring-boot-modules/spring-boot-libraries-3/pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
<groupId>org.springframework.boot</groupId>
1717
<artifactId>spring-boot-starter-data-jpa</artifactId>
1818
</dependency>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
1924
<dependency>
2025
<groupId>org.springframework.kafka</groupId>
2126
<artifactId>spring-kafka</artifactId>
@@ -27,6 +32,17 @@
2732
<version>${postgresql.version}</version>
2833
</dependency>
2934

35+
<dependency>
36+
<groupId>io.eventuate.tram.core</groupId>
37+
<artifactId>eventuate-tram-spring-jdbc-kafka</artifactId>
38+
<version>${eventuate.tram.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>io.eventuate.tram.core</groupId>
42+
<artifactId>eventuate-tram-spring-events</artifactId>
43+
<version>${eventuate.tram.version}</version>
44+
</dependency>
45+
3046
<dependency>
3147
<groupId>org.springframework.modulith</groupId>
3248
<artifactId>spring-modulith-events-api</artifactId>
@@ -84,6 +100,12 @@
84100
<scope>test</scope>
85101
</dependency>
86102

103+
<dependency>
104+
<groupId>net.javacrumbs.json-unit</groupId>
105+
<artifactId>json-unit-assertj</artifactId>
106+
<version>${json-unit-assertj.version}</version>
107+
<scope>test</scope>
108+
</dependency>
87109
<dependency>
88110
<groupId>org.awaitility</groupId>
89111
<artifactId>awaitility</artifactId>
@@ -112,7 +134,23 @@
112134
<postgresql.version>42.3.1</postgresql.version>
113135
<h2.version>2.2.224</h2.version>
114136
<jolokia-support-spring.version>2.2.1</jolokia-support-spring.version>
137+
<eventuate.tram.version>0.36.0.RELEASE</eventuate.tram.version>
138+
<json-unit-assertj.version>3.5.0</json-unit-assertj.version>
115139
</properties>
116140

141+
<build>
142+
<plugins>
143+
<plugin>
144+
<groupId>org.apache.maven.plugins</groupId>
145+
<artifactId>maven-compiler-plugin</artifactId>
146+
<version>3.8.1</version>
147+
<configuration>
148+
<source>21</source>
149+
<target>21</target>
150+
</configuration>
151+
</plugin>
152+
</plugins>
153+
</build>
154+
117155
</project>
118156

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
package com.baeldung.springmodulith;
1+
package com.baeldung;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55

6+
/**
7+
* use the appropriate profile: eventuate|modulith
8+
*/
69
@SpringBootApplication
710
public class Application {
811

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.eventuate.tram;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import com.baeldung.eventuate.tram.domain.CommentService;
11+
import com.baeldung.eventuate.tram.domain.Comment;
12+
13+
@RestController
14+
public class CommentsController {
15+
16+
private final CommentService commentService;
17+
18+
public CommentsController(CommentService service) {
19+
this.commentService = service;
20+
}
21+
22+
@PostMapping("/api/articles/{slug}/comments")
23+
public ResponseEntity<Long> addComment(@RequestBody AddCommentDto dto, @PathVariable String slug) {
24+
Comment comment = new Comment(dto.text(), slug, dto.commentAuthor());
25+
long id = commentService.save(comment);
26+
return ResponseEntity.status(HttpStatus.CREATED)
27+
.body(id);
28+
}
29+
30+
record AddCommentDto(String text, String commentAuthor) {
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.eventuate.tram;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.context.annotation.Import;
5+
6+
import io.eventuate.tram.spring.events.publisher.TramEventsPublisherConfiguration;
7+
import io.eventuate.tram.spring.messaging.producer.jdbc.TramMessageProducerJdbcConfiguration;
8+
9+
@Configuration
10+
@Import({ TramEventsPublisherConfiguration.class, TramMessageProducerJdbcConfiguration.class })
11+
class EventuateConfig {
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.eventuate.tram.domain;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
8+
@Entity
9+
public class Comment {
10+
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.IDENTITY)
13+
private Long id;
14+
private String text;
15+
private String articleSlug;
16+
private String commentAuthor;
17+
18+
public Comment(String text, String articleSlug, String commentAuthor) {
19+
this.text = text;
20+
this.articleSlug = articleSlug;
21+
this.commentAuthor = commentAuthor;
22+
}
23+
24+
Comment() {
25+
}
26+
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public String getText() {
32+
return text;
33+
}
34+
35+
public String getArticleSlug() {
36+
return articleSlug;
37+
}
38+
39+
public String getCommentAuthor() {
40+
return commentAuthor;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.eventuate.tram.domain;
2+
3+
import io.eventuate.tram.events.common.DomainEvent;
4+
5+
record CommentAddedEvent(Long id, String articleSlug) implements DomainEvent {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.eventuate.tram.domain;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface CommentRepository extends JpaRepository<Comment, Long> {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.eventuate.tram.domain;
2+
3+
import static java.util.Collections.singletonList;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.stereotype.Service;
8+
9+
import io.eventuate.tram.events.publisher.DomainEventPublisher;
10+
import jakarta.transaction.Transactional;
11+
12+
@Service
13+
public class CommentService {
14+
15+
private static final Logger log = LoggerFactory.getLogger(CommentService.class);
16+
17+
private final CommentRepository comments;
18+
private final DomainEventPublisher domainEvents;
19+
20+
public CommentService(CommentRepository commentRepository, DomainEventPublisher domainEvents) {
21+
this.comments = commentRepository;
22+
this.domainEvents = domainEvents;
23+
}
24+
25+
@Transactional
26+
public Long save(Comment comment) {
27+
Comment saved = this.comments.save(comment);
28+
log.info("Comment created: {}", saved);
29+
30+
CommentAddedEvent commentAdded = new CommentAddedEvent(saved.getId(), saved.getArticleSlug());
31+
domainEvents.publish("baeldung.comment.added", saved.getId(), singletonList(commentAdded));
32+
return saved.getId();
33+
}
34+
35+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.springmodulith.application.events.orders;
1+
package com.baeldung.spring.modulith.events.orders;
22

33
import java.time.Instant;
44
import java.util.List;

0 commit comments

Comments
 (0)