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.
Merge pull request #3 from ralf-ueberfuhr-ars/feature/consumer
Add Kafka producer.
- Loading branch information
Showing
15 changed files
with
212 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
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
...ervice-provider/src/main/java/de/sample/schulung/accounts/kafka/CustomJsonSerializer.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.accounts.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.JsonSerializer; | ||
|
||
public class CustomJsonSerializer extends JsonSerializer<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 CustomJsonSerializer() { | ||
super(createCustomObjectMapper()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...service-provider/src/main/java/de/sample/schulung/accounts/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.accounts.kafka; | ||
|
||
import java.util.UUID; | ||
|
||
public record CustomerEventRecord( | ||
String eventType, | ||
UUID uuid, | ||
CustomerRecord customer | ||
) { | ||
} |
42 changes: 42 additions & 0 deletions
42
...vice-provider/src/main/java/de/sample/schulung/accounts/kafka/CustomerEventsProducer.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,42 @@ | ||
package de.sample.schulung.accounts.kafka; | ||
|
||
import de.sample.schulung.accounts.domain.events.CustomerCreatedEvent; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.UUID; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CustomerEventsProducer { | ||
|
||
private final KafkaTemplate<UUID, Object> kafkaTemplate; | ||
|
||
@EventListener | ||
public void handleCustomerCreatedEvent(CustomerCreatedEvent event) { | ||
// map event to record | ||
var customer = event.customer(); | ||
var payload = new CustomerEventRecord( | ||
"created", | ||
customer.getUuid(), | ||
new CustomerRecord( | ||
customer.getName(), | ||
customer.getDateOfBirth(), | ||
switch(customer.getState()) { | ||
case ACTIVE -> "active"; | ||
case LOCKED -> "locked"; | ||
case DISABLED -> "disabled"; | ||
} | ||
) | ||
); | ||
// send message | ||
kafkaTemplate.send( | ||
KafkaConstants.CUSTOMER_EVENTS_TOPIC, | ||
event.customer().getUuid(), | ||
payload | ||
); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
account-service-provider/src/main/java/de/sample/schulung/accounts/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,10 @@ | ||
package de.sample.schulung.accounts.kafka; | ||
|
||
import java.time.LocalDate; | ||
|
||
public record CustomerRecord( | ||
String name, | ||
LocalDate birthdate, | ||
String state | ||
) { | ||
} |
34 changes: 34 additions & 0 deletions
34
...-service-provider/src/main/java/de/sample/schulung/accounts/kafka/KafkaConfiguration.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,34 @@ | ||
package de.sample.schulung.accounts.kafka; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.event.ContextRefreshedEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.kafka.config.TopicBuilder; | ||
import org.springframework.kafka.support.JacksonUtils; | ||
|
||
@Configuration | ||
public class KafkaConfiguration { | ||
|
||
// Topic beim Start der Anwendung erzeugen | ||
// -> nur für lokale Tests, NICHT für Produktion | ||
// siehe application.yml: spring.kafka.admin.auto-create | ||
@Bean | ||
public NewTopic customerEventsTopic() { | ||
return TopicBuilder | ||
.name(KafkaConstants.CUSTOMER_EVENTS_TOPIC) | ||
// .partitions(3) | ||
// .replicas(3) | ||
.build(); | ||
} | ||
|
||
@EventListener(ContextRefreshedEvent.class) | ||
public void configureJsonSerializer() { | ||
JacksonUtils | ||
.enhancedObjectMapper() | ||
.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
account-service-provider/src/main/java/de/sample/schulung/accounts/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.accounts.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
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
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
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
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
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
26 changes: 26 additions & 0 deletions
26
...vider/src/test/java/de/sample/schulung/accounts/kafka/AutoConfigureKafkaTemplateMock.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,26 @@ | ||
package de.sample.schulung.accounts.kafka; | ||
|
||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Auto-configures a {@link KafkaTemplate} mock in the test context. | ||
* You can get the mock injected by simply using | ||
* <pre> | ||
* \u0040Autowired | ||
* KafkaTemplate<String, CustomerDto> templateMock; | ||
* </pre> | ||
*/ | ||
@Documented | ||
@Inherited | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@EnableAutoConfiguration(exclude = KafkaAutoConfiguration.class) | ||
@MockBean(KafkaTemplate.class) | ||
public @interface AutoConfigureKafkaTemplateMock { | ||
|
||
} |