-
-
Save pawelpluta/c5fc87154748c9751321187603c70f4b to your computer and use it in GitHub Desktop.
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
@Component | |
class UserEventListener { | |
private final UserRepository userRepository; | |
private final ObjectMapper objectMapper; | |
UserEventListener(UserRepository userRepository, ObjectMapper objectMapper) { | |
this.userRepository = userRepository; | |
this.objectMapper = objectMapper; | |
} | |
@KafkaListener(topics = "user-data") | |
void userDataListener(String eventData) { | |
UserRegistered event = eventFrom(eventData); | |
userRepository.save(User.empty().after(event)); | |
} | |
private UserRegistered eventFrom(String eventData) { | |
try { | |
return objectMapper.readValue(eventData, UserRegistered.class); | |
} catch (JsonProcessingException e) { | |
throw new RuntimeException("Could not deserialize event", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment