Commit 8992b1a6 authored by ReemyHasan's avatar ReemyHasan

Kafka producer-consumer ek rethinkdb

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