-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ralf Ueberfuhr
committed
Dec 19, 2024
1 parent
6342072
commit 241fabc
Showing
14 changed files
with
234 additions
and
7 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
...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 | ||
) { | ||
} |
55 changes: 55 additions & 0 deletions
55
...e-provider/src/main/java/de/sample/schulung/accounts/kafka/CustomerEventRecordMapper.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,55 @@ | ||
package de.sample.schulung.accounts.kafka; | ||
|
||
import de.sample.schulung.accounts.domain.Customer; | ||
import de.sample.schulung.accounts.domain.Customer.CustomerState; | ||
import de.sample.schulung.accounts.domain.events.CustomerCreatedEvent; | ||
import de.sample.schulung.accounts.domain.events.CustomerDeletedEvent; | ||
import de.sample.schulung.accounts.domain.events.CustomerReplacedEvent; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CustomerEventRecordMapper { | ||
|
||
public String map(CustomerState state) { | ||
return switch (state) { | ||
case ACTIVE -> "active"; | ||
case LOCKED -> "locked"; | ||
case DISABLED -> "disabled"; | ||
}; | ||
} | ||
|
||
public CustomerRecord map(Customer customer) { | ||
return new CustomerRecord( | ||
customer.getName(), | ||
customer.getDateOfBirth(), | ||
this.map(customer.getState()) | ||
); | ||
} | ||
|
||
public CustomerEventRecord map(CustomerCreatedEvent event) { | ||
var customer = event.customer(); | ||
return new CustomerEventRecord( | ||
"created", | ||
customer.getUuid(), | ||
this.map(customer) | ||
); | ||
} | ||
|
||
public CustomerEventRecord map(CustomerReplacedEvent event) { | ||
var customer = event.customer(); | ||
return new CustomerEventRecord( | ||
"replaced", | ||
customer.getUuid(), | ||
this.map(customer) | ||
); | ||
} | ||
|
||
public CustomerEventRecord map(CustomerDeletedEvent event) { | ||
return new CustomerEventRecord( | ||
"deleted", | ||
event.uuid(), | ||
null | ||
); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...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,29 @@ | ||
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; | ||
private final CustomerEventRecordMapper mapper; | ||
private final KafkaApplicationConfiguration config; | ||
|
||
@EventListener | ||
public void handleCustomerCreatedEvent(CustomerCreatedEvent event) { | ||
var payload = mapper.map(event); | ||
kafkaTemplate.send( | ||
config.getCustomerEventsTopic(), | ||
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 | ||
) { | ||
} |
16 changes: 16 additions & 0 deletions
16
...ovider/src/main/java/de/sample/schulung/accounts/kafka/KafkaApplicationConfiguration.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,16 @@ | ||
package de.sample.schulung.accounts.kafka; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "application.kafka") | ||
@Getter | ||
@Setter | ||
public class KafkaApplicationConfiguration { | ||
|
||
private String customerEventsTopic = "customer-events"; | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...-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,20 @@ | ||
package de.sample.schulung.accounts.kafka; | ||
|
||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.config.TopicBuilder; | ||
|
||
@Configuration | ||
public class KafkaConfiguration { | ||
|
||
@Bean | ||
public NewTopic customerEventsTopic(KafkaApplicationConfiguration config) { | ||
return TopicBuilder | ||
.name(config.getCustomerEventsTopic()) | ||
.partitions(5) | ||
.replicas(1) | ||
.build(); | ||
} | ||
|
||
} |
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
31 changes: 31 additions & 0 deletions
31
...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,31 @@ | ||
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.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* 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 { | ||
|
||
} |