春季非阻塞休假“发送并忘记" [英] Spring Non-blocking Rest "Send and forget"

查看:86
本文介绍了春季非阻塞休假“发送并忘记"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个无阻塞的Spring Rest控制器.我的客户应该发送请求,并且不关心响应,也不需要等待.

I'm writing a non-blocking Spring Rest controller. My client should send a request and doesn't care for the response and doesn't need to wait.

这是我的服务器代码:

@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!");
}

但是,当我使用(POSTMAN,Chrome等)调用 http://localhost:8181/test 时, ..) 我在服务器日志上得到以下信息:

However, When I call http://localhost:8181/test using(POSTMAN, Chrome, etc...) I get the following on the server log:

我应该排名第二!

我应该是第一位

并且仅在等待5秒后我的浏览器显示:

AND only after waiting 5 seconds my browser shows:

服务器正在处理您的请求

这是发送并忘记"行为的正确方法吗?

Is that the correct way for a "send and forget" Behavior?

推荐答案

根据文档页面,

According to the doc page the @EnableAsync should be added on configuration class.

启用Spring的异步方法执行功能,类似于 在Spring的XML名称空间中找到的功能.

Enables Spring's asynchronous method execution capability, similar to functionality found in Spring's XML namespace.

将在以下@Configuration类上使用,其中MyAsyncBean是 用户定义的类型,带有一个或多个使用以下方法之一注释的方法 Spring的@Async注释,EJB 3.1 @ javax.ejb.Asynchronous 注释,或通过comment()指定的任何自定义注释 属性.

To be used on @Configuration classes as follows, where MyAsyncBean is a user-defined type with one or more methods annotated with either Spring's @Async annotation, the EJB 3.1 @javax.ejb.Asynchronous annotation, or any custom annotation specified via the annotation() attribute.

这篇关于春季非阻塞休假“发送并忘记"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆