Commit 8992b1a6 authored by ReemyHasan's avatar ReemyHasan

Kafka producer-consumer ek rethinkdb

parent d6474c12
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
<sourceOutputDir name="target/generated-sources/annotations" /> <sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" /> <outputRelativeToContentRoot value="true" />
<module name="UserManagement" />
<module name="producer" /> <module name="producer" />
<module name="consumer" /> <module name="consumer" />
</profile> </profile>
...@@ -14,10 +15,12 @@ ...@@ -14,10 +15,12 @@
<bytecodeTargetLevel> <bytecodeTargetLevel>
<module name="consumer" target="17" /> <module name="consumer" target="17" />
<module name="producer" target="17" /> <module name="producer" target="17" />
<module name="UserManagement" target="17" />
</bytecodeTargetLevel> </bytecodeTargetLevel>
</component> </component>
<component name="JavacSettings"> <component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE"> <option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="UserManagement" options="-parameters" />
<module name="consumer" options="-parameters" /> <module name="consumer" options="-parameters" />
<module name="producer" options="-parameters" /> <module name="producer" options="-parameters" />
</option> </option>
......
...@@ -3,5 +3,6 @@ ...@@ -3,5 +3,6 @@
<component name="Encoding"> <component name="Encoding">
<file url="file://$PROJECT_DIR$/consumer/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/consumer/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/producer/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/producer/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/userManagement/src/main/java" charset="UTF-8" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
<list> <list>
<option value="$PROJECT_DIR$/consumer/pom.xml" /> <option value="$PROJECT_DIR$/consumer/pom.xml" />
<option value="$PROJECT_DIR$/producer/pom.xml" /> <option value="$PROJECT_DIR$/producer/pom.xml" />
<option value="$PROJECT_DIR$/userManagement/pom.xml" />
</list> </list>
</option> </option>
</component> </component>
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/.idea/SNMP Collector.iml" filepath="$PROJECT_DIR$/.idea/SNMP Collector.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/SNMP Collector.iml" filepath="$PROJECT_DIR$/.idea/SNMP Collector.iml" />
<module fileurl="file://$PROJECT_DIR$/userManagement/UserManagement.iml" filepath="$PROJECT_DIR$/userManagement/UserManagement.iml" />
<module fileurl="file://$PROJECT_DIR$/consumer/consumer.iml" filepath="$PROJECT_DIR$/consumer/consumer.iml" /> <module fileurl="file://$PROJECT_DIR$/consumer/consumer.iml" filepath="$PROJECT_DIR$/consumer/consumer.iml" />
<module fileurl="file://$PROJECT_DIR$/producer/producer.iml" filepath="$PROJECT_DIR$/producer/producer.iml" /> <module fileurl="file://$PROJECT_DIR$/producer/producer.iml" filepath="$PROJECT_DIR$/producer/producer.iml" />
</modules> </modules>
......
package com.example.consumer.controller; package com.example.consumer.controller;
import com.example.consumer.repository.Trap; import com.example.consumer.entity.Trap;
import com.example.consumer.services.ElasticService; import com.example.consumer.services.TrapService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener; import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -9,23 +9,23 @@ import org.springframework.web.bind.annotation.*; ...@@ -9,23 +9,23 @@ import org.springframework.web.bind.annotation.*;
@RestController @RestController
@CrossOrigin(origins = {"http://localhost:5173"}) @CrossOrigin(origins = {"http://localhost:5173"})
@RequestMapping("/api/elastic") @RequestMapping("/api/elastic")
public class ElasticController { public class TrapController {
@Autowired @Autowired
private ElasticService elasticService; private TrapService trapService;
@GetMapping("/findAllTraps") @GetMapping("/findAllTraps")
Iterable<Trap> findAll(){ Iterable<Trap> findAll(){
return elasticService.getTraps(); return trapService.getTraps();
} }
@PostMapping("/insertTraps") @PostMapping("/insertTraps")
public Trap insertProduct(@RequestBody Trap trap){ public Trap insertTrap(@RequestBody Trap trap){
return elasticService.insertTrap(trap); return trapService.insertTrap(trap);
} }
@KafkaListener(topics = "Test") @KafkaListener(topics = "Test")
public void handleKafkaMessage(String message) { public void handleKafkaMessage(String message) {
elasticService.saveKafkaMessageToElastic(message); trapService.saveKafkaMessageToElastic(message);
} }
} }
......
package com.example.consumer.repository; package com.example.consumer.entity;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
......
package com.example.consumer.repository; package com.example.consumer.repository;
import com.example.consumer.entity.Trap;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
public interface ElasticRepository extends ElasticsearchRepository<Trap,Integer> { @Repository
public interface TrapRepository extends ElasticsearchRepository<Trap,Integer> {
} }
package com.example.consumer.services; package com.example.consumer.services;
import com.example.consumer.repository.ElasticRepository; import com.example.consumer.repository.TrapRepository;
import com.example.consumer.repository.Trap; import com.example.consumer.entity.Trap;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -9,15 +9,15 @@ import org.springframework.stereotype.Service; ...@@ -9,15 +9,15 @@ import org.springframework.stereotype.Service;
@Service @Service
public class ElasticService { public class TrapService {
@Autowired @Autowired
private ElasticRepository elasticRepository; private TrapRepository trapRepository;
private final Logger log = LoggerFactory.getLogger(ElasticService.class); private final Logger log = LoggerFactory.getLogger(TrapService.class);
public Iterable<Trap> getTraps() { public Iterable<Trap> getTraps() {
try{ try{
return elasticRepository.findAll(); return trapRepository.findAll();
} catch (Exception e) { } catch (Exception e) {
log.error("Error getting data from Elastic", e); log.error("Error getting data from Elastic", e);
return null; return null;
...@@ -26,7 +26,7 @@ public class ElasticService { ...@@ -26,7 +26,7 @@ public class ElasticService {
public Trap insertTrap(Trap trap) { public Trap insertTrap(Trap trap) {
try { try {
elasticRepository.save(trap); trapRepository.save(trap);
log.info("Data saved successfully in Elastic"); log.info("Data saved successfully in Elastic");
return trap; return trap;
...@@ -38,18 +38,18 @@ public class ElasticService { ...@@ -38,18 +38,18 @@ public class ElasticService {
} }
public Trap updateTrap(Trap trap, int id) { public Trap updateTrap(Trap trap, int id) {
Trap trap1 = elasticRepository.findById(id).get(); Trap trap1 = trapRepository.findById(id).get();
trap1.setTrap(trap.getTrap()); trap1.setTrap(trap.getTrap());
return trap1; return trap1;
} }
public void deleteTrap(int id ) { public void deleteTrap(int id ) {
elasticRepository.deleteById(id); trapRepository.deleteById(id);
} }
public void saveKafkaMessageToElastic(String message) { public void saveKafkaMessageToElastic(String message) {
try { try {
Trap trap = new Trap(message); Trap trap = new Trap(message);
elasticRepository.save(trap); trapRepository.save(trap);
log.info("Data saved successfully in Elastic"); log.info("Data saved successfully in Elastic");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment