topicname 일괄 변경

This commit is contained in:
kimscott
2019-09-02 17:10:52 +09:00
parent cf76c05918
commit c85b8eb9bf
6 changed files with 158 additions and 28 deletions

View File

@@ -13,6 +13,7 @@ import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.client.RestTemplate;
import javax.persistence.*;
import java.util.Optional;
@Entity
@Table(name = "order_table")
@@ -25,6 +26,7 @@ public class Order {
private String productName;
private int quantity;
private int price;
private String customerId;
private String customerName;
private String customerAddr;
@@ -45,35 +47,29 @@ public class Order {
}
// 1. 주문에 대한 상품 조회 - API
// String productUrl = env.getProperty("productUrl") + "/products/" + productId;
//
// ResponseEntity<String> productEntity = restTemplate.getForEntity(productUrl, String.class);
// System.out.println(productEntity.getStatusCode());
// System.out.println(productEntity.getBody());
//
// JsonParser parser = new JsonParser();
// JsonObject jsonObject = parser.parse(productEntity.getBody()).getAsJsonObject();
//
// OrderPlaced orderPlaced = new OrderPlaced();
// try {
// orderPlaced.setOrderId(id);
//
// this.setPrice(jsonObject.get("price").getAsInt());
// this.setProductName(jsonObject.get("name").getAsString());
//
// BeanUtils.copyProperties(this, orderPlaced);
// json = objectMapper.writeValueAsString(orderPlaced);
// } catch (JsonProcessingException e) {
// throw new RuntimeException("JSON format exception", e);
// }
if( env.getProperty("useRest") != null && "true".equals(env.getProperty("useRest").toLowerCase())){
String productUrl = env.getProperty("productUrl") + "/products/" + productId;
ResponseEntity<String> productEntity = restTemplate.getForEntity(productUrl, String.class);
JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(productEntity.getBody()).getAsJsonObject();
this.setPrice(jsonObject.get("price").getAsInt());
this.setProductName(jsonObject.get("name").getAsString());
}else{
// 2. 자체 DB 조회
ProductRepository productRepository = Application.applicationContext.getBean(ProductRepository.class);
Optional<Product> productOptional = productRepository.findById(productId);
Product product = productOptional.get();
this.setPrice(product.getPrice());
this.setProductName(product.getName());
}
OrderPlaced orderPlaced = new OrderPlaced();
try {
orderPlaced.setOrderId(id);
this.setPrice(1000);
this.setProductName("TV");
BeanUtils.copyProperties(this, orderPlaced);
json = objectMapper.writeValueAsString(orderPlaced);
} catch (JsonProcessingException e) {
@@ -118,6 +114,14 @@ public class Order {
this.price = price;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}

View File

@@ -20,12 +20,35 @@ public class OrderService {
private KafkaTemplate kafkaTemplate;
@Autowired
private OrderRepository orderRepository;
private ProductRepository productRepository;
/**
* 상품 변경이 발생할때마다, 상품정보를 저장해 놓음
*/
// @KafkaListener(topics = "eventTopic")
@KafkaListener(topics = "${eventTopic}")
public void onDeliveryCompleted(@Payload String message, ConsumerRecord<?, ?> consumerRecord) {
System.out.println("##### listener : " + message);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ProductChanged productChanged = null;
try {
productChanged = objectMapper.readValue(message, ProductChanged.class);
if( productChanged.getEventType().equals(ProductChanged.class.getSimpleName())){
}
Product product = new Product();
product.setId(productChanged.getProductId());
product.setStock(productChanged.getProductStock());
product.setName(productChanged.getProductName());
product.setPrice(productChanged.getProductPrice());
productRepository.save(product);
}catch (Exception e){
}
}
}

View File

@@ -0,0 +1,51 @@
package com.example.template;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.core.env.Environment;
import org.springframework.kafka.core.KafkaTemplate;
import javax.persistence.*;
@Entity
public class Product {
@Id
private Long id;
String name;
int price;
int stock;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}

View File

@@ -0,0 +1,41 @@
package com.example.template;
public class ProductChanged extends AbstractEvent{
private Long productId;
private String productName;
private int productPrice;
private int productStock;
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getProductPrice() {
return productPrice;
}
public void setProductPrice(int productPrice) {
this.productPrice = productPrice;
}
public int getProductStock() {
return productStock;
}
public void setProductStock(int productStock) {
this.productStock = productStock;
}
}

View File

@@ -0,0 +1,11 @@
package com.example.template;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface ProductRepository extends CrudRepository<Product, Long> {
List<Product> findByName(@Param("name") String name);
}

View File

@@ -3,7 +3,7 @@ server:
eventTopic: eventTopic
useRest : false
---
spring:
profiles: default