Java “弹簧无阻塞支架”;“发送并忘记”;
我正在编写一个非阻塞Spring Rest控制器。我的客户应该发送一个请求,不关心响应,不需要等待 这是我的服务器代码:Java “弹簧无阻塞支架”;“发送并忘记”;,java,spring,rest,spring-mvc,asynchronous,Java,Spring,Rest,Spring Mvc,Asynchronous,我正在编写一个非阻塞Spring Rest控制器。我的客户应该发送一个请求,不关心响应,不需要等待 这是我的服务器代码: @RestController @EnableAsync public class testController { @RequestMapping(value = "test", method = RequestMethod.GET) public ResponseEntity<String> test() throws InterruptedExceptio
@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!");
}
@RestController
@使能同步
公共类测试控制器{
@RequestMapping(value=“test”,method=RequestMethod.GET)
public ResponseEntity test()抛出InterruptedException{
timeConsumingMethod();
System.out.println(“我应该是第一”);
返回新的ResponseEntity(“服务器正在处理您的请求”,HttpStatus.OK);
}
@异步的
private void timeConsumingMethod()引发InterruptedException{
线程。睡眠(1000*5);
System.out.println(“我应该是第二名!”);
}
但是,当我使用(邮递员、Chrome等)呼叫时
我在服务器日志中获得以下信息:
我应该是第二名!
我应该是第一名
等待5秒钟后,我的浏览器才会显示:
服务器正在处理您的请求
这是“发送并忘记”行为的正确方式吗?根据文档页面,应该在配置类中添加 启用Spring的异步方法执行功能,类似于 Spring的XML名称空间中的功能 在@Configuration类上使用,如下所示,其中MyAsyncBean是 用户定义的类型,其中一个或多个方法用 Spring的@Async注释,EJB3.1@javax.EJB.Asynchronous 注释,或通过注释()指定的任何自定义注释 属性 你为什么不使用这个:
Webflux客户端似乎也这样做。我正在搜索一个类似的解决方案,其中1个微服务调用多个微服务异步,这符合模型
@async
不适用于在同一类上调用的方法。@enablesync
也不适用于控制器它应该在@Configuration
类上。@yuz I h同样的情况,但我以正确的方式构建了我的控制器和服务。idk不起作用…bean配置服务