Skip to content

Commit

Permalink
feat: improve event consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Nov 13, 2023
1 parent 4f0ff47 commit b268158
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
27 changes: 24 additions & 3 deletions etc/http/publish_domain_events.http
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Content-Type: application/json
"type": "course.created",
"occurred_on": "2023-11-14 10:00:00",
"attributes": {
"id": "9bd0c98a-92cc-4a56-a5a1-7d40839ddc83",
"id": "c3a11f1d-512e-420b-aeae-e687a3c449aa",
"name": "Demo course",
"duration": "2 days"
}
Expand All @@ -31,8 +31,29 @@ Content-Type: application/json
"type": "course.renamed",
"occurred_on": "2023-11-14 10:00:00",
"attributes": {
"id": "9bd0c98a-92cc-4a56-a5a1-7d40839ddc83",
"name": "heeey 22222"
"id": "7b081a3e-f90e-4efe-a3a5-81e853e89c8b",
"name": "Este es el nombre bueno"
}
},
"meta": {
}
}
}

###
POST http://localhost:8030/domain-events
Content-Type: application/json

{
"eventName": "course.renamed",
"eventRaw": {
"data": {
"id": "{{$random.uuid}}",
"type": "course.renamed",
"occurred_on": "2022-11-14 10:00:00",
"attributes": {
"id": "7b081a3e-f90e-4efe-a3a5-81e853e89c8b",
"name": "Este es el nombre malo"
}
},
"meta": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public BackofficeCourseCreator(BackofficeCourseRepository repository) {
}

public void create(String id, String name, String duration) {
this.repository.save(new BackofficeCourse(id, name, duration));
if (this.repository.search(id).isEmpty()) {
this.repository.save(new BackofficeCourse(id, name, duration));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@
@Primary
@Service
public final class ElasticsearchBackofficeCourseRepository extends ElasticsearchRepository<BackofficeCourse> implements BackofficeCourseRepository {
public ElasticsearchBackofficeCourseRepository(ElasticsearchClient client) {
super(client);
}
public ElasticsearchBackofficeCourseRepository(ElasticsearchClient client) {
super(client);
}

@Override
public void save(BackofficeCourse course) {
persist(course.id(), course.toPrimitives());
}
@Override
public void save(BackofficeCourse course) {
persist(course.id(), course.toPrimitives());
}

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

@Override
public List<BackofficeCourse> searchAll() {
return searchAllInElastic(BackofficeCourse::fromPrimitives);
}

@Override
public List<BackofficeCourse> matching(Criteria criteria) {
return searchByCriteria(criteria, BackofficeCourse::fromPrimitives);
}

@Override
protected String moduleName() {
return "courses";
}
public List<BackofficeCourse> searchAll() {
return searchAllInElastic(BackofficeCourse::fromPrimitives);
}

@Override
public List<BackofficeCourse> matching(Criteria criteria) {
return searchByCriteria(criteria, BackofficeCourse::fromPrimitives);
}

@Override
protected String moduleName() {
return "courses";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ public void consumer(Message message) throws Exception {

try {
subscriberOnMethod.invoke(subscriber, domainEvent);

System.out.println("ACK: Consumed correctly!");
} catch (Exception error) {
handleConsumptionError(message, queue);
System.out.println("Error consuming");

handleConsumptionError(message, queue);
}
}

Expand Down

0 comments on commit b268158

Please sign in to comment.