本文共 1532 字,大约阅读时间需要 5 分钟。
一、需求:
前端http请求(REST方式)为同步处理,后台调用Kafka消息队列变成异步处理,因此有同步等待异步需求。
前置1、前置2同属一套代码,但部署到不同的服务器(也就是不同的tomcat容器)。
二、规划模型:
三、纯伪代码-前置1:
package com.test;import java.util.Date;import java.util.HashMap;import java.util.concurrent.atomic.AtomicLong;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.util.UriComponentsBuilder;@RestController@RequestMapping("/service")public class GreetingController { private static HashMapobj = new HashMap<>(); @RequestMapping(value = "/input", method = RequestMethod.POST) public String getInput(@RequestBody String param, UriComponentsBuilder ucBuilder) { System.out.println("Creating User " + param); Date date = new Date(); kafkaProducer(date, param); while(true){ if (obj.get(date.toString()) == null) { Thread.sleep(100); } else { String param = obj.get(date.toString()); obj.clear(); return param; } } return param; } @RequestMapping(value = "/result", method = RequestMethod.POST) public String getResult(@RequestBody String param, UriComponentsBuilder ucBuilder) { obj.put(param.getDate(), param); return param; }}