상품에 OneToMany 로직 추가

This commit is contained in:
kimscott
2019-11-21 16:52:00 +09:00
parent 5f15812c11
commit 3ab8cdf2d9
5 changed files with 68 additions and 40 deletions

View File

@@ -1 +1,2 @@
# orders
http http://localhost:8081/orders productId=2 quantity=3 customerId=1@uengine.org

View File

@@ -1,6 +1,13 @@
package com.example.template;
import com.example.template.config.kafka.KafkaProcessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;
public class AbstractEvent {
@@ -23,4 +30,37 @@ public class AbstractEvent {
this.timestamp = timestamp;
}
public boolean isMe(){
return getEventType().equals(getClass().getSimpleName());
}
public String toJson(){
ObjectMapper objectMapper = new ObjectMapper();
String json = null;
try {
json = objectMapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new RuntimeException("JSON format exception", e);
}
return json;
}
public void sendMessage(String json){
if( json != null ){
/**
* spring streams 방식
*/
KafkaProcessor processor = Application.applicationContext.getBean(KafkaProcessor.class);
MessageChannel outputChannel = processor.outboundTopic();
outputChannel.send(MessageBuilder
.withPayload(json)
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
.build());
}
}
}

View File

@@ -42,10 +42,7 @@ public class Order {
@ExceptionHandler(OrderException.class)
private void publishOrderPlaced(){
RestTemplate restTemplate = Application.applicationContext.getBean(RestTemplate.class);
Environment env = Application.applicationContext.getEnvironment();
ObjectMapper objectMapper = new ObjectMapper();
String json = null;
if( productId == null ){
throw new RuntimeException();
@@ -74,34 +71,16 @@ public class Order {
}
}
OrderPlaced orderPlaced = new OrderPlaced();
try {
orderPlaced.setOrderId(id);
BeanUtils.copyProperties(this, orderPlaced);
json = objectMapper.writeValueAsString(orderPlaced);
} catch (JsonProcessingException e) {
throw new RuntimeException("JSON format exception", e);
}
OrderPlaced orderPlaced = new OrderPlaced(this);
orderPlaced.sendMessage(orderPlaced.toJson());
}
// 2. 주문이 발생함 이벤트 발송
/**
* spring kafka 방식
*/
// Environment env = Application.applicationContext.getEnvironment();
// String topicName = env.getProperty("eventTopic");
// ProducerRecord producerRecord = new ProducerRecord<>(topicName, json);
// kafkaTemplate.send(producerRecord);
public Long getId() {
return id;
}
/**
* spring streams 방식
*/
KafkaProcessor processor = Application.applicationContext.getBean(KafkaProcessor.class);
MessageChannel outputChannel = processor.outboundTopic();
outputChannel.send(MessageBuilder
.withPayload(json)
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
.build());
public void setId(Long id) {
this.id = id;
}
public Long getProductId() {

View File

@@ -23,6 +23,18 @@ public class OrderPlaced extends AbstractEvent{
this.timestamp = defaultSimpleDateFormat.format(new Date());
}
public OrderPlaced(Order order){
this();
this.setProductId(order.getProductId());
this.setProductName(order.getProductName());
this.setOrderId(order.getId());
this.setQuantity(order.getQuantity());
this.setPrice(order.getPrice());
this.setCustomerId(order.getCustomerId());
this.setCustomerName(order.getCustomerName());
this.setCustomerAddr(order.getCustomerAddr());
}
public String getStateMessage() {
return stateMessage;
}

View File

@@ -18,9 +18,6 @@ import java.util.Optional;
@Service
public class OrderService {
@Autowired
private KafkaTemplate kafkaTemplate;
@Autowired
private ProductRepository productRepository;
@@ -38,16 +35,15 @@ public class OrderService {
ProductChanged productChanged = null;
try {
productChanged = objectMapper.readValue(message, ProductChanged.class);
if( productChanged.getEventType().equals(ProductChanged.class.getSimpleName())){
if( productChanged.isMe()){
Product product = new Product();
product.setId(productChanged.getProductId());
product.setStock(productChanged.getProductStock());
product.setName(productChanged.getProductName());
product.setPrice(productChanged.getProductPrice());
productRepository.save(product);
}
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){