博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
同步等待异步模型
阅读量:4203 次
发布时间:2019-05-26

本文共 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 HashMap
obj = 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; }}

你可能感兴趣的文章
【一天一道LeetCode】#91. Decode Ways
查看>>
【一天一道LeetCode】#92. Reverse Linked List II
查看>>
【一天一道LeetCode】#93. Restore IP Addresses
查看>>
【一天一道LeetCode】#94. Binary Tree Inorder Traversal
查看>>
【一天一道LeetCode】#113. Path Sum II
查看>>
【一天一道LeetCode】#114. Flatten Binary Tree to Linked List
查看>>
【unix网络编程第三版】阅读笔记(二):套接字编程简介
查看>>
【一天一道LeetCode】#115. Distinct Subsequences
查看>>
【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node
查看>>
【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
查看>>
【一天一道LeetCode】#118. Pascal's Triangle
查看>>
【一天一道LeetCode】#119. Pascal's Triangle II
查看>>
【unix网络编程第三版】ubuntu端口占用问题
查看>>
【一天一道LeetCode】#120. Triangle
查看>>
【unix网络编程第三版】阅读笔记(三):基本套接字编程
查看>>
【一天一道LeetCode】#121. Best Time to Buy and Sell Stock
查看>>
【一天一道LeetCode】#125. Valid Palindrome
查看>>
【一天一道LeetCode】#231. Power of Two
查看>>
【一天一道LeetCode】#202. Happy Number
查看>>
带你深入理解STL之Vector容器
查看>>