Committed Changes to Configure Producer and Integration with Order Generator

parent c9f374b7
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version> <version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>com.nisum.omd</groupId> <groupId>com.nisum.omd</groupId>
<artifactId>omd-checkout-service</artifactId> <artifactId>omd-checkout-service</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>omd-checkout-service</name> <name>omd-checkout-service</name>
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.kafka</groupId>
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-kafka</artifactId>
<scope>runtime</scope> </dependency>
<optional>true</optional> <dependency>
</dependency> <groupId>org.slf4j</groupId>
<dependency> <artifactId>slf4j-simple</artifactId>
<groupId>com.h2database</groupId> <version>1.7.30</version>
<artifactId>h2</artifactId> <scope>test</scope>
<scope>runtime</scope> </dependency>
</dependency> <dependency>
<dependency> <groupId>org.springframework.boot</groupId>
<groupId>com.github.javafaker</groupId> <artifactId>spring-boot-devtools</artifactId>
<artifactId>javafaker</artifactId> <scope>runtime</scope>
<version>1.0.2</version> <optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>com.h2database</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>h2</artifactId>
<scope>test</scope> <scope>runtime</scope>
<exclusions> </dependency>
<exclusion> <dependency>
<groupId>org.junit.vintage</groupId> <groupId>com.github.javafaker</groupId>
<artifactId>junit-vintage-engine</artifactId> <artifactId>javafaker</artifactId>
</exclusion> <version>1.0.2</version>
</exclusions> </dependency>
</dependency> <dependency>
<dependency> <groupId>org.springframework.boot</groupId>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId>
<artifactId>spring-boot-configuration-processor</artifactId> <scope>test</scope>
<optional>true</optional> <exclusions>
</dependency> <exclusion>
</dependencies> <groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>
...@@ -2,6 +2,7 @@ package com.nisum.omd; ...@@ -2,6 +2,7 @@ package com.nisum.omd;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import com.nisum.omd.service.OmdProducerService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -19,6 +20,10 @@ public class OrderGenerator implements CommandLineRunner{ ...@@ -19,6 +20,10 @@ public class OrderGenerator implements CommandLineRunner{
RandomDataGeneratorImpl rdg; RandomDataGeneratorImpl rdg;
@Autowired @Autowired
Order order; Order order;
@Autowired
OmdProducerService omdProducerService;
Boolean orderGenerateFlag = true; Boolean orderGenerateFlag = true;
public Boolean getOrderGenerateFlag() { public Boolean getOrderGenerateFlag() {
...@@ -33,6 +38,7 @@ public class OrderGenerator implements CommandLineRunner{ ...@@ -33,6 +38,7 @@ public class OrderGenerator implements CommandLineRunner{
while(orderGenerateFlag){ while(orderGenerateFlag){
order=getObjectData(order); order=getObjectData(order);
omdProducerService.sendMessage(order);
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule()); objectMapper.registerModule(new JavaTimeModule());
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
......
package com.nisum.omd.config;
import com.nisum.omd.domain.Order;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.serializer.JsonSerializer;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class OmdProducerConfig {
@Value("${bootstrap-server}")
private String bootstrapServer;
@Bean
public ProducerFactory<String, Order> producerFactory(){
Map<String,Object> config = new HashMap<>();
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
return new DefaultKafkaProducerFactory<>(config);
}
@Bean
public KafkaTemplate<String, Order> kafkaTemplate(){
return new KafkaTemplate<>(producerFactory());
}
}
package com.nisum.omd.service;
import com.nisum.omd.domain.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
@Service
public class OmdProducerService {
@Autowired
private KafkaTemplate<String, Order> kafkaTemplate;
@Value("${topic-name}")
private String topic;
Logger logger = LoggerFactory.getLogger(OmdProducerService.class);
public void sendMessage(Order order) {
ListenableFuture<SendResult<String, Order>> future =
kafkaTemplate.send(topic, order);
future.addCallback(new ListenableFutureCallback<SendResult<String, Order>>() {
@Override
public void onSuccess(SendResult<String, Order> result) {
logger.info(" Published Order Data with Order Number :"
+ order.getOrderNum() + " In Topic :"+ result.getRecordMetadata().topic());
}
@Override
public void onFailure(Throwable ex) {
logger.error("Unable to Publish the Order Data, Due to: ",ex.getMessage());
}
});
}
}
...@@ -67,3 +67,8 @@ app.paymentMethod.paymentStatus=AUTH ...@@ -67,3 +67,8 @@ app.paymentMethod.paymentStatus=AUTH
app.paymentMethod.paymentDetail.TransactionId=021361475199 app.paymentMethod.paymentDetail.TransactionId=021361475199
app.paymentMethod.paymentDetail.ProcessedAmount=1000.00 app.paymentMethod.paymentDetail.ProcessedAmount=1000.00
app.paymentMethod.paymentDetail.RequestAmount=1000.00 app.paymentMethod.paymentDetail.RequestAmount=1000.00
#Kafka Properties
bootstrap-server = localhost:9092
topic-name = TOPIC_OMD_ORDER_DATA
\ No newline at end of file
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