add request response

This commit is contained in:
kimscott
2020-02-06 15:03:26 +09:00
parent a2ae16fec3
commit c8ae3a67f4
6 changed files with 71 additions and 5 deletions

View File

@@ -55,7 +55,10 @@ public class AbstractEvent {
return json;
}
public void sendMessage(String json){
public void publish(){
this.publish(this.toJson());
}
public void publish(String json){
if( json != null ){
/**

View File

@@ -32,7 +32,7 @@ public class Product {
@PostPersist @PostUpdate
private void publishStart() {
ProductChanged productChanged = new ProductChanged(this);
productChanged.sendMessage(productChanged.toJson());
productChanged.publish();
}
public Long getId() {

View File

@@ -14,9 +14,7 @@ public class ProductChanged extends AbstractEvent{
private String imageUrl;
public ProductChanged(){
this.setEventType(this.getClass().getSimpleName());
SimpleDateFormat defaultSimpleDateFormat = new SimpleDateFormat("YYYYMMddHHmmss");
this.timestamp = defaultSimpleDateFormat.format(new Date());
super();
}
public ProductChanged(Product product){

View File

@@ -1,14 +1,31 @@
package com.example.template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
public class ProductController {
private static final String RESPONSE_STRING_FORMAT = "product change from '%s': %d";
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
ProductService productService;
private int count = 0;
private static final String HOSTNAME = parseContainerIdFromHostname(
System.getenv().getOrDefault("HOSTNAME", "deliveries"));
static String parseContainerIdFromHostname(String hostname) {
return hostname.replaceAll("deliveries-v\\d+-", "");
}
@GetMapping("/product/{productId}")
Product productStockCheck(@PathVariable(value = "productId") Long productId) {
@@ -27,4 +44,20 @@ public class ProductController {
System.out.println(data);
return this.productService.save(data);
}
@PatchMapping("/product/{productId}")
ResponseEntity<String> fakeProductPatch(@PathVariable(value = "productId") Long productId, @RequestBody String data) {
count++;
logger.info(String.format("product start from %s: %d", HOSTNAME, count));
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ResponseEntity.ok(String.format(RESPONSE_STRING_FORMAT, HOSTNAME, count));
}
}