Skip to content

Instantly share code, notes, and snippets.

@pawelpluta
Created January 19, 2020 15:55
Show Gist options
  • Save pawelpluta/c5fc87154748c9751321187603c70f4b to your computer and use it in GitHub Desktop.
Save pawelpluta/c5fc87154748c9751321187603c70f4b to your computer and use it in GitHub Desktop.
@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