我正在使用Spring Boot来构建RESTful Web服务.我的IDE是Eclipse Oxygen.
我送多在HTTP GET请求每2秒通过Chrome浏览器,但它们引发一个接一个.每个请求都将等待上一个请求完成.
这是我的控制器代码:
@RestController
@RequestMapping("/dummy")
public class DummyController {
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Map<String, String>> dummytsp(@RequestParam(value="msg", defaultValue="Hello") String msg) {
System.out.println("" + new Date() + ": ThreadId " + Thread.currentThread().getId());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, String> response = new HashMap<>();
response.put("message", msg);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制台输出是:
Thu Sep 14 11:31:15 EDT 2017: …Run Code Online (Sandbox Code Playgroud) 我正在编写一个无阻塞的Spring Rest控制器.我的客户端应该发送请求,不关心响应,也不需要等待.
这是我的服务器代码:
@RestController
@EnableAsync
public class testController {
@RequestMapping(value = "test", method = RequestMethod.GET)
public ResponseEntity<String> test() throws InterruptedException {
timeConsumingMethod();
System.out.println("I'm should be first");
return new ResponseEntity<String>("the server is processing your request", HttpStatus.OK);
}
@Async
private void timeConsumingMethod() throws InterruptedException {
Thread.sleep(1000*5);
System.out.println("I'm should be second!");
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用(POSTMAN,Chrome等)调用http:// localhost:8181/test时,我在服务器日志中得到以下信息:
我应该是第二名!
我应该是第一个
并且等待5秒后我的浏览器显示:
服务器正在处理您的请求
这是"发送和忘记"行为的正确方法吗?