Yoa*_*nda 4 java rest spring asynchronous spring-mvc
我正在编写一个无阻塞的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秒后我的浏览器显示:
服务器正在处理您的请求
这是"发送和忘记"行为的正确方法吗?
根据doc页面,@EnableAsync
应该在配置类上添加.
启用Spring的异步方法执行功能,类似于Spring的XML命名空间中的功能.
要在@Configuration类上使用,如下所示,其中MyAsyncBean是一个用户定义的类型,其中一个或多个方法使用Spring的@Async批注,EJB 3.1 @ javax.ejb.Asynchronous批注或通过批注指定的任何自定义批注进行批注()属性.
归档时间: |
|
查看次数: |
3292 次 |
最近记录: |