상품에 OneToMany 로직 추가
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
# orders
|
# orders
|
||||||
|
http http://localhost:8081/orders productId=2 quantity=3 customerId=1@uengine.org
|
||||||
@@ -1,6 +1,13 @@
|
|||||||
package com.example.template;
|
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.stereotype.Service;
|
||||||
|
import org.springframework.util.MimeTypeUtils;
|
||||||
|
|
||||||
public class AbstractEvent {
|
public class AbstractEvent {
|
||||||
|
|
||||||
@@ -23,4 +30,37 @@ public class AbstractEvent {
|
|||||||
this.timestamp = timestamp;
|
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());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,10 +42,7 @@ public class Order {
|
|||||||
@ExceptionHandler(OrderException.class)
|
@ExceptionHandler(OrderException.class)
|
||||||
private void publishOrderPlaced(){
|
private void publishOrderPlaced(){
|
||||||
RestTemplate restTemplate = Application.applicationContext.getBean(RestTemplate.class);
|
RestTemplate restTemplate = Application.applicationContext.getBean(RestTemplate.class);
|
||||||
|
|
||||||
Environment env = Application.applicationContext.getEnvironment();
|
Environment env = Application.applicationContext.getEnvironment();
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
String json = null;
|
|
||||||
|
|
||||||
if( productId == null ){
|
if( productId == null ){
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
@@ -74,34 +71,16 @@ public class Order {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OrderPlaced orderPlaced = new OrderPlaced();
|
OrderPlaced orderPlaced = new OrderPlaced(this);
|
||||||
try {
|
orderPlaced.sendMessage(orderPlaced.toJson());
|
||||||
orderPlaced.setOrderId(id);
|
}
|
||||||
BeanUtils.copyProperties(this, orderPlaced);
|
|
||||||
json = objectMapper.writeValueAsString(orderPlaced);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
throw new RuntimeException("JSON format exception", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. 주문이 발생함 이벤트 발송
|
public Long getId() {
|
||||||
/**
|
return id;
|
||||||
* spring kafka 방식
|
}
|
||||||
*/
|
|
||||||
// Environment env = Application.applicationContext.getEnvironment();
|
|
||||||
// String topicName = env.getProperty("eventTopic");
|
|
||||||
// ProducerRecord producerRecord = new ProducerRecord<>(topicName, json);
|
|
||||||
// kafkaTemplate.send(producerRecord);
|
|
||||||
|
|
||||||
/**
|
public void setId(Long id) {
|
||||||
* spring streams 방식
|
this.id = id;
|
||||||
*/
|
|
||||||
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 Long getProductId() {
|
public Long getProductId() {
|
||||||
|
|||||||
@@ -23,6 +23,18 @@ public class OrderPlaced extends AbstractEvent{
|
|||||||
this.timestamp = defaultSimpleDateFormat.format(new Date());
|
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() {
|
public String getStateMessage() {
|
||||||
return stateMessage;
|
return stateMessage;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,9 +18,6 @@ import java.util.Optional;
|
|||||||
@Service
|
@Service
|
||||||
public class OrderService {
|
public class OrderService {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private KafkaTemplate kafkaTemplate;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ProductRepository productRepository;
|
private ProductRepository productRepository;
|
||||||
|
|
||||||
@@ -38,16 +35,15 @@ public class OrderService {
|
|||||||
ProductChanged productChanged = null;
|
ProductChanged productChanged = null;
|
||||||
try {
|
try {
|
||||||
productChanged = objectMapper.readValue(message, ProductChanged.class);
|
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){
|
}catch (Exception e){
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user