Skip to content

Commit b268158

Browse files
committed
feat: improve event consumption
1 parent 4f0ff47 commit b268158

4 files changed

Lines changed: 52 additions & 25 deletions

File tree

etc/http/publish_domain_events.http

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Content-Type: application/json
99
"type": "course.created",
1010
"occurred_on": "2023-11-14 10:00:00",
1111
"attributes": {
12-
"id": "9bd0c98a-92cc-4a56-a5a1-7d40839ddc83",
12+
"id": "c3a11f1d-512e-420b-aeae-e687a3c449aa",
1313
"name": "Demo course",
1414
"duration": "2 days"
1515
}
@@ -31,8 +31,29 @@ Content-Type: application/json
3131
"type": "course.renamed",
3232
"occurred_on": "2023-11-14 10:00:00",
3333
"attributes": {
34-
"id": "9bd0c98a-92cc-4a56-a5a1-7d40839ddc83",
35-
"name": "heeey 22222"
34+
"id": "7b081a3e-f90e-4efe-a3a5-81e853e89c8b",
35+
"name": "Este es el nombre bueno"
36+
}
37+
},
38+
"meta": {
39+
}
40+
}
41+
}
42+
43+
###
44+
POST http://localhost:8030/domain-events
45+
Content-Type: application/json
46+
47+
{
48+
"eventName": "course.renamed",
49+
"eventRaw": {
50+
"data": {
51+
"id": "{{$random.uuid}}",
52+
"type": "course.renamed",
53+
"occurred_on": "2022-11-14 10:00:00",
54+
"attributes": {
55+
"id": "7b081a3e-f90e-4efe-a3a5-81e853e89c8b",
56+
"name": "Este es el nombre malo"
3657
}
3758
},
3859
"meta": {

src/backoffice/main/tv/codely/backoffice/courses/application/create/BackofficeCourseCreator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public BackofficeCourseCreator(BackofficeCourseRepository repository) {
1313
}
1414

1515
public void create(String id, String name, String duration) {
16-
this.repository.save(new BackofficeCourse(id, name, duration));
16+
if (this.repository.search(id).isEmpty()) {
17+
this.repository.save(new BackofficeCourse(id, name, duration));
18+
}
1719
}
1820
}

src/backoffice/main/tv/codely/backoffice/courses/infrastructure/persistence/ElasticsearchBackofficeCourseRepository.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,32 @@
1414
@Primary
1515
@Service
1616
public final class ElasticsearchBackofficeCourseRepository extends ElasticsearchRepository<BackofficeCourse> implements BackofficeCourseRepository {
17-
public ElasticsearchBackofficeCourseRepository(ElasticsearchClient client) {
18-
super(client);
19-
}
17+
public ElasticsearchBackofficeCourseRepository(ElasticsearchClient client) {
18+
super(client);
19+
}
2020

21-
@Override
22-
public void save(BackofficeCourse course) {
23-
persist(course.id(), course.toPrimitives());
24-
}
21+
@Override
22+
public void save(BackofficeCourse course) {
23+
persist(course.id(), course.toPrimitives());
24+
}
2525

2626
@Override
2727
public Optional<BackofficeCourse> search(String id) {
2828
return this.searchById(id, BackofficeCourse::fromPrimitives);
2929
}
3030

3131
@Override
32-
public List<BackofficeCourse> searchAll() {
33-
return searchAllInElastic(BackofficeCourse::fromPrimitives);
34-
}
35-
36-
@Override
37-
public List<BackofficeCourse> matching(Criteria criteria) {
38-
return searchByCriteria(criteria, BackofficeCourse::fromPrimitives);
39-
}
40-
41-
@Override
42-
protected String moduleName() {
43-
return "courses";
44-
}
32+
public List<BackofficeCourse> searchAll() {
33+
return searchAllInElastic(BackofficeCourse::fromPrimitives);
34+
}
35+
36+
@Override
37+
public List<BackofficeCourse> matching(Criteria criteria) {
38+
return searchByCriteria(criteria, BackofficeCourse::fromPrimitives);
39+
}
40+
41+
@Override
42+
protected String moduleName() {
43+
return "courses";
44+
}
4545
}

src/shared/main/tv/codely/shared/infrastructure/bus/event/rabbitmq/RabbitMqDomainEventsConsumer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ public void consumer(Message message) throws Exception {
7070

7171
try {
7272
subscriberOnMethod.invoke(subscriber, domainEvent);
73+
74+
System.out.println("ACK: Consumed correctly!");
7375
} catch (Exception error) {
74-
handleConsumptionError(message, queue);
76+
System.out.println("Error consuming");
77+
78+
handleConsumptionError(message, queue);
7579
}
7680
}
7781

0 commit comments

Comments
 (0)