이벤트 order 이벤트 핸들링
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
package contracts.messaging
|
package contracts.messaging
|
||||||
//
|
|
||||||
org.springframework.cloud.contract.spec.Contract.make {
|
org.springframework.cloud.contract.spec.Contract.make {
|
||||||
description("""
|
description("""
|
||||||
spring contract 에서 메세지를 받는 방식은 총 3가지인데,
|
spring contract 에서 메세지를 받는 방식은 총 3가지인데,
|
||||||
@@ -1,6 +1,14 @@
|
|||||||
package com.example.template;
|
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 {
|
public class AbstractEvent {
|
||||||
|
|
||||||
@@ -23,4 +31,17 @@ public class AbstractEvent {
|
|||||||
this.timestamp = timestamp;
|
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;
|
package com.example.template;
|
||||||
|
|
||||||
import com.example.template.config.kafka.KafkaProcessor;
|
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.MessageChannel;
|
||||||
import org.springframework.messaging.MessageHeaders;
|
import org.springframework.messaging.MessageHeaders;
|
||||||
import org.springframework.messaging.support.MessageBuilder;
|
import org.springframework.messaging.support.MessageBuilder;
|
||||||
@@ -29,30 +22,11 @@ public class Product {
|
|||||||
|
|
||||||
@PostPersist @PostUpdate
|
@PostPersist @PostUpdate
|
||||||
private void publishStart() {
|
private void publishStart() {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
String json = null;
|
|
||||||
|
|
||||||
ProductChanged productChanged = new ProductChanged();
|
ProductChanged productChanged = new ProductChanged(this);
|
||||||
productChanged.setProductId(this.id);
|
String json = productChanged.toJson();
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if( json != null ){
|
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 방식
|
* spring streams 방식
|
||||||
|
|||||||
@@ -1,18 +1,40 @@
|
|||||||
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.util.MimeTypeUtils;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class ProductChanged extends AbstractEvent{
|
public class ProductChanged extends AbstractEvent{
|
||||||
|
|
||||||
private String stateMessage = "상품 변경이 발생함";
|
private String stateMessage = "상품 변경이 발생함";
|
||||||
|
|
||||||
private Long productId;
|
private Long productId;
|
||||||
|
|
||||||
// private String productTitle;
|
|
||||||
private String productName;
|
private String productName;
|
||||||
private int productPrice;
|
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;
|
private String imageUrl;
|
||||||
|
|
||||||
public ProductChanged(){
|
public ProductChanged(){
|
||||||
@@ -21,6 +43,23 @@ public class ProductChanged extends AbstractEvent{
|
|||||||
this.timestamp = defaultSimpleDateFormat.format(new Date());
|
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() {
|
public String getStateMessage() {
|
||||||
return stateMessage;
|
return stateMessage;
|
||||||
}
|
}
|
||||||
@@ -29,22 +68,6 @@ public class ProductChanged extends AbstractEvent{
|
|||||||
this.stateMessage = stateMessage;
|
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() {
|
public String getProductName() {
|
||||||
return productName;
|
return productName;
|
||||||
}
|
}
|
||||||
@@ -61,14 +84,6 @@ public class ProductChanged extends AbstractEvent{
|
|||||||
this.productPrice = productPrice;
|
this.productPrice = productPrice;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getProductStock() {
|
|
||||||
return productStock;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setProductStock(int productStock) {
|
|
||||||
this.productStock = productStock;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getImageUrl() {
|
public String getImageUrl() {
|
||||||
return imageUrl;
|
return imageUrl;
|
||||||
}
|
}
|
||||||
@@ -76,4 +91,5 @@ public class ProductChanged extends AbstractEvent{
|
|||||||
public void setImageUrl(String imageUrl) {
|
public void setImageUrl(String imageUrl) {
|
||||||
this.imageUrl = imageUrl;
|
this.imageUrl = imageUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,25 +41,17 @@ public abstract class MessagingBase {
|
|||||||
|
|
||||||
public void productChanged() {
|
public void productChanged() {
|
||||||
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
Product product = new Product();
|
||||||
String json = null;
|
product.setId(1L);
|
||||||
|
product.setName("TEST");
|
||||||
|
product.setPrice(10000);
|
||||||
|
product.setStock(10);
|
||||||
|
product.setImageUrl("/test.jpg");
|
||||||
|
|
||||||
// TODO json 으로 변경
|
ProductChanged productChanged = new ProductChanged(product);
|
||||||
ProductChanged productChanged = new ProductChanged();
|
String json = productChanged.toJson();
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("test output Topic = " + kafkaProcessor.outboundTopic().toString());
|
System.out.println("test output Topic = " + kafkaProcessor.outboundTopic().toString());
|
||||||
|
|
||||||
this.messaging.send(MessageBuilder
|
this.messaging.send(MessageBuilder
|
||||||
.withPayload(json)
|
.withPayload(json)
|
||||||
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
|
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
|
||||||
|
|||||||
Reference in New Issue
Block a user