-
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
1 parent
3b23d1a
commit c681ce2
Showing
7 changed files
with
179 additions
and
2 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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/itsnaveenk/spring_kafka_consumer_elastic/configs/ModelMapperConfig.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,14 @@ | ||
package com.itsnaveenk.spring_kafka_consumer_elastic.configs; | ||
|
||
import org.modelmapper.ModelMapper; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class ModelMapperConfig { | ||
|
||
@Bean | ||
public ModelMapper modelMapper() { | ||
return new ModelMapper(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/itsnaveenk/spring_kafka_consumer_elastic/entity/NotificationEntity.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,53 @@ | ||
package com.itsnaveenk.spring_kafka_consumer_elastic.entity; | ||
|
||
import org.springframework.data.elasticsearch.annotations.Document; | ||
|
||
@Document(indexName = "notification") | ||
public class NotificationEntity { | ||
|
||
private int user_id; | ||
private String message; | ||
private int recipient_id; | ||
|
||
public NotificationEntity() { | ||
} | ||
|
||
public NotificationEntity(int user_id, String message, int recipient_id) { | ||
this.user_id = user_id; | ||
this.message = message; | ||
this.recipient_id = recipient_id; | ||
} | ||
|
||
public int getUser_id() { | ||
return user_id; | ||
} | ||
|
||
public void setUser_id(int user_id) { | ||
this.user_id = user_id; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public int getRecipient_id() { | ||
return recipient_id; | ||
} | ||
|
||
public void setRecipient_id(int recipient_id) { | ||
this.recipient_id = recipient_id; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "NotificationEntity{" + | ||
"user_id=" + user_id + | ||
", message='" + message + '\'' + | ||
", recipient_id=" + recipient_id + | ||
'}'; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/itsnaveenk/spring_kafka_consumer_elastic/repository/ElasticRepository.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,13 @@ | ||
package com.itsnaveenk.spring_kafka_consumer_elastic.repository; | ||
|
||
|
||
import com.itsnaveenk.spring_kafka_consumer_elastic.entity.NotificationEntity; | ||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface ElasticRepository extends ElasticsearchRepository<NotificationEntity, String> { | ||
List<NotificationEntity> findByMessage(String text); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/itsnaveenk/spring_kafka_consumer_elastic/service/ElasticsearchService.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,30 @@ | ||
package com.itsnaveenk.spring_kafka_consumer_elastic.service; | ||
|
||
|
||
import com.itsnaveenk.spring_kafka_consumer_elastic.entity.NotificationEntity; | ||
import com.itsnaveenk.spring_kafka_consumer_elastic.model.Notification; | ||
import com.itsnaveenk.spring_kafka_consumer_elastic.repository.ElasticRepository; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
|
||
@Service | ||
public class ElasticsearchService { | ||
|
||
private final ElasticRepository elasticRepository; | ||
private final ModelMapper modelMapper; | ||
|
||
@Autowired | ||
public ElasticsearchService(ElasticRepository elasticRepository, ModelMapper modelMapper) { | ||
this.elasticRepository = elasticRepository; | ||
this.modelMapper = modelMapper; | ||
} | ||
|
||
public void saveNotification(Notification notification) { | ||
NotificationEntity notificationEntity = modelMapper.map(notification, NotificationEntity.class); | ||
elasticRepository.save(notificationEntity); | ||
} | ||
|
||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/itsnaveenk/spring_kafka_consumer_elastic/service/KafkaConsumerService.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 |
---|---|---|
@@ -1,15 +1,23 @@ | ||
package com.itsnaveenk.spring_kafka_consumer_elastic.service; | ||
|
||
import com.itsnaveenk.spring_kafka_consumer_elastic.entity.NotificationEntity; | ||
import com.itsnaveenk.spring_kafka_consumer_elastic.model.Notification; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class KafkaConsumerService { | ||
|
||
@Autowired | ||
ElasticsearchService elasticsearchService; | ||
|
||
@KafkaListener(topics = "foodsOrder", groupId = "${spring.kafka.consumer.group-id}") | ||
public void listen(Notification notification) { | ||
System.out.println("Received notification: " + notification); | ||
// Process the notification | ||
elasticsearchService.saveNotification(notification); | ||
|
||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/itsnaveenk/spring_kafka_consumer_elastic/service/NotificationMapper.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,57 @@ | ||
package com.itsnaveenk.spring_kafka_consumer_elastic.service; | ||
|
||
import com.itsnaveenk.spring_kafka_consumer_elastic.entity.NotificationEntity; | ||
import com.itsnaveenk.spring_kafka_consumer_elastic.model.Notification; | ||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Custom DTO for Notification | ||
*/ | ||
public class NotificationMapper implements Serializable { | ||
private final int user_id; | ||
private final String message; | ||
private final int recipient_id; | ||
|
||
public NotificationMapper(int user_id, String message, int recipient_id) { | ||
this.user_id = user_id; | ||
this.message = message; | ||
this.recipient_id = recipient_id; | ||
} | ||
|
||
public int getUser_id() { | ||
return user_id; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public int getRecipient_id() { | ||
return recipient_id; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
NotificationMapper entity = (NotificationMapper) o; | ||
return Objects.equals(this.user_id, entity.user_id) && | ||
Objects.equals(this.message, entity.message) && | ||
Objects.equals(this.recipient_id, entity.recipient_id); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(user_id, message, recipient_id); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "(" + | ||
"user_id = " + user_id + ", " + | ||
"message = " + message + ", " + | ||
"recipient_id = " + recipient_id + ")"; | ||
} | ||
} |