This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consume customer events in statistics service.
- Loading branch information
Ralf Ueberfuhr
committed
Jun 28, 2024
1 parent
f548945
commit 63a8cc4
Showing
7 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ce-provider/src/main/java/de/sample/schulung/statistics/kafka/CustomJsonDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package de.sample.schulung.statistics.kafka; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import org.springframework.kafka.support.JacksonUtils; | ||
import org.springframework.kafka.support.serializer.JsonDeserializer; | ||
|
||
public class CustomJsonDeserializer extends JsonDeserializer<Object> { | ||
|
||
private static ObjectMapper createCustomObjectMapper() { | ||
final var result = JacksonUtils.enhancedObjectMapper(); | ||
result.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); | ||
result.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||
result.registerModule(new JavaTimeModule()); | ||
return result; | ||
} | ||
|
||
public CustomJsonDeserializer() { | ||
super(createCustomObjectMapper()); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...ice-provider/src/main/java/de/sample/schulung/statistics/kafka/CustomerEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package de.sample.schulung.statistics.kafka; | ||
|
||
import de.sample.schulung.statistics.domain.Customer; | ||
import de.sample.schulung.statistics.domain.CustomersService; | ||
import jakarta.validation.ValidationException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.kafka.support.KafkaHeaders; | ||
import org.springframework.messaging.handler.annotation.Header; | ||
import org.springframework.messaging.handler.annotation.Payload; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class CustomerEventListener { | ||
|
||
private final CustomersService customersService; | ||
|
||
@KafkaListener( | ||
topics = KafkaConstants.CUSTOMER_EVENTS_TOPIC | ||
) | ||
public void consume( | ||
@Payload CustomerEventRecord record, | ||
@Header(KafkaHeaders.RECEIVED_PARTITION) String partition, | ||
@Header(KafkaHeaders.OFFSET) int offset | ||
) { | ||
log.info( | ||
"Received record {} {} (Partition: {}, Offset: {})", | ||
record.eventType(), | ||
record.uuid(), | ||
partition, | ||
offset | ||
); | ||
if(record.eventType() == null) { | ||
return; | ||
} | ||
switch (record.eventType()) { | ||
case "created": | ||
case "updated": | ||
if("active".equals(record.customer().state())) { | ||
var customer = Customer | ||
.builder() | ||
.uuid(record.uuid()) | ||
.dateOfBirth(record.customer().birthdate()) | ||
.build(); | ||
customersService.saveCustomer(customer); | ||
} else { | ||
// TODO wenn "created" / nicht "active" -> kein DB-Zugriff | ||
customersService.deleteCustomer(record.uuid()); | ||
} | ||
break; | ||
case "deleted": | ||
customersService.deleteCustomer(record.uuid()); | ||
break; | ||
default: | ||
throw new ValidationException(); | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...rvice-provider/src/main/java/de/sample/schulung/statistics/kafka/CustomerEventRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.sample.schulung.statistics.kafka; | ||
|
||
import java.util.UUID; | ||
|
||
public record CustomerEventRecord( | ||
String eventType, | ||
UUID uuid, | ||
CustomerRecord customer | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
...cs-service-provider/src/main/java/de/sample/schulung/statistics/kafka/CustomerRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package de.sample.schulung.statistics.kafka; | ||
|
||
import java.time.LocalDate; | ||
|
||
public record CustomerRecord( | ||
LocalDate birthdate, | ||
String state | ||
) { | ||
} |
10 changes: 10 additions & 0 deletions
10
...cs-service-provider/src/main/java/de/sample/schulung/statistics/kafka/KafkaConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.sample.schulung.statistics.kafka; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class KafkaConstants { | ||
|
||
public final String CUSTOMER_EVENTS_TOPIC = "customer-events"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters