이벤트 order 이벤트 핸들링
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
package com.example.template;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
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.util.MimeTypeUtils;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
public class AbstractEvent {
|
||||
|
||||
@@ -23,4 +31,17 @@ public class AbstractEvent {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
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.apache.kafka.clients.producer.ProducerRecord;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
@@ -29,30 +22,11 @@ public class Product {
|
||||
|
||||
@PostPersist @PostUpdate
|
||||
private void publishStart() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String json = null;
|
||||
|
||||
ProductChanged productChanged = new ProductChanged();
|
||||
productChanged.setProductId(this.id);
|
||||
// productChanged.setProductTitle(this.name);
|
||||
productChanged.setProductName(this.name);
|
||||
productChanged.setProductPrice(this.price);
|
||||
productChanged.setProductStock(this.stock);
|
||||
productChanged.setImageUrl(this.imageUrl);
|
||||
try {
|
||||
json = objectMapper.writeValueAsString(productChanged);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException("JSON format exception", e);
|
||||
}
|
||||
ProductChanged productChanged = new ProductChanged(this);
|
||||
String json = productChanged.toJson();
|
||||
|
||||
if( json != null ){
|
||||
/**
|
||||
* spring kafka 방식
|
||||
*/
|
||||
// Environment env = Application.applicationContext.getEnvironment();
|
||||
// String topicName = env.getProperty("eventTopic");
|
||||
// ProducerRecord producerRecord = new ProducerRecord<>(topicName, json);
|
||||
// kafkaTemplate.send(producerRecord);
|
||||
|
||||
/**
|
||||
* spring streams 방식
|
||||
|
||||
@@ -1,18 +1,40 @@
|
||||
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.util.MimeTypeUtils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class ProductChanged extends AbstractEvent{
|
||||
public class ProductChanged extends AbstractEvent{
|
||||
|
||||
private String stateMessage = "상품 변경이 발생함";
|
||||
|
||||
private Long productId;
|
||||
|
||||
// private String productTitle;
|
||||
private String productName;
|
||||
private int productPrice;
|
||||
private int productStock;
|
||||
|
||||
// private int productStock;
|
||||
// public int getProductStock() {
|
||||
// return productStock;
|
||||
// }
|
||||
// public void setProductStock(int productStock) {
|
||||
// this.productStock = productStock;
|
||||
// }
|
||||
|
||||
private int stock;
|
||||
public int getStock() {
|
||||
return stock;
|
||||
}
|
||||
public void setStock(int stock) {
|
||||
this.stock = stock;
|
||||
}
|
||||
|
||||
private String imageUrl;
|
||||
|
||||
public ProductChanged(){
|
||||
@@ -21,6 +43,23 @@ public class ProductChanged extends AbstractEvent{
|
||||
this.timestamp = defaultSimpleDateFormat.format(new Date());
|
||||
}
|
||||
|
||||
public ProductChanged(Product product){
|
||||
this();
|
||||
this.setProductId(product.getId());
|
||||
this.setProductName(product.getName());
|
||||
this.setProductPrice(product.getPrice());
|
||||
// this.setProductStock(product.getStock());
|
||||
this.setStock(product.getStock());
|
||||
this.setImageUrl(product.getImageUrl());
|
||||
}
|
||||
|
||||
public Long getProductId() {
|
||||
return productId;
|
||||
}
|
||||
public void setProductId(Long productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public String getStateMessage() {
|
||||
return stateMessage;
|
||||
}
|
||||
@@ -29,22 +68,6 @@ public class ProductChanged extends AbstractEvent{
|
||||
this.stateMessage = stateMessage;
|
||||
}
|
||||
|
||||
public Long getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(Long productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
// public String getProductTitle() {
|
||||
// return productTitle;
|
||||
// }
|
||||
//
|
||||
// public void setProductTitle(String productTitle) {
|
||||
// this.productTitle = productTitle;
|
||||
// }
|
||||
|
||||
public String getProductName() {
|
||||
return productName;
|
||||
}
|
||||
@@ -61,14 +84,6 @@ public class ProductChanged extends AbstractEvent{
|
||||
this.productPrice = productPrice;
|
||||
}
|
||||
|
||||
public int getProductStock() {
|
||||
return productStock;
|
||||
}
|
||||
|
||||
public void setProductStock(int productStock) {
|
||||
this.productStock = productStock;
|
||||
}
|
||||
|
||||
public String getImageUrl() {
|
||||
return imageUrl;
|
||||
}
|
||||
@@ -76,4 +91,5 @@ public class ProductChanged extends AbstractEvent{
|
||||
public void setImageUrl(String imageUrl) {
|
||||
this.imageUrl = imageUrl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -41,25 +41,17 @@ public abstract class MessagingBase {
|
||||
|
||||
public void productChanged() {
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String json = null;
|
||||
Product product = new Product();
|
||||
product.setId(1L);
|
||||
product.setName("TEST");
|
||||
product.setPrice(10000);
|
||||
product.setStock(10);
|
||||
product.setImageUrl("/test.jpg");
|
||||
|
||||
// TODO json 으로 변경
|
||||
ProductChanged productChanged = new ProductChanged();
|
||||
productChanged.setProductId(1L);
|
||||
// productChanged.setProductTitle("TEST");
|
||||
productChanged.setProductName("TEST");
|
||||
productChanged.setProductPrice(10000);
|
||||
productChanged.setProductStock(10);
|
||||
productChanged.setImageUrl("/test.jpg");
|
||||
try {
|
||||
json = objectMapper.writeValueAsString(productChanged);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException("JSON format exception", e);
|
||||
}
|
||||
ProductChanged productChanged = new ProductChanged(product);
|
||||
String json = productChanged.toJson();
|
||||
|
||||
System.out.println("test output Topic = " + kafkaProcessor.outboundTopic().toString());
|
||||
|
||||
this.messaging.send(MessageBuilder
|
||||
.withPayload(json)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
package contracts.messaging
|
||||
//
|
||||
org.springframework.cloud.contract.spec.Contract.make {
|
||||
description("""
|
||||
spring contract 에서 메세지를 받는 방식은 총 3가지인데,
|
||||
1. input 은 없고 output만 있는 경우
|
||||
2. input 을 받아서 output 으로 보내는 경우
|
||||
3. input 만 있는 경우
|
||||
|
||||
input 에 triggeredBy method 를 호출하는 경우는 보통 input 메세지가 없는 경우이다.
|
||||
|
||||
```
|
||||
given:
|
||||
product changed event occurred
|
||||
when:
|
||||
he applies for a beer
|
||||
then:
|
||||
we'll send a message with a ProductChanged message
|
||||
```
|
||||
|
||||
""")
|
||||
// Label by means of which the output message can be triggered
|
||||
label 'productChanged'
|
||||
// input to the contract
|
||||
input {
|
||||
// the contract will be triggered by a method
|
||||
triggeredBy('productChanged()')
|
||||
}
|
||||
// output message of the contract
|
||||
outputMessage {
|
||||
// destination to which the output message will be sent
|
||||
sentTo 'eventTopic'
|
||||
// the body of the output message
|
||||
body(
|
||||
eventType: "ProductChanged",
|
||||
productId: 1,
|
||||
productName: "TV",
|
||||
productPrice: 10000,
|
||||
productStock: 10,
|
||||
imageUrl: "testUrl"
|
||||
)
|
||||
bodyMatchers {
|
||||
jsonPath('$.eventType', byRegex("ProductChanged"))
|
||||
jsonPath('$.productId', byRegex(nonEmpty()).asLong())
|
||||
jsonPath('$.productName', byRegex(nonEmpty()).asString())
|
||||
jsonPath('$.productPrice', byRegex(nonEmpty()).asLong())
|
||||
jsonPath('$.productStock', byRegex(nonEmpty()).asLong())
|
||||
jsonPath('$.imageUrl', byRegex(nonEmpty()).asString())
|
||||
}
|
||||
headers {
|
||||
messagingContentType(applicationJson())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user