add saga rollback

This commit is contained in:
kimscott
2020-02-10 13:34:44 +09:00
parent 2cacfca6b8
commit b76e9f3abd
3 changed files with 51 additions and 2 deletions

View File

@@ -49,7 +49,7 @@ public class Order {
price = jsonObject.get("price").getAsInt();
productName = jsonObject.get("name").getAsString();
if( jsonObject.get("stock").getAsInt() < getQuantity()){
throw new OrderException("No Available stock!");
// throw new OrderException("No Available stock!");
}
@@ -61,7 +61,7 @@ public class Order {
price = product.getPrice();
productName = product.getName();
if( product.getStock() < getQuantity()){
throw new OrderException("No Available stock!");
// throw new OrderException("No Available stock!");
}
}
this.setPrice(price);

View File

@@ -20,6 +20,8 @@ public class OrderService {
@Autowired
private ProductRepository productRepository;
@Autowired
private OrderRepository orderRepository;
/**
* 상품 변경이 발생할때마다, 상품정보를 저장해 놓음
@@ -41,4 +43,19 @@ public class OrderService {
e.printStackTrace();
}
}
@StreamListener(KafkaProcessor.INPUT)
public void onProductOutOfStock(@Payload ProductOutOfStock productOutOfStock) {
try {
if (productOutOfStock.isMe()) {
System.out.println("##### listener : " + productOutOfStock.toJson());
Optional<Order> orderOptional = orderRepository.findById(productOutOfStock.getOrderId());
Order order = orderOptional.get();
order.setState("OrderCancelled");
orderRepository.save(order);
}
}catch (Exception e){
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,32 @@
package com.example.template;
public class ProductOutOfStock extends AbstractEvent {
private String stateMessage = "재고량 바닥";
private Long productId;
private Long orderId;
public String getStateMessage() {
return stateMessage;
}
public void setStateMessage(String stateMessage) {
this.stateMessage = stateMessage;
}
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
}